/** * Gets the value of a key. * * @param string $key * * @return mixed The value of the key. * * @throws KeyNotFoundException * @throws \Exception */ public function get($key) { $getResult = $this->getValue($key); $unserialized = @unserialize($getResult); if (Util::hasInternalExpireTime($unserialized)) { $getResult = $unserialized['v']; } return $getResult; }
/** * Sets the value of a key. * * @param string $key * @param mixed $value Can be any of serializable data type. * * @return bool True if the set was successful, false if it was unsuccessful. * * @throws \InvalidArgumentException * @throws InternalException */ public function set($key, $value) { Util::checkArgString($key); Util::checkArgSerializable($value); try { return $this->getAdapter()->set($key, $value); } catch (\Exception $e) { throw new InternalException($e->getMessage(), $e->getCode(), $e); } }
/** * Gets value, watches expiring. * * @param string $key * * @return mixed * * @throws KeyNotFoundException */ protected function getValue($key) { if (!array_key_exists($key, $this->store)) { throw new KeyNotFoundException(); } $getResult = $this->store[$key]; $unserialized = @unserialize($getResult); if (Util::hasInternalExpireTime($unserialized)) { $this->handleTtl($key, $unserialized['ts'], $unserialized['s']); $getResult = $unserialized['v']; } return $getResult; }
/** * Gets value, watches expiring. * * @param string $key * * @return mixed * * @throws KeyNotFoundException * @throws \Exception */ protected function getValue($key) { if (!$this->shmProxy->has($this->client, $key)) { throw new KeyNotFoundException(); } $getResult = $this->shmProxy->get($this->client, $key); $unserialized = @unserialize($getResult); if (Util::hasInternalExpireTime($unserialized)) { $this->handleTtl($key, $unserialized['ts'], $unserialized['s']); $getResult = $unserialized['v']; } return $getResult; }
/** * Removes the existing timeout on key, turning the key from volatile (a key with an expire set) * to persistent (a key that will never expire as no timeout is associated). * * @param string $key * * @return bool True if the persist was success, false if the persis was unsuccessful. * * @throws \Exception */ public function persist($key) { $getResult = $this->getValue($key); $unserialized = @unserialize($getResult); if (!Util::hasInternalExpireTime($unserialized)) { return false; } return $this->getClient()->replace($key, $unserialized['v'], 0); }
/** * @dataProvider kvsProvider * * @param KeyValueStore $kvs * @param MemcachedAdapter $dummyMemchdAdapter * @param \Memcached $dummyMemchd */ public function testPersist(KeyValueStore $kvs, MemcachedAdapter $dummyMemchdAdapter, \Memcached $dummyMemchd) { $dummyMemchd->shouldReceive('get')->with('key-e')->andReturn(Util::getDataWithExpire('value-e', 5, time())); $dummyMemchd->shouldReceive('getResultCode')->andReturn(\Memcached::RES_SUCCESS); $dummyMemchd->shouldReceive('replace')->andReturn(true); $this->assertTrue($kvs->persist('key-e')); }
/** * @dataProvider kvsProvider * * @param KeyValueStore $kvs * @param MemcacheAdapter $dummyMemchAdapter * @param \Memcache $dummyMemch */ public function testGetSerialized(KeyValueStore $kvs, MemcacheAdapter $dummyMemchAdapter, \Memcache $dummyMemch) { $dummyMemch->shouldReceive('get')->with('key-e')->andReturn(Util::getDataWithExpire('value-e', 5, time())); $this->assertEquals('value-e', $kvs->get('key-e')); }
/** * @dataProvider kvsProvider * * @param KeyValueStore $kvs * @param MemcachedAdapter $dummyMemchdAdapter * @param \Memcached $dummyMemchd */ public function testGetSerialized(KeyValueStore $kvs, MemcachedAdapter $dummyMemchdAdapter, \Memcached $dummyMemchd) { $dummyMemchd->shouldReceive('get')->with('key-e')->andReturn(Util::getDataWithExpire('value-e', 5, time())); $dummyMemchd->shouldReceive('getResultCode')->andReturn(\Memcached::RES_SUCCESS); $this->assertEquals('value-e', $kvs->get('key-e')); }
/** * Removes the existing timeout on key, turning the key from volatile (a key with an expire set) * to persistent (a key that will never expire as no timeout is associated). * * @param string $key * * @return bool True if the persist was success, false if the persis was unsuccessful. * * @throws \Exception */ public function persist($key) { try { $getResult = $this->getValue($key); } catch (KeyNotFoundException $e) { return false; } $unserialized = @unserialize($getResult); if (!Util::hasInternalExpireTime($unserialized)) { return false; } try { $this->handleTtl($key, $unserialized['ts'], $unserialized['s']); } catch (KeyNotFoundException $e) { return false; } return $this->set($key, $unserialized['v']); }
/** * Removes the existing timeout on key, turning the key from volatile (a key with an expire set) * to persistent (a key that will never expire as no timeout is associated). * * @param string $key * * @return bool True if the persist was success, false if the persis was unsuccessful. * * @throws \InvalidArgumentException * @throws InternalException */ public function persist($key) { Util::checkArgString($key); try { return $this->getAdapter()->persist($key); } catch (\Exception $e) { throw new InternalException($e->getMessage(), $e->getCode(), $e); } }
/** * Removes the existing timeout on key, turning the key from volatile (a key with an expire set) * to persistent (a key that will never expire as no timeout is associated). * * @param string $key * * @return bool True if the persist was success, false if the persis was unsuccessful. * * @throws \Exception */ public function persist($key) { $getResult = $this->getValue($key); $unserialized = @unserialize($getResult); if (!Util::hasInternalExpireTime($unserialized)) { throw new \Exception("{$key} has no associated timeout"); } return $this->getClient()->replace($key, $unserialized['v'], false, 0); }
/** * @dataProvider kvsProvider * * @param KeyValueStore $kvs * @param MemcacheAdapter $dummyMemchAdapter * @param \Memcache $dummyMemch */ public function testPersist(KeyValueStore $kvs, MemcacheAdapter $dummyMemchAdapter, \Memcache $dummyMemch) { $dummyMemch->shouldReceive('get')->with('key-e')->andReturn(Util::getDataWithExpire('value-e', 5, time())); $dummyMemch->shouldReceive('replace')->andReturn(true); $this->assertTrue($kvs->persist('key-e')); }