/** * {@inheritdoc} */ public function add(string $alias, FeatureInterface $feature) : StorageInterface { $key = $this->key($alias); $this->memcached->set($key, $this->feature($feature)); if (!$this->memcached->append($this->prefix, "|{$key}")) { $this->memcached->set($this->prefix, $key); } return $this; }
/** * Append 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 appended to the initial value to the * type of the initial value. Be careful as this leads to unexpected behavior at times. 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 appends only occur with data of * the same type. * * @link http://www.php.net/manual/en/memcached.append.php * * @param string $key The key under which to store the value. * @param mixed $value Must be string as appending mixed values is not well-defined. * @param string $group The group value appended 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 append($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, append 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, 'app'); $this->add_to_internal_cache($derived_key, $combined); return true; } // Append to Memcached value if (false !== $byKey) { $result = $this->mc->appendByKey($server_key, $derived_key, $value); } else { $result = $this->mc->append($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, 'app'); $this->add_to_internal_cache($derived_key, $combined); } return $result; }
/** * Appends 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 append * @param Integer $expire The lifespan of this cache value, in seconds * @return \r8\Cache\Memcached Returns a self reference */ public function append($key, $value, $expire = 0) { if (!$this->memcached->append($this->prepareKey($key), (string) $value)) { $this->set($key, $value, $expire); } $this->handleErrors(); return $this; }
public function append($key, $data) { $this->ensureTriedToConnect(); try { return $this->instance->append($key, $data); } catch (BaseException $e) { return $this->alive = false; } }
/** * {@inheritdoc } */ public function has($key) { /* It seems that the most efficient way to check has in memcached is by using an append with an empty string. However, we need to make sure that OPT_COMPRESSION is turned off because you can't append if you compressing data */ /* store for later use */ $cur_compression = $this->adapter->getOption(BaseMemcached::OPT_COMPRESSION); /* set compression off */ $this->adapter->setOption(BaseMemcached::OPT_COMPRESSION, false); $res = $this->adapter->append($this->buildKey($key), ''); $this->adapter->setOption(BaseMemcached::OPT_COMPRESSION, $cur_compression); return $res; }
/** * 向已存在元素后追加数据 * @param string|array $key * @param mixed $value */ public function append($key = NULL, $value = NULL) { $this->local_cache[$this->key_name($key)] = $value; switch ($this->client_type) { case 'Memcache': $append_status = $this->m->append($this->key_name($key), $value); break; default: case 'Memcached': $append_status = $this->m->append($this->key_name($key), $value); break; } return $append_status; }
public function testAppendCasRaceFailed() { $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("barfoo"); $item->setFlags(0); // string $item->setCasId(123456); $item->setSetPolicy(SetPolicy::CAS); $item->setExpirationTime(0); $response = new MemcacheSetResponse(); $response->addSetStatus(SetStatusCode::EXISTS); $this->apiProxyMock->expectCall('memcache', 'Set', $request, $response); $request = new MemcacheGetRequest(); $request->addKey("widgets_float"); $request->setForCas(true); $response = new MemcacheGetResponse(); $item = $response->addItem(); $item->setKey("widgets_float"); $item->setValue("baz"); $item->setFlags(0); // string. $item->setCasId(1234567); $this->apiProxyMock->expectCall('memcache', 'Get', $request, $response); $request = new MemcacheSetRequest(); $item = $request->addItem(); $item->setKey("widgets_float"); $item->setValue("bazfoo"); $item->setFlags(0); // string $item->setCasId(1234567); $item->setSetPolicy(SetPolicy::CAS); $item->setExpirationTime(0); $response = new MemcacheSetResponse(); $response->addSetStatus(SetStatusCode::STORED); $this->apiProxyMock->expectCall('memcache', 'Set', $request, $response); $memcached = new Memcached(); $memcached->setOption(Memcached::OPT_PREFIX_KEY, "widgets_"); $this->assertTrue($memcached->append("float", "foo", 30)); $this->assertEquals($memcached->getResultCode(), Memcached::RES_SUCCESS); $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 append($key, $value, $expiration = null) { $key = $this->prefix . $key; return parent::append($key, $value, $expiration); }