Skip to content

Commit

Permalink
Improved performance by leaving a lot of useless data in Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
stekycz committed Nov 26, 2014
1 parent 9f2d16b commit b0fae69
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 65 deletions.
42 changes: 2 additions & 40 deletions src/Kdyby/Redis/RedisJournal.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,53 +148,15 @@ public function clean(array $conds, Nette\Caching\IStorage $storage = NULL)

$entries = array_unique($entries);

$removingKeys = array();
$removingKeyTags = array();
$removingKeyPriorities = array();
foreach ($entries as $key) {
if ($storage instanceof RedisStorage) {
$removingKeys[] = $key;
}
$removingKeyTags[] = $this->formatKey($key, self::TAGS);
$removingKeyPriorities[] = $this->formatKey($key, self::PRIORITY);
if (count($removingKeyTags) >= self::BATCH_SIZE) {
$this->cleanBatchData($removingKeys, $removingKeyPriorities, $removingKeyTags, $entries);
$removingKeys = array();
$removingKeyTags = array();
$removingKeyPriorities = array();
}
if ($storage instanceof RedisStorage && $entries) {
call_user_func_array(array($this->client, 'del'), $entries);
}

$this->cleanBatchData($removingKeys, $removingKeyPriorities, $removingKeyTags, $entries);

return $storage instanceof RedisStorage ? array() : $entries;
}



private function cleanBatchData(array $removingKeys, array $removingKeyPriorities, array $removingKeyTags, array $keys)
{
if ($removingKeyTags) {
if ($keys) {
$affectedTags = call_user_func_array(array($this->client, 'sunion'), array($removingKeyTags));
foreach ($affectedTags as $tag) {
if ($tag) {
call_user_func_array(array($this->client, 'sRem'), array_merge(array($this->formatKey($tag, self::KEYS)), $keys));
}
}
}
call_user_func_array(array($this->client, 'del'), $removingKeyTags);
}
if ($removingKeyPriorities) {
call_user_func_array(array($this->client, 'del'), $removingKeyPriorities);
}
if ($removingKeys) {
call_user_func_array(array($this->client, 'del'), $removingKeys);
}
}



/**
* @param int $priority
* @return array
Expand Down
37 changes: 17 additions & 20 deletions src/Kdyby/Redis/scripts/journal.clean.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,9 @@ if conds["all"] ~= nil then
end

local entries = {}
if conds["tags"] ~= nil then
for i, tag in pairs(conds["tags"]) do
local found = tagEntries(tag)
if #found > 0 then
cleanEntry(found)

for i, key in pairs(found) do
if conds["delete-entries"] ~= nil then
redis.call("del", key)
else
entries[#entries + 1] = key
end
end
end
end
end

if conds["priority"] ~= nil then
local found = priorityEntries(conds["priority"])
local processFoundKeys = function (found)
if #found > 0 then
cleanEntry(found)

for i, key in pairs(found) do
if conds["delete-entries"] ~= nil then
redis.call("del", key)
Expand All @@ -43,4 +24,20 @@ if conds["priority"] ~= nil then
end
end

if conds["tags"] ~= nil then
local formattedTagKeys = {}
for i, tag in pairs(conds["tags"]) do
processFoundKeys(tagEntries(tag))
formattedTagKeys[#formattedTagKeys + 1] = formatKey(tag, 'keys')
end
if #formattedTagKeys > 0 then
redis.call("del", unpack(formattedTagKeys))
end
end

if conds["priority"] ~= nil then
processFoundKeys(priorityEntries(conds["priority"]))
redis.call('zRemRangeByScore', formatKey('priority'), 0, conds["priority"])
end

return entries
10 changes: 5 additions & 5 deletions tests/KdybyTests/Redis/RedisJournal.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ class RedisJournalTest extends AbstractRedisTestCase
Assert::same('ok_test6_7', $result[0], "clean tag homepage/7");

$result = $this->journal->clean(array(Cache::TAGS => array('test:homepage/4')));
Assert::same(0, count($result), "clean non exists tag");
Assert::same(1, count($result), "clean non exists tag");

$result = $this->journal->clean(array(Cache::PRIORITY => 4));
Assert::same(0, count($result), "clean non exists priority");

$result = $this->journal->clean(array(Cache::TAGS => array('test:homepage')));
Assert::same(4, count($result), "clean other");
Assert::same(10, count($result), "clean other");
sort($result);
Assert::same(array('ok_test6_10', 'ok_test6_6', 'ok_test6_8', 'ok_test6_9'), $result, "clean other");
Assert::same(array('ok_test6_1', 'ok_test6_10', 'ok_test6_2', 'ok_test6_3', 'ok_test6_4', 'ok_test6_5', 'ok_test6_6', 'ok_test6_7', 'ok_test6_8', 'ok_test6_9'), $result, "clean other");
}


Expand Down Expand Up @@ -313,7 +313,7 @@ LUA;
$this->assertKeysInDatabase(5100);

$this->journal->clean(array(Cache::TAGS => 'test.4356'));
$this->assertKeysInDatabase(0);
$this->assertKeysInDatabase(5099);
}


Expand All @@ -340,7 +340,7 @@ LUA;
$this->assertKeysInDatabase(200001);

$this->journal->clean(array(Cache::TAGS => 'kdyby'));
$this->assertKeysInDatabase(0);
$this->assertKeysInDatabase(200000);
}


Expand Down

0 comments on commit b0fae69

Please sign in to comment.