/** * Prepend data to an existing item. * * This method should throw an error if it is used with compressed data. This is an expected behavior. * Memcached casts the value to be prepended to the initial value to the type of the initial value. Be * careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23 to * (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer" * (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been * mimicked in the internal cache to produce similar results and improve consistency. It is recommend * that prepends only occur with data of the same type. * * @link http://www.php.net/manual/en/memcached.prepend.php * * @param string $key The key under which to store the value. * @param string $value Must be string as prepending mixed values is not well-defined. * @param string $group The group value prepended to the $key. * @param string $server_key The key identifying the server to store the value on. * @param bool $byKey True to store in internal cache by key; false to not store by key * @return bool Returns TRUE on success or FALSE on failure. */ public function prepend($key, $value, $group = 'default', $server_key = '', $byKey = false) { if (!is_string($value) && !is_int($value) && !is_float($value)) { return false; } $derived_key = $this->buildKey($key, $group); // If group is a non-Memcached group, prepend to runtime cache value, not Memcached if (in_array($group, $this->no_mc_groups)) { if (!isset($this->cache[$derived_key])) { return false; } $combined = $this->combine_values($this->cache[$derived_key], $value, 'pre'); $this->add_to_internal_cache($derived_key, $combined); return true; } // Append to Memcached value if (false !== $byKey) { $result = $this->mc->prependByKey($server_key, $derived_key, $value); } else { $result = $this->mc->prepend($derived_key, $value); } // Store in runtime cache if add was successful if (Memcached::RES_SUCCESS === $this->getResultCode()) { $combined = $this->combine_values($this->cache[$derived_key], $value, 'pre'); $this->add_to_internal_cache($derived_key, $combined); } return $result; }
/** * Prepends a value to the end of an existing cached value. * * If the value doesn't exist, it will be set with the given value * * @param String $key The key for the value * @param mixed $value The value to prepend * @param Integer $expire The lifespan of this cache value, in seconds * @return \r8\Cache\Memcached Returns a self reference */ public function prepend($key, $value, $expire = 0) { if (!$this->memcached->prepend($this->prepareKey($key), (string) $value)) { $this->set($key, $value, $expire); } $this->handleErrors(); return $this; }
public function testPrependReplaceFailed() { $request = new MemcacheGetRequest(); $request->addKey("widgets_float"); $request->setForCas(true); $response = new MemcacheGetResponse(); $item = $response->addItem(); $item->setKey("widgets_float"); $item->setValue("bar"); $item->setFlags(0); // string. $item->setCasId(123456); $this->apiProxyMock->expectCall('memcache', 'Get', $request, $response); $request = new MemcacheSetRequest(); $item = $request->addItem(); $item->setKey("widgets_float"); $item->setValue("foobar"); $item->setFlags(0); // string $item->setCasId(123456); $item->setSetPolicy(SetPolicy::CAS); $item->setExpirationTime(0); $response = new MemcacheSetResponse(); $response->addSetStatus(SetStatusCode::NOT_STORED); $this->apiProxyMock->expectCall('memcache', 'Set', $request, $response); $memcached = new Memcached(); $memcached->setOption(Memcached::OPT_PREFIX_KEY, "widgets_"); $this->assertFalse($memcached->prepend("float", "foo", 30)); $this->assertEquals($memcached->getResultCode(), Memcached::RES_NOTSTORED); $this->apiProxyMock->verify(); }
<?php $cache = new Memcached(); $cache->setOption(Memcached::OPT_BINARY_PROTOCOL, true); $cache->setOption(Memcached::OPT_COMPRESSION, false); $cache->addServer('localhost', 3434); $cache->add("add_key", "hello", 500); $cache->append("append_key", "world"); $cache->prepend("prepend_key", "world"); $cache->increment("incr", 2, 1, 500); $cache->decrement("decr", 2, 1, 500); $cache->delete("delete_k"); $cache->flush(1); var_dump($cache->get('get_this')); $cache->set('set_key', 'value 1', 100); $cache->replace('replace_key', 'value 2', 200); $cache->getStats(); $cache->quit(); sleep(1);
/** * @inheritdoc */ public function prepend($key, $value, $expiration = null) { $key = $this->prefix . $key; return parent::prepend($key, $value, $expiration); }