|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Flowpack\DecoupledContentStore\Core; |
| 4 | + |
| 5 | +use Flowpack\DecoupledContentStore\Core\Domain\ValueObject\RedisInstanceIdentifier; |
| 6 | +use Flowpack\DecoupledContentStore\Core\Infrastructure\RedisClientManager; |
| 7 | +use Neos\Flow\Annotations as Flow; |
| 8 | + |
| 9 | +/** |
| 10 | + * @Flow\Scope("singleton") |
| 11 | + */ |
| 12 | +class RedisPruneService |
| 13 | +{ |
| 14 | + // go through all keys in the selected content store |
| 15 | + // check wether they are reserved keys or currently active or contain one of the currently registered releases ids |
| 16 | + // if not: delete |
| 17 | + const PRUNE_LUA_SCRIPT = ' |
| 18 | + local contentStoreCurrent = redis.call("GET", "contentStore:current") |
| 19 | + local contentStoreAllKeys = redis.call("KEYS", "*") |
| 20 | + local contentStoreRegisteredReleases = redis.call("ZRANGE", "contentStore:registeredReleases", 0, -1) |
| 21 | + local currentContentStoreStart = "contentStore:" .. contentStoreCurrent |
| 22 | +
|
| 23 | + local function table_contains_value(tab, val) |
| 24 | + for index,value in ipairs(tab) do |
| 25 | + if string.find(val, value) then |
| 26 | + return true |
| 27 | + end |
| 28 | + end |
| 29 | + return false |
| 30 | + end |
| 31 | +
|
| 32 | + for index,contentStoreKey in ipairs(contentStoreAllKeys) do |
| 33 | + if contentStoreKey ~= "contentStore:current" |
| 34 | + and contentStoreKey ~= "contentStore:registeredReleases" |
| 35 | + and string.sub(contentStoreKey, 1, string.len(currentContentStoreStart)) ~= currentContentStoreStart |
| 36 | + and not table_contains_value(contentStoreRegisteredReleases, contentStoreKey) then |
| 37 | + redis.call("DEL", contentStoreKey) |
| 38 | + end |
| 39 | + end |
| 40 | + '; |
| 41 | + |
| 42 | + /** |
| 43 | + * @Flow\Inject |
| 44 | + * @var RedisClientManager |
| 45 | + */ |
| 46 | + protected $redisClientManager; |
| 47 | + |
| 48 | + |
| 49 | + public function pruneRedisInstance(RedisInstanceIdentifier $redisInstanceIdentifier) |
| 50 | + { |
| 51 | + $this->redisClientManager->getRedis($redisInstanceIdentifier)->eval(self::PRUNE_LUA_SCRIPT, []); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments