1 | <?php |
||
24 | class RedisCache extends AbstractCache |
||
25 | { |
||
26 | /** |
||
27 | * Redis object instance |
||
28 | * |
||
29 | * @var object Redis object |
||
30 | */ |
||
31 | private $_redis; |
||
32 | |||
33 | /** |
||
34 | * Instantiates the `Redis` object and connects it to the configured server. |
||
35 | * |
||
36 | * @return void |
||
|
|||
37 | */ |
||
38 | function __construct() |
||
39 | { |
||
40 | global $redisServer, $redisPort; |
||
41 | |||
42 | if (!$this->_redis) { |
||
43 | $this->_redis = new Redis(); |
||
44 | } |
||
45 | |||
46 | if (substr($redisServer, 0, 7) == "unix://") { |
||
47 | $this->_redis->pconnect(substr($redisServer, 7), 0.0, null, 0); |
||
48 | } else { |
||
49 | $this->_redis->pconnect($redisServer, $redisPort, 0.0, null, 0); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Sets expiration time for cache key |
||
55 | * |
||
56 | * @param string $key The key to uniquely identify the cached item |
||
57 | * @param mixed $timeout A `strtotime()`-compatible string or a Unix timestamp. |
||
58 | * @return boolean |
||
59 | */ |
||
60 | protected function _expireAt($key, $timeout) |
||
64 | |||
65 | /** |
||
66 | * Read value from the cache |
||
67 | * |
||
68 | * @param string $key The key to uniquely identify the cached item |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function get($key) |
||
75 | |||
76 | /** |
||
77 | * Write value to the cache |
||
78 | * |
||
79 | * @param string $key The key to uniquely identify the cached item |
||
80 | * @param mixed $value The value to be cached |
||
81 | * @param null|string $timeout A strtotime() compatible cache time. |
||
82 | * @return boolean |
||
83 | */ |
||
84 | public function set($key, $value, $timeout) |
||
85 | { |
||
86 | $result = $this->_redis->set($key, $value); |
||
87 | return $result ? $this->_expireAt($key, $timeout) : $result; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Override value in the cache |
||
92 | * |
||
93 | * @param string $key The key to uniquely identify the cached item |
||
94 | * @param mixed $value The value to be cached |
||
95 | * @param null|string $timeout A strtotime() compatible cache time. |
||
96 | * @return boolean |
||
97 | */ |
||
98 | public function replace($key, $value, $timeout) |
||
102 | |||
103 | /** |
||
104 | * Delete value from the cache |
||
105 | * |
||
106 | * @param string $key The key to uniquely identify the cached item |
||
107 | */ |
||
108 | public function delete($key) |
||
112 | |||
113 | /** |
||
114 | * Performs an atomic increment operation on specified numeric cache item. |
||
115 | * |
||
116 | * Note that if the value of the specified key is *not* an integer, the increment |
||
117 | * operation will have no effect whatsoever. Redis chooses to not typecast values |
||
118 | * to integers when performing an atomic increment operation. |
||
119 | * |
||
120 | * @param string $key Key of numeric cache item to increment |
||
121 | * @return Closure Function returning item's new value on successful increment, else `false` |
||
122 | */ |
||
123 | public function increment($key, $step = 1, $timeout = 0) |
||
124 | { |
||
125 | if ($timeout) { |
||
126 | $this->_expireAt($key, $timeout); |
||
127 | } |
||
128 | return $this->_redis->incr($key, $step); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Performs an atomic decrement operation on specified numeric cache item. |
||
133 | * |
||
134 | * Note that if the value of the specified key is *not* an integer, the decrement |
||
135 | * operation will have no effect whatsoever. Redis chooses to not typecast values |
||
136 | * to integers when performing an atomic decrement operation. |
||
137 | * |
||
138 | * @param string $key Key of numeric cache item to decrement |
||
139 | * @param integer $step Offset to decrement - defaults to 1 |
||
140 | * @param integer $timeout A strtotime() compatible cache time. |
||
141 | * @return Closure Function returning item's new value on successful decrement, else `false` |
||
142 | */ |
||
143 | public function decrement($key, $step = 1, $timeout = 0) |
||
144 | { |
||
145 | if ($timeout) { |
||
146 | $this->_expireAt($key, $timeout); |
||
147 | } |
||
148 | return $this->_redis->decr($key, $step); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Clears user-space cache |
||
153 | * |
||
154 | * @return boolean|null |
||
155 | */ |
||
156 | public function flush() |
||
160 | |||
161 | } |
||
162 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.