Skip to content

Commit

Permalink
features update: expire, getTtl
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Balogh committed Sep 27, 2014
1 parent 7ef6f87 commit 376ddd1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 81 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": ">=5.4.0",
"adammbalogh/key-value-store": "~0.2",
"adammbalogh/key-value-store": "~0.3",
"fire015/flintstone": "~1.5"
},
"require-dev": {
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 41 additions & 26 deletions src/Adapter/FileAdapter/KeyTrait.php
@@ -1,6 +1,7 @@
<?php namespace AdammBalogh\KeyValueStore\Adapter\FileAdapter;

use AdammBalogh\KeyValueStore\Exception\NotImplementedException;
use AdammBalogh\KeyValueStore\Adapter\Helper;
use AdammBalogh\KeyValueStore\Exception\KeyNotFoundException;

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
Expand All @@ -22,29 +23,22 @@ public function delete($key)
}

/**
* Not implemented.
*
* @param string $key
* @param int $seconds
*
* @throws NotImplementedException
* @return bool True if the timeout was set, false if the timeout could not be set.
*
* @throws \Exception
*/
public function expire($key, $seconds)
{
throw new NotImplementedException();
}
try {
$value = $this->get($key);
} catch (KeyNotFoundException $e) {
return false;
}

/**
* Not implemented.
*
* @param string $key
* @param int $timestamp
*
* @throws NotImplementedException
*/
public function expireAt($key, $timestamp)
{
throw new NotImplementedException();
return $this->set($key, Helper::getDataWithExpire($value, $seconds, time()));
}

/**
Expand All @@ -58,17 +52,25 @@ public function getKeys()
}

/**
* Not implemented.
*
* Returns the remaining time to live of a key that has a timeout.
*
* @param string $key
*
* @throws NotImplementedException
* @return int Ttl in seconds
*
* @throws KeyNotFoundException
* @throws \Exception
*/
public function getTtl($key)
{
throw new NotImplementedException();
$getResult = $this->getValue($key);
$unserialized = @unserialize($getResult);

if (!Helper::hasInternalExpireTime($unserialized)) {
throw new \Exception('Cannot retrieve ttl');
}

return $unserialized['ts'] + $unserialized['s'] - time();
}

/**
Expand All @@ -80,21 +82,34 @@ public function getTtl($key)
*/
public function has($key)
{
return $this->getClient()->get($key) === false ? false : true;
try {
$this->get($key);
} catch (KeyNotFoundException $e) {
return false;
}

return true;
}

/**
* Not implemented.
*
* Remove 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
*
* @throws NotImplementedException
* @return bool True if the persist was success, false if the persis was unsuccessful.
*
* @throws \Exception
*/
public function persist($key)
{
throw new NotImplementedException();
$getResult = $this->getValue($key);
$unserialized = @unserialize($getResult);

if (!Helper::hasInternalExpireTime($unserialized)) {
throw new \Exception("{$key} has no associated timeout");
}

return $this->set($key, $unserialized['v']);
}
}
50 changes: 27 additions & 23 deletions src/Adapter/FileAdapter/StringTrait.php
@@ -1,5 +1,6 @@
<?php namespace AdammBalogh\KeyValueStore\Adapter\FileAdapter;

use AdammBalogh\KeyValueStore\Adapter\Helper;
use AdammBalogh\KeyValueStore\Exception\KeyNotFoundException;

/**
Expand Down Expand Up @@ -50,14 +51,7 @@ public function decrement($key)
*/
public function decrementBy($key, $decrement)
{
$storedValue = $this->get($key);
if (!$this->isInteger($storedValue)) {
throw new \Exception('The stored value is not an integer.');
}

$this->set($key, (string)($storedValue - $decrement));

return $storedValue - $decrement;
return $this->incrementBy($key, -$decrement);
}

/**
Expand All @@ -70,12 +64,20 @@ public function decrementBy($key, $decrement)
*/
public function get($key)
{
$storedValue = $this->getClient()->get($key);
if ($storedValue === false) {
throw new KeyNotFoundException($key);
$getResult = $this->getValue($key);
$unserialized = @unserialize($getResult);

if (Helper::hasInternalExpireTime($unserialized)) {

if (0 > $unserialized['ts'] + $unserialized['s'] - time()) {
$this->delete($key);
throw new KeyNotFoundException();
}

$getResult = $unserialized['v'];
}

return $storedValue;
return $getResult;
}

/**
Expand Down Expand Up @@ -116,9 +118,8 @@ public function increment($key)
public function incrementBy($key, $increment)
{
$storedValue = $this->get($key);
if (!$this->isInteger($storedValue)) {
throw new \Exception('The stored value is not an integer.');
}

Helper::checkInteger($storedValue);

$this->set($key, (string)($storedValue + $increment));

Expand Down Expand Up @@ -148,24 +149,27 @@ public function set($key, $value)
*/
public function setIfNotExists($key, $value)
{
$storedValue = $this->getClient()->get($key);
if ($storedValue !== false) {
if ($this->has($key)) {
return false;
}

return $this->set($key, $value);
}

/**
* @param mixed $number
* @param string $key
*
* @return bool
* @return string
* @throws KeyNotFoundException
* @throws \Exception
*/
private function isInteger($number)
protected function getValue($key)
{
if (is_numeric($number) && is_integer($number + 0)) {
return true;
$getResult = $this->getClient()->get($key);
if ($getResult === false) {
throw new KeyNotFoundException($key);
}
return false;

return $getResult;
}
}
53 changes: 33 additions & 20 deletions tests/Adapter/FileAdapter/FileAdapterKeyTest.php
Expand Up @@ -24,24 +24,21 @@ public function testDeleteWithWrongKey()
$this->kvs->delete(str_repeat('a', 2048));
}

/**
* Not implemented.
*
* @expectedException \AdammBalogh\KeyValueStore\Exception\InternalException
*/
public function testExpire()
{
$this->kvs->expire('key', 10);
$this->kvs->set('key', 'value');
$this->assertTrue($this->kvs->expire('key', 1));

$this->assertTrue($this->kvs->has('key'));

sleep(3);

$this->assertFalse($this->kvs->has('key'));
}

/**
* Not implemented.
*
* @expectedException \AdammBalogh\KeyValueStore\Exception\InternalException
*/
public function testExpireAt()
public function testExpireWithNotExistedKey()
{
$this->kvs->expireAt('key', time());
$this->assertFalse($this->kvs->expire('key', 1));
}

public function testGetKeysWithEmpty()
Expand All @@ -56,13 +53,21 @@ public function testGetKeysWithNotEmpty()
$this->assertCount(1, $this->kvs->getKeys());
}

public function testGetTtl()
{
$this->kvs->set('key', 'value');
$this->kvs->expire('key', 10);

$this->assertGreaterThan(0, $this->kvs->getTtl('key'));
}

/**
* Not implemented.
*
* @expectedException \AdammBalogh\KeyValueStore\Exception\InternalException
*/
public function testGetTtl()
public function testGetTtlOnPersistentKey()
{
$this->kvs->set('key', 'value');

$this->kvs->getTtl('key');
}

Expand All @@ -83,13 +88,21 @@ public function testHasWithWrongKey()
$this->kvs->has(str_repeat('a', 2048));
}

public function testPersist()
{
$this->kvs->set('key', 'value');
$this->kvs->expire('key', 1);

$this->assertTrue($this->kvs->persist('key'));
}

/**
* Not implemented.
*
* @expectedException \AdammBalogh\KeyValueStore\Exception\InternalException
*/
public function testPersist()
public function testPersistWithPersistentKey()
{
$this->kvs->persist('key');
$this->kvs->set('key', 'value');

$this->assertTrue($this->kvs->persist('key'));
}
}

0 comments on commit 376ddd1

Please sign in to comment.