Commit 8abe98dd authored by Hannes Peterseim's avatar Hannes Peterseim
Browse files

adding an simple ttl handling

parent d819e87f
......@@ -8,13 +8,13 @@ https://www.petit-souris.de
YAMS is an small in memory data store, completely written in PHP.
It comes with an server component and an client libary for an simple use in your PHP Scripts without the need of installing big products like redis or memcache.
The whole project has an size of some kilobytes.
You can use it as an very fast in memory caching layer for small data of any type. Perfect for an real fast session storing.
You can use it as a very fast in memory caching layer for small data of any type. Perfect for a real fast session storing.
##installation
Copy the server script (server.php) to an save place and run it as an service with "php server.php &".
Standard port will be 5000, but you can define an own port via "php server.php port &" the server listens only on 127.0.0.1:port.
Do not forget to check the amount of memory an script can use, this defines the maximum size of the array the the data will be hold.
Do not forget to check the amount of memory a script can use, this defines the maximum size of the array the the data will be hold.
Copy the client script (client.php) to the place where you need it.
You can test everything with the supplied test script via "php test.php".
For your other use include it into your scripts via "use hp/yams/client;" if there is an autoloader or use "require('pathtothescript/client.php');" instead.
......@@ -155,12 +155,13 @@ $yamsClient->get('choosedkey');
###set
sets the value of an given key in a given store
set(key, value, store);
set(key, value, ttl store);
####parameters :
key : string 'choosedkey'
value : any value
key : string 'choosedkey'
value : any value
ttl : int 'seconds'
store : string 'choosedstore' (optional, if not setted, standard store will be used)
####return :
......@@ -170,7 +171,7 @@ true if the key could be setted, false if not
####example
$yamsClient->set('choosedkey', 'choosedvalue', 'choosedstore');
$yamsClient->set('choosedkey', 'choosedvalue', 300, 'choosedstore');
###del
......
......@@ -114,13 +114,13 @@ class client
* @param string $store
* @return bool|mixed
*/
public function set ($key = '', $value = '', $store = 'standard')
public function set ($key = '', $value = '', $ttl = 0, $store = 'standard')
{
if ($key == '') {
return false;
}
$msg = json_encode(['command' => 'set', 'key' => $key, 'value' => $value, 'store' => $store]);
$msg = json_encode(['command' => 'set', 'key' => $key, 'value' => $value, 'ttl' => $ttl, 'store' => $store]);
socket_write($this->socket, $msg, strlen($msg));
return $this->getreturn();
......
......@@ -134,7 +134,7 @@ class server
$this->write($client, $this->get($input['key'], $input['store']));
}
if ($input['command'] == 'set') {
$this->write($client, $this->set($input['key'], $input['value'], $input['store']));
$this->write($client, $this->set($input['key'], $input['value'], $input['ttl'], $input['store']));
}
if ($input['command'] == 'del') {
$this->write($client, $this->del($input['key'], $input['store']));
......@@ -184,9 +184,12 @@ class server
*/
private function exist ($key, $store)
{
if (isset($this->memMap[$store]['values'][$key])) {
if (isset($this->memMap[$store]['values'][$key]) && ($this->memMap[$store]['values'][$key]['ttl'] == 0 || $this->memMap[$store]['values'][$key]['ttl'] > time())) {
$msg = json_encode(['return' => 'ok', 'value' => true]);
} else {
if ($this->memMap[$store]['values'][$key]['ttl'] != 0 && $this->memMap[$store]['values'][$key]['ttl'] < time()) {
$this->killelement($key, $store);
}
$msg = json_encode(['return' => 'ok', 'value' => false]);
}
......@@ -202,9 +205,12 @@ class server
*/
private function get ($key, $store)
{
if ($key != '' && isset($this->memMap[$store]['values'][$key])) {
if ($key != '' && isset($this->memMap[$store]['values'][$key]) && ($this->memMap[$store]['values'][$key]['ttl'] == 0 || $this->memMap[$store]['values'][$key]['ttl'] > time())) {
$msg = json_encode(['return' => 'ok', 'value' => $this->memMap[$store]['values'][$key]['value']]);
} else {
if ($this->memMap[$store]['values'][$key]['ttl'] != 0 && $this->memMap[$store]['values'][$key]['ttl'] < time()) {
$this->killelement($key, $store);
}
$msg = json_encode(['return' => 'ok', 'value' => '']);
}
......@@ -216,13 +222,19 @@ class server
*
* @param string $key
* @param string $value
* @param string $ttl
* @param string $store
* @return false|string
*/
private function set ($key, $value, $store)
private function set ($key, $value, $ttl, $store)
{
if ($key != '') {
$this->memMap[$store]['values'][$key] = ['value' => $value];
if ($ttl > 0) {
$ttl = time() + (int) $ttl;
} else {
$ttl = 0;
}
$this->memMap[$store]['values'][$key] = ['value' => $value, 'ttl' => $ttl];
$this->memMap[$store]['elements']++;
$msg = json_encode(['return' => 'ok']);
} else {
......@@ -275,10 +287,13 @@ class server
*/
private function inc ($key, $store)
{
if ($key != '' && isset($this->memMap[$store]['values'][$key]) && is_numeric($this->memMap[$store]['values'][$key]['value'])) {
if ($key != '' && isset($this->memMap[$store]['values'][$key]) && is_numeric($this->memMap[$store]['values'][$key]['value']) && ($this->memMap[$store]['values'][$key]['ttl'] == 0 || $this->memMap[$store]['values'][$key]['ttl'] > time())) {
$this->memMap[$store]['values'][$key]['value']++;
$msg = json_encode(['return' => 'ok', 'value' => $this->memMap[$store]['values'][$key]['value']]);
} else {
if ($this->memMap[$store]['values'][$key]['ttl'] != 0 && $this->memMap[$store]['values'][$key]['ttl'] < time()) {
$this->killelement($key, $store);
}
$msg = json_encode(['return' => 'ok', 'value' => '']);
}
......@@ -294,10 +309,13 @@ class server
*/
private function dec ($key, $store)
{
if ($key != '' && isset($this->memMap[$store]['values'][$key])&& is_numeric($this->memMap[$store]['values'][$key]['value'])) {
if ($key != '' && isset($this->memMap[$store]['values'][$key])&& is_numeric($this->memMap[$store]['values'][$key]['value']) && ($this->memMap[$store]['values'][$key]['ttl'] == 0 || $this->memMap[$store]['values'][$key]['ttl'] > time())) {
$this->memMap[$store]['values'][$key]['value']--;
$msg = json_encode(['return' => 'ok', 'value' => $this->memMap[$store]['values'][$key]['value']]);
} else {
if ($this->memMap[$store]['values'][$key]['ttl'] != 0 && $this->memMap[$store]['values'][$key]['ttl'] < time()) {
$this->killelement($key, $store);
}
$msg = json_encode(['return' => 'ok', 'value' => '']);
}
......@@ -357,6 +375,17 @@ class server
return $msg;
}
/**
* deletes an element
*
* @param $key
* @param $store
*/
private function killelement ($key, $store)
{
unset($this->memMap[$store][$key]);
}
}
if (isset($_SERVER[1])) {
......
......@@ -41,6 +41,11 @@ class test
*/
private static $testvalue = 'testvalue';
/**
* @var string
*/
private static $testttl = '300';
/**
* @var int
*/
......@@ -90,7 +95,7 @@ class test
{
echo 'set test...';
$this->start();
$result = $this->client->set(self::$testkey,self::$testvalue,self::$teststore);
$result = $this->client->set(self::$testkey,self::$testvalue,self::$testttl,self::$teststore);
$this->end();
if ($result === true) {
echo 'successful' . "\n";
......@@ -135,6 +140,37 @@ class test
}
}
/**
* ttl function test
*
* @retrun void
*/
public function ttltest ()
{
echo 'ttl test...';
$this->start();
$result = $this->client->set(self::$testkey,self::$testvalue,1,self::$teststore);
$this->end();
if ($result == self::$testvalue) {
echo 'set successful';
} else {
echo 'set failed';
}
echo " sleeping 2 seconds.";
sleep(1);
echo ".";
sleep(1);
echo ".getting value should be false because of expired ttl ";
$this->start();
$result = $this->client->get(self::$testkey,self::$teststore);
$this->end();
if ($result == false) {
echo 'successful' . "\n";
} else {
echo 'failed' . "\n";
}
}
/**
* inc function test
*
......@@ -293,6 +329,7 @@ $test->inctest();
$test->dectest();
$test->statstest();
$test->deltest();
$test->ttltest();
$test->existstoretest();
$test->delstoretest();
$test->statstest();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment