Browse Source

redis: move lua scripts to class and add type hints

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/37758/head
Varun Patil 3 years ago
parent
commit
c6cee282b3
  1. 12
      lib/private/Memcache/Redis.php
  2. 4
      tests/lib/Memcache/RedisTest.php

12
lib/private/Memcache/Redis.php

@ -31,8 +31,9 @@ namespace OC\Memcache;
use OCP\IMemcacheTTL;
class Redis extends Cache implements IMemcacheTTL {
/** name => [script, sha1] */
const LUA_SCRIPTS = [
public const LUA_SCRIPTS = [
'dec' => [
'if redis.call("exists", KEYS[1]) == 1 then return redis.call("decrby", KEYS[1], ARGV[1]) else return "NEX" end',
'720b40cb66cef1579f2ef16ec69b3da8c85510e9',
@ -47,7 +48,6 @@ const LUA_SCRIPTS = [
],
];
class Redis extends Cache implements IMemcacheTTL {
/**
* @var \Redis|\RedisCluster $cache
*/
@ -185,10 +185,10 @@ class Redis extends Cache implements IMemcacheTTL {
return \OC::$server->getGetRedisFactory()->isAvailable();
}
protected function evalLua($scriptName, $keys, $args) {
protected function evalLua(string $scriptName, array $keys, array $args) {
$keys = array_map(fn ($key) => $this->getPrefix() . $key, $keys);
$args = array_merge($keys, $args);
$script = LUA_SCRIPTS[$scriptName];
$script = self::LUA_SCRIPTS[$scriptName];
$result = $this->getCache()->evalSha($script[1], $args, count($keys));
if (false === $result) {
@ -198,11 +198,11 @@ class Redis extends Cache implements IMemcacheTTL {
return $result;
}
protected static function encodeValue($value) {
protected static function encodeValue(mixed $value): string {
return is_int($value) ? (string) $value : json_encode($value);
}
protected static function decodeValue($value) {
protected static function decodeValue(string $value): mixed {
return is_numeric($value) ? (int) $value : json_decode($value, true);
}
}

4
tests/lib/Memcache/RedisTest.php

@ -9,8 +9,6 @@
namespace Test\Memcache;
use const OC\Memcache\LUA_SCRIPTS;
/**
* @group Memcache
* @group Redis
@ -60,7 +58,7 @@ class RedisTest extends Cache {
}
public function testScriptHashes() {
foreach (LUA_SCRIPTS as $script) {
foreach (\OC\Memcache\Redis::LUA_SCRIPTS as $script) {
$this->assertEquals(sha1($script[0]), $script[1]);
}
}

Loading…
Cancel
Save