1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
ar_pinp::allow('ar_cache_redisStore'); |
4
|
|
|
|
5
|
|
|
class ar_cache_redisStore implements ar_cacheStoreInterface, arKeyValueStoreInterface { |
6
|
|
|
private $timeout = 7200; |
7
|
|
|
private $redis = null; |
8
|
|
|
|
9
|
|
|
public function __construct( $options, $timeout = 7200, $prefix = 'AR_') { |
10
|
|
|
|
11
|
|
|
if ( is_string($timeout) ) { |
12
|
|
|
$timeout = strtotime( $timeout, 0); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
$this->timeout = $timeout; |
16
|
|
|
$this->prefix = $prefix; |
|
|
|
|
17
|
|
|
$this->redis = $redis = new Redis(); |
18
|
|
|
|
19
|
|
|
if (isset($options['port'])) { |
20
|
|
|
$redis->pconnect($options['server'],$options['port']); |
21
|
|
|
} else { |
22
|
|
|
$redis->pconnect($options['server']); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if (isset($options['auth'])) { |
26
|
|
|
$redis->auth($options['auth']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (isset($options['db'])) { |
30
|
|
|
$redis->select($options['db']); |
31
|
|
|
} |
32
|
|
|
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); |
33
|
|
|
$redis->setOption(Redis::OPT_PREFIX, $prefix); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// key |
37
|
|
|
public function get( $path ) { |
38
|
|
|
$res = $this->redis->exists($path); |
39
|
|
|
if($res === true ) { |
40
|
|
|
return $this->redis->get($path); |
41
|
|
|
} |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getIfFresh( $path, $timeout = 0 ) { |
46
|
|
|
$ttl = $this->redis->ttl($path); |
47
|
|
|
if($ttl > 0 || $ttl === -1 ){ |
48
|
|
|
return $this->redis->get($path); |
49
|
|
|
} |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// key value expire |
54
|
|
|
public function set( $path, $value, $timeout = 7200 ) { |
55
|
|
|
$res = $this->redis->setEx($path, $timeout, $value); |
56
|
|
|
return $res; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// meta info |
60
|
|
|
public function info( $path ) { |
61
|
|
|
$res = [ |
62
|
|
|
'size' => null, |
63
|
|
|
'timeout' => $this->redis->ttl( $path ), |
64
|
|
|
'ctime' => null, |
65
|
|
|
]; |
66
|
|
|
$res['fresh'] = (bool)$res['timeout']; |
67
|
|
|
return $res; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// remove key |
71
|
|
|
public function clear( $path = null ) { |
72
|
|
|
return $this->redis->delete($path); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function subStore( $path ) { |
76
|
|
|
// geen idee eigenlijk |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// hoeveel tijd hebben we nog |
80
|
|
|
public function isFresh( $path ) { |
81
|
|
|
return (bool)$this->redis->ttl($path); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// purge, we do not have the option to partial purge, so purge all |
85
|
|
|
public function purge($name=null){ |
86
|
|
|
return $this->redis->delete( $this->redis->keys($name .'*')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function putvar( $name, $value ) { |
90
|
|
|
return $this->set( $name, $value ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getvar( $name ) { |
94
|
|
|
return $this->get( $name ); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: