1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf\Cache\Storages; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Redis usage |
13
|
|
|
* |
14
|
|
|
* <code> |
15
|
|
|
* // Put an item in the cache for 15 minutes |
16
|
|
|
* Cache::put('name', 'Robin', 15); |
17
|
|
|
* </code> |
18
|
|
|
* |
19
|
|
|
* @package Cache_Storages |
20
|
|
|
* @author Gjero Krsteski <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Redis extends Storage |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The Redis database instance. |
26
|
|
|
* |
27
|
|
|
* @var \Pimf\Redis |
28
|
|
|
*/ |
29
|
|
|
protected $redis; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Pimf\Redis $redis |
33
|
|
|
*/ |
34
|
|
|
public function __construct(\Pimf\Redis $redis) |
35
|
|
|
{ |
36
|
|
|
$this->redis = $redis; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Determine if an item exists in the cache. |
41
|
|
|
* |
42
|
|
|
* @param string $key |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function has($key) |
47
|
|
|
{ |
48
|
|
|
return ($this->redis->get($key) !== null); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Retrieve an item from the cache storage. |
53
|
|
|
* |
54
|
|
|
* @param string $key |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
protected function retrieve($key) |
59
|
|
|
{ |
60
|
|
|
if (!is_null($cache = $this->redis->get($key))) { |
61
|
|
|
return unserialize($cache); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Write an item to the cache for a given number of minutes. |
69
|
|
|
* |
70
|
|
|
* @param string $key |
71
|
|
|
* @param mixed $value |
72
|
|
|
* @param int $minutes |
73
|
|
|
*/ |
74
|
|
|
public function put($key, $value, $minutes) |
75
|
|
|
{ |
76
|
|
|
$this->forever($key, $value); |
77
|
|
|
$this->redis->expire($key, $minutes * 60); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Write an item to the cache that lasts forever. |
82
|
|
|
* |
83
|
|
|
* @param string $key |
84
|
|
|
* @param $value |
85
|
|
|
*/ |
86
|
|
|
public function forever($key, $value) |
87
|
|
|
{ |
88
|
|
|
$this->redis->set($key, serialize($value)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Delete an item from the cache. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
*/ |
96
|
|
|
public function forget($key) |
97
|
|
|
{ |
98
|
|
|
$this->redis->del($key); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|