Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Open sidebar
Administrator
yams---yet-another-memory-store
Commits
90422f90
Commit
90422f90
authored
Jan 31, 2021
by
Hannes Peterseim
Browse files
adding some ttl commands + adding fancy colors for test output
parent
8abe98dd
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
311 additions
and
87 deletions
+311
-87
README.md
README.md
+63
-2
client.php
client.php
+59
-0
server.php
server.php
+110
-42
test.php
test.php
+79
-43
No files found.
README.md
View file @
90422f90
...
...
@@ -81,7 +81,7 @@ $yamsClient->addstore('choosedstore');
###existstore
detemines weth
d
er an dedicated memory exists
detemines wether an dedicated memory exists
existstore(store);
####parameters :
...
...
@@ -161,7 +161,7 @@ set(key, value, ttl store);
key : string 'choosedkey'
value : any value
ttl : int
'
seconds
'
ttl : int seconds
, 0 if never expires
store : string 'choosedstore' (optional, if not setted, standard store will be used)
####return :
...
...
@@ -229,3 +229,64 @@ integer value of the key if decrement was sucessful, false if not
####example
$yamsClient->dec('choosedkey', 'choosedstore');
###getttl
gets the ttl an given key in the given in seconds left
getttl(key, store);
####parameters :
key : string 'choosedkey'
store : string 'choosedstore' (optional, if not setted, standard store will be used)
####return :
integer|boolean
integer value of the key if exists and ttl > 0, false if not
####example
$yamsClient->getttl('choosedkey', 'choosedstore');
###addttl
adds seconds to the ttl an given key in the given
addttl(key, ttl, store);
####parameters :
key : string 'choosedkey'
ttl : int 'amountofseconds'
store : string 'choosedstore' (optional, if not setted, standard store will be used)
####return :
integer|boolean
integer seconds the object will live if the key exists and ttl > 0, false if not
####example
$yamsClient->addttl('choosedkey', 5, 'choosedstore');
###delttl
reduces seconds from the ttl an given key in the given
delttl(key, ttl, store);
####parameters :
key : string 'choosedkey'
ttl : int 'amountofseconds'
store : string 'choosedstore' (optional, if not setted, standard store will be used)
####return :
integer|boolean
integer seconds the object will live if the key exists and ttl > 0, false if not
####example
$yamsClient->addttl('choosedkey', 5, 'choosedstore');
client.php
View file @
90422f90
...
...
@@ -237,6 +237,65 @@ class client
return
$this
->
getreturn
();
}
/**
* gets the ttl for the given key in the given store
*
* @param string $key
* @param string $store
* @return bool|mixed
*/
public
function
getttl
(
$key
=
''
,
$store
=
'standard'
)
{
if
(
$key
==
''
)
{
return
false
;
}
$msg
=
json_encode
([
'command'
=>
'getttl'
,
'key'
=>
$key
,
'store'
=>
$store
]);
socket_write
(
$this
->
socket
,
$msg
,
strlen
(
$msg
));
return
$this
->
getreturn
();
}
/**
* adds seconds to the ttl of the given key in the given store
*
* @param string $key
* @param string $store
* @param string $ttl
* @return bool|mixed
*/
public
function
addttl
(
$key
=
''
,
$ttl
=
0
,
$store
=
'standard'
)
{
if
(
$key
==
''
)
{
return
false
;
}
$msg
=
json_encode
([
'command'
=>
'addttl'
,
'key'
=>
$key
,
'ttl'
=>
$ttl
,
'store'
=>
$store
]);
socket_write
(
$this
->
socket
,
$msg
,
strlen
(
$msg
));
return
$this
->
getreturn
();
}
/**
* reduces seconds from the ttl of the given key in the given store
*
* @param string $key
* @param string $store
* @param string $ttl
* @return bool|mixed
*/
public
function
delttl
(
$key
=
''
,
$ttl
=
0
,
$store
=
'standard'
)
{
if
(
$key
==
''
)
{
return
false
;
}
$msg
=
json_encode
([
'command'
=>
'delttl'
,
'key'
=>
$key
,
'store'
=>
$store
,
'ttl'
=>
$ttl
]);
socket_write
(
$this
->
socket
,
$msg
,
strlen
(
$msg
));
return
$this
->
getreturn
();
}
/**
* closes the server connection
*
...
...
server.php
View file @
90422f90
...
...
@@ -127,35 +127,46 @@ class server
socket_close
(
$client
);
break
;
}
if
(
$input
[
'command'
]
==
'exist'
)
{
$this
->
write
(
$client
,
$this
->
exist
(
$input
[
'key'
],
$input
[
'store'
]));
}
if
(
$input
[
'command'
]
==
'get'
)
{
$this
->
write
(
$client
,
$this
->
get
(
$input
[
'key'
],
$input
[
'store'
]));
}
if
(
$input
[
'command'
]
==
'set'
)
{
$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'
]));
}
if
(
$input
[
'command'
]
==
'stats'
)
{
$this
->
write
(
$client
,
$this
->
stats
());
}
if
(
$input
[
'command'
]
==
'inc'
)
{
$this
->
write
(
$client
,
$this
->
inc
(
$input
[
'key'
],
$input
[
'store'
]));
}
if
(
$input
[
'command'
]
==
'dec'
)
{
$this
->
write
(
$client
,
$this
->
dec
(
$input
[
'key'
],
$input
[
'store'
]));
}
if
(
$input
[
'command'
]
==
'addstore'
)
{
$this
->
write
(
$client
,
$this
->
addstore
(
$input
[
'memstore'
]));
}
if
(
$input
[
'command'
]
==
'existstore'
)
{
$this
->
write
(
$client
,
$this
->
existstore
(
$input
[
'store'
]));
}
if
(
$input
[
'command'
]
==
'delstore'
)
{
$this
->
write
(
$client
,
$this
->
delstore
(
$input
[
'memstore'
]));
switch
(
$input
[
'command'
])
{
case
'exist'
:
$this
->
write
(
$client
,
$this
->
exist
(
$input
[
'key'
],
$input
[
'store'
]));
break
;
case
'get'
:
$this
->
write
(
$client
,
$this
->
get
(
$input
[
'key'
],
$input
[
'store'
]));
break
;
case
'set'
:
$this
->
write
(
$client
,
$this
->
set
(
$input
[
'key'
],
$input
[
'value'
],
(
int
)
$input
[
'ttl'
],
$input
[
'store'
]));
break
;
case
'del'
:
$this
->
write
(
$client
,
$this
->
del
(
$input
[
'key'
],
$input
[
'store'
]));
break
;
case
'stats'
:
$this
->
write
(
$client
,
$this
->
stats
());
break
;
case
'inc'
:
$this
->
write
(
$client
,
$this
->
inc
(
$input
[
'key'
],
$input
[
'store'
]));
break
;
case
'dec'
:
$this
->
write
(
$client
,
$this
->
dec
(
$input
[
'key'
],
$input
[
'store'
]));
break
;
case
'addstore'
:
$this
->
write
(
$client
,
$this
->
addstore
(
$input
[
'memstore'
]));
break
;
case
'existstore'
:
$this
->
write
(
$client
,
$this
->
existstore
(
$input
[
'store'
]));
break
;
case
'delstore'
:
$this
->
write
(
$client
,
$this
->
delstore
(
$input
[
'memstore'
]));
break
;
case
'getttl'
:
$this
->
write
(
$client
,
$this
->
getttl
(
$input
[
'key'
],
$input
[
'store'
]));
break
;
case
'addttl'
:
$this
->
write
(
$client
,
$this
->
addttl
(
$input
[
'key'
],
$input
[
'ttl'
],
$input
[
'store'
]));
break
;
case
'delttl'
:
$this
->
write
(
$client
,
$this
->
delttl
(
$input
[
'key'
],
$input
[
'ttl'
],
$input
[
'store'
]));
break
;
}
}
}
...
...
@@ -187,9 +198,7 @@ class server
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
);
}
$this
->
killelement
(
$key
,
$store
);
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
false
]);
}
...
...
@@ -208,9 +217,7 @@ class server
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
);
}
$this
->
killelement
(
$key
,
$store
);
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
''
]);
}
...
...
@@ -291,9 +298,7 @@ class server
$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
);
}
$this
->
killelement
(
$key
,
$store
);
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
''
]);
}
...
...
@@ -313,9 +318,7 @@ class server
$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
);
}
$this
->
killelement
(
$key
,
$store
);
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
''
]);
}
...
...
@@ -375,6 +378,69 @@ class server
return
$msg
;
}
/**
* gets the seconds this object have to live
*
* @param string $key
* @param string $store
* @return false|string
*/
private
function
getttl
(
$key
,
$store
)
{
if
(
isset
(
$this
->
memMap
[
$store
][
'values'
][
$key
])
&&
(
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
==
0
||
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
>
time
()))
{
$ttl
=
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
-
time
();
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
$ttl
]);
}
else
{
$this
->
killelement
(
$key
,
$store
);
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
false
]);
}
return
$msg
;
}
/**
* adds x seconds to objects ttl
*
* @param string $key
* @param string $ttl
* @param string $store
* @return false|string
*/
private
function
addttl
(
$key
,
$ttl
,
$store
)
{
if
(
isset
(
$this
->
memMap
[
$store
][
'values'
][
$key
])
&&
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
>
time
())
{
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
+=
(
int
)
$ttl
;
$ttl
=
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
-
time
();
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
$ttl
]);
}
else
{
$this
->
killelement
(
$key
,
$store
);
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
false
]);
}
return
$msg
;
}
/**
* reduces x seconds from objects ttl
*
* @param string $key
* @param string $ttl
* @param string $store
* @return false|string
*/
private
function
delttl
(
$key
,
$ttl
,
$store
)
{
if
(
isset
(
$this
->
memMap
[
$store
][
'values'
][
$key
])
&&
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
>
time
())
{
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
-=
(
int
)
$ttl
;
$ttl
=
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
-
time
();
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
$ttl
]);
}
else
{
$this
->
killelement
(
$key
,
$store
);
$msg
=
json_encode
([
'return'
=>
'ok'
,
'value'
=>
false
]);
}
return
$msg
;
}
/**
* deletes an element
*
...
...
@@ -383,7 +449,9 @@ class server
*/
private
function
killelement
(
$key
,
$store
)
{
unset
(
$this
->
memMap
[
$store
][
$key
]);
if
(
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
!=
0
&&
$this
->
memMap
[
$store
][
'values'
][
$key
][
'ttl'
]
<
time
())
{
unset
(
$this
->
memMap
[
$store
][
$key
]);
}
}
}
...
...
test.php
View file @
90422f90
...
...
@@ -57,12 +57,12 @@ class test
public
function
__construct
()
{
$this
->
client
=
new
client
();
echo
'connect test...'
;
echo
'
\e[0m
connect test...'
;
$this
->
start
();
$result
=
$this
->
client
->
connect
();
$this
->
end
();
if
(
$result
===
true
)
{
echo
'successful'
.
"
\n
"
;
echo
'
\e[32m
successful'
.
"
\n
"
;
}
else
{
echo
'failed'
.
"
\n
"
;
}
...
...
@@ -75,14 +75,14 @@ class test
*/
public
function
statstest
()
{
echo
'
stats test...
'
;
echo
"
\e
[0m
stats test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
stats
();
$this
->
end
();
if
(
is_array
(
$result
))
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -93,14 +93,14 @@ class test
*/
public
function
settest
()
{
echo
'
set test...
'
;
echo
"
\e
[0m
set test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
set
(
self
::
$testkey
,
self
::
$testvalue
,
self
::
$testttl
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
===
true
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -111,12 +111,12 @@ class test
*/
public
function
existtest
()
{
echo
'
exist test...
'
;
echo
"
\e
[0m
exist test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
exist
(
self
::
$testkey
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
===
true
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'failed'
.
"
\n
"
;
}
...
...
@@ -129,14 +129,14 @@ class test
*/
public
function
gettest
()
{
echo
'
get test...
'
;
echo
"
\e
[0m
get test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
get
(
self
::
$testkey
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
==
self
::
$testvalue
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -147,27 +147,63 @@ class test
*/
public
function
ttltest
()
{
echo
'
ttl test...
'
;
echo
"
\e
[0m
ttl test
s
...
\n
setting test object..."
;
$this
->
start
();
$result
=
$this
->
client
->
set
(
self
::
$testkey
,
self
::
$testvalue
,
1
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
==
self
::
$testvalue
)
{
echo
'set
successful
'
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'set
failed
'
;
echo
"
\e
[31m
failed
\n
"
;
}
echo
" sleeping 2 seconds."
;
echo
"
\e
[0m
sleeping 2 seconds."
;
sleep
(
1
);
echo
"."
;
sleep
(
1
);
echo
".
getting value should be false because of
expired ttl
"
;
echo
".
checking for
expired ttl
...
"
;
$this
->
start
();
$result
=
$this
->
client
->
get
(
self
::
$testkey
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
==
false
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'failed'
.
"
\n
"
;
echo
"
\e
[31mfailed
\n
"
;
}
echo
"
\e
[0m setting test object..."
;
$this
->
start
();
$result
=
$this
->
client
->
set
(
self
::
$testkey
,
self
::
$testvalue
,
self
::
$testttl
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
==
self
::
$testvalue
)
{
echo
"set
\e
[32msuccessful
\n
"
;
}
else
{
echo
"set failed
\n
"
;
}
echo
"
\e
[0m getting ttl..."
;
$this
->
start
();
$result
=
$this
->
client
->
getttl
(
self
::
$testkey
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
==
300
)
{
echo
"
\e
[32msuccessful
\n
"
;
}
else
{
echo
"
\e
[31mfailed
\n
"
;
}
echo
"
\e
[0m adding ttl..."
;
$this
->
start
();
$result
=
$this
->
client
->
addttl
(
self
::
$testkey
,
5
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
==
305
||
$result
==
304
)
{
echo
"
\e
[32msuccessful
\n
"
;
}
else
{
echo
"
\e
[31mfailed
\n
"
;
}
echo
"
\e
[0m deleting ttl.."
;
$this
->
start
();
$result
=
$this
->
client
->
delttl
(
self
::
$testkey
,
5
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
==
299
||
$result
==
300
)
{
echo
"
\e
[32msuccessful
\n
"
;
}
else
{
echo
"
\e
[31mfailed
\n
"
;
}
}
...
...
@@ -179,14 +215,14 @@ class test
public
function
inctest
()
{
$this
->
client
->
set
(
'incdectest'
,
1
);
echo
'
inc test...
'
;
echo
"
\e
[0m
inc test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
inc
(
'incdectest'
);
$this
->
end
();
if
(
$result
==
2
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -197,14 +233,14 @@ class test
*/
public
function
dectest
()
{
echo
'
dec test...
'
;
echo
"
\e
[0m
dec test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
dec
(
'incdectest'
);
$this
->
end
();
if
(
$result
==
1
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -215,14 +251,14 @@ class test
*/
public
function
deltest
()
{
echo
'
del test...
'
;
echo
"
\e
[0m
del test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
del
(
self
::
$testkey
,
self
::
$teststore
);
$this
->
end
();
if
(
$result
===
true
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -233,14 +269,14 @@ class test
*/
public
function
addstoretest
()
{
echo
'
addstore test...
'
;
echo
"
\e
[0m
addstore test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
addstore
(
self
::
$teststore
);
$this
->
end
();
if
(
$result
===
true
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -251,14 +287,14 @@ class test
*/
public
function
existstoretest
()
{
echo
'
existstore test...
'
;
echo
"
\e
[0m
existstore test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
existstore
(
self
::
$teststore
);
$this
->
end
();
if
(
$result
===
true
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -269,14 +305,14 @@ class test
*/
public
function
delstoretest
()
{
echo
'
delstore test...
'
;
echo
"
\e
[0m
delstore test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
delstore
(
self
::
$teststore
);
$this
->
end
();
if
(
$result
===
true
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}
}
...
...
@@ -287,14 +323,14 @@ class test
*/
public
function
closetest
()
{
echo
'
close test...
'
;
echo
"
\e
[0m
close test...
"
;
$this
->
start
();
$result
=
$this
->
client
->
close
();
$this
->
end
();
if
(
$result
===
true
)
{
echo
'
successful
'
.
"
\n
"
;
echo
"
\e
[32m
successful
\n
"
;
}
else
{
echo
'
failed
'
.
"
\n
"
;
echo
"
\e
[31m
failed
\n
"
;
}