-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCacheHelper.php
54 lines (45 loc) · 1.12 KB
/
CacheHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace sitkoru\cache\ar;
use yii\redis\Connection;
/**
* Class CacheHelper
*
* @package common\components\cache
*/
class CacheHelper
{
/**
* @return Connection
*/
public static function getRedis()
{
return \Yii::$app->cache->redis;
}
public static function loadScript($path)
{
$script = file_get_contents($path);
return static::getRedis()->scriptLoad($script);
}
public static function scriptExists($sha)
{
$data = static::getRedis()->scriptExists($sha);
return reset($data);
}
public static function evalSHA($sha, $args, $numKeys)
{
return static::getRedis()->evalsha($sha, $numKeys, ...$args);
}
public static function get($key)
{
$res = static::getRedis()->get($key);
if ($res !== false && $res!==null) {
try {
return unserialize(zlib_decode($res));
} catch (\Throwable $ex) {
\Yii::error("Can't unserialize data for key {$key}: {$ex->getMessage()}");
}
return null;
}
return $res;
}
}