-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCache.class.php
62 lines (56 loc) · 1.35 KB
/
Cache.class.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
55
56
57
58
59
60
61
62
<?php
require_once('util4p/util.php');
require_once('util4p/CRObject.class.php');
require_once('util4p/RedisDAO.class.php');
class Cache
{
private static $time_out = 300; // 5 min
private static $prefix = 'cache';
/* configuration && initialization */
public static function configure(CRObject $config)
{
self::$time_out = (int)$config->get('time_out', self::$time_out);
self::$prefix = $config->get('prefix', self::$prefix);
}
/**/
public static function put($key, $value)
{
$redis = RedisDAO::instance();
if ($redis === null) {
return false;
}
$redis_key = self::$prefix . ':' . $key;
$redis->set($redis_key, $value, 'EX', self::$time_out);
$redis->disconnect();
return true;
}
/**/
public static function get($key, $default = null)
{
$redis = RedisDAO::instance();
if ($redis === null) {
return $default;
}
$redis_key = self::$prefix . ':' . $key;
$value = $redis->get($redis_key);
if (!is_null($value)) { //hit
$redis->expire($redis_key, self::$time_out); // reset time out
} else { //miss
$value = $default;
}
$redis->disconnect();
return $value;
}
/* expire by key */
public static function expire($key)
{
$redis = RedisDAO::instance();
if ($redis === null) {
return false;
}
$redis_key = self::$prefix . ':' . $key;
$redis->del(array($redis_key));
$redis->disconnect();
return true;
}
}