Skip to content

Commit

Permalink
make scripts static to reduce Redis cache memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
limen committed Nov 29, 2018
1 parent e20b7f5 commit f477d1a
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/
.idea/
vendor/
composer.lock
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"license": "MIT",
"require": {
"php": ">=5.5",
"predis/predis": "1.1.*"
"predis/predis": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
Expand Down
7 changes: 5 additions & 2 deletions src/Commands/GetsetHashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class GetsetHashCommand extends Command
{
public function getScript()
{
$elementsPart = $this->joinArguments();
$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;

Expand All @@ -16,7 +15,11 @@ public function getScript()
local ttl = redis.pcall('ttl', v);
values[#values+1] = redis.pcall('hgetall',v);
redis.pcall('del',v);
redis.pcall('hmset',v,$elementsPart);
local j=1
while j<#ARGV do
redis.pcall('hset',v,ARGV[j],ARGV[j+1]);
j=j+2
end
if setTtl == 1 then
$luaSetTtl
elseif ttl >= 0 then
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/GetsetListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class GetsetListCommand extends Command
{
public function getScript()
{
$elementsPart = $this->joinArguments();
$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;

Expand All @@ -16,7 +15,9 @@ public function getScript()
local ttl = redis.pcall('ttl', v);
values[#values+1] = redis.pcall('lrange',v,0,-1);
redis.pcall('del',v);
redis.pcall('rpush',v,$elementsPart);
for j=1,#ARGV do
redis.pcall('rpush',v,ARGV[j]);
end
if setTtl == 1 then
$luaSetTtl
elseif ttl >= 0 then
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/GetsetSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class GetsetSetCommand extends Command
{
public function getScript()
{
$elementsPart = $this->joinArguments();
$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;

Expand All @@ -16,7 +15,9 @@ public function getScript()
local ttl = redis.pcall('ttl', v);
values[#values+1] = redis.pcall('smembers',v);
redis.pcall('del',v);
redis.pcall('sadd',v,$elementsPart);
for j=1,#ARGV do
redis.pcall('sadd',v,ARGV[j]);
end
if setTtl == 1 then
$luaSetTtl
elseif ttl >= 0 then
Expand Down
7 changes: 5 additions & 2 deletions src/Commands/GetsetZsetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class GetsetZsetCommand extends Command
{
public function getScript()
{
$elementsPart = $this->joinArguments();
$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;

Expand All @@ -16,7 +15,11 @@ public function getScript()
local ttl = redis.pcall('ttl', v);
values[#values+1] = redis.pcall('zrange', v, 0, -1);
redis.pcall('del',v);
redis.pcall('zadd', v, $elementsPart);
local j=1;
while j<#ARGV do
redis.pcall('zadd',v,ARGV[j],ARGV[j+1]);
j=j+2
end
if setTtl == 1 then
$luaSetTtl
elseif ttl >= 0 then
Expand Down
7 changes: 5 additions & 2 deletions src/Commands/HmsetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class HmsetCommand extends Command
{
public function getScript()
{
$argString = $this->joinArguments();
$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;
$checkExist = $this->existenceScript;
Expand All @@ -27,7 +26,11 @@ public function getScript()
for i,v in ipairs(KEYS) do
local ttl = redis.pcall('ttl', v)
$delScript
values[#values+1] = redis.pcall('hmset',v, $argString);
local j=1
while j<#ARGV do
values[i]=redis.pcall('hset',v,ARGV[j],ARGV[j+1]);
j=j+2
end
if setTtl == '1' then
$luaSetTtl
elseif ttl > 0 then
Expand Down
8 changes: 5 additions & 3 deletions src/Commands/LpushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class LpushCommand extends Command
{
public function getScript()
{
$elementsPart = $this->joinArguments();
$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;
$checkScript = $this->existenceScript;
Expand All @@ -18,13 +17,16 @@ public function getScript()
for i,v in ipairs(KEYS) do
local ttl = redis.pcall('ttl', v)
$delScript
local rs = redis.pcall('lpush',v,$elementsPart);
local rs
for j=1,#ARGV do
rs=redis.pcall('lpush',v,ARGV[j]);
end
if setTtl=='1' then
$luaSetTtl
elseif ttl > 0 then
redis.pcall('expire', v, ttl)
end
values[#values+1] = rs;
values[#values+1]=rs;
end
return {KEYS,values};
LUA;
Expand Down
9 changes: 5 additions & 4 deletions src/Commands/RpushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class RpushCommand extends Command
{
public function getScript()
{
$elementsPart = $this->joinArguments();

$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;
$checkScript = $this->existenceScript;
Expand All @@ -28,13 +26,16 @@ public function getScript()
for i,v in ipairs(KEYS) do
local ttl = redis.pcall('ttl', v)
$delScript
local rs = redis.pcall('rpush',v,$elementsPart);
local rs
for j=1,#ARGV do
rs=redis.pcall('rpush',v,ARGV[j]);
end
if setTtl=='1' then
$luaSetTtl
elseif ttl > 0 then
redis.pcall('expire', v, ttl)
end
values[#values+1] = rs;
values[#values+1]=rs;
end
return {KEYS,values};
LUA;
Expand Down
12 changes: 7 additions & 5 deletions src/Commands/SaddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class SaddCommand extends Command
{
public function getScript()
{
$elementsPart = $this->joinArguments();
$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;
$checkScript = $this->existenceScript;
Expand All @@ -27,16 +26,19 @@ public function getScript()
for i,v in ipairs(KEYS) do
local ttl = redis.pcall('ttl', v)
$delScript
local rs1 = redis.pcall('sadd', v, $elementsPart);
local rs1
for j=1,#ARGV do
rs1=redis.pcall('sadd',v,ARGV[j]);
end
if rs1 then
if setTtl=='1' then
$luaSetTtl
elseif ttl > 0 then
redis.pcall('expire', v, ttl)
redis.pcall('expire',v,ttl)
end
values[#values+1] = rs1;
values[#values+1]=rs1;
else
values[#values+1] = nil;
values[#values+1]=nil;
end
end
return {KEYS,values};
Expand Down
8 changes: 6 additions & 2 deletions src/Commands/ZaddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class ZaddCommand extends Command
{
public function getScript()
{
$elementsPart = $this->joinArguments();
$luaSetTtl = $this->luaSetTtl($this->getTtl());
$setTtl = $luaSetTtl ? 1 : 0;
$checkScript = $this->existenceScript;
Expand All @@ -18,7 +17,12 @@ public function getScript()
for i,v in ipairs(KEYS) do
local ttl = redis.pcall('ttl', v)
$delScript
local rs1 = redis.pcall('zadd', v, $elementsPart);
local rs1
local j=1
while j<#ARGV do
rs1=redis.pcall('zadd',v,ARGV[j],ARGV[j+1]);
j=j+2
end
if rs1 then
if setTtl=='1' then
$luaSetTtl
Expand Down

0 comments on commit f477d1a

Please sign in to comment.