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
|
|
|
* @package Cache_Storages |
13
|
|
|
* @author Gjero Krsteski <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Memcached extends Storage |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The Memcache instance. |
19
|
|
|
* |
20
|
|
|
* @var \Memcached |
21
|
|
|
*/ |
22
|
|
|
public $memcache; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The cache key from the cache configuration file. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $key; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Memcached $memcache |
33
|
|
|
* @param $key |
34
|
|
|
*/ |
35
|
|
|
public function __construct(\Memcached $memcache, $key) |
36
|
|
|
{ |
37
|
|
|
$this->key = $key; |
38
|
|
|
$this->memcache = $memcache; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $key |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
protected function retrieve($key) |
47
|
|
|
{ |
48
|
|
|
if (($cache = $this->memcache->get($this->key . $key)) !== false) { |
49
|
|
|
return $cache; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Write an item to the cache for a given number of minutes. |
57
|
|
|
* |
58
|
|
|
* <code> |
59
|
|
|
* // Put an item in the cache for 15 minutes |
60
|
|
|
* Cache::put('name', 'Robin', 15); |
61
|
|
|
* </code> |
62
|
|
|
* |
63
|
|
|
* @param string $key |
64
|
|
|
* @param mixed $value |
65
|
|
|
* @param int $minutes |
66
|
|
|
* |
67
|
|
|
* @return bool|void |
68
|
|
|
*/ |
69
|
|
|
public function put($key, $value, $minutes) |
70
|
|
|
{ |
71
|
|
|
return $this->memcache->set($this->key . $key, $value, $minutes * 60); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Write an item to the cache that lasts forever. |
76
|
|
|
* |
77
|
|
|
* @param $key |
78
|
|
|
* @param $value |
79
|
|
|
* |
80
|
|
|
* @return bool|void |
81
|
|
|
*/ |
82
|
|
|
public function forever($key, $value) |
83
|
|
|
{ |
84
|
|
|
return $this->put($key, $value, 0); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Delete an item from the cache. |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* |
92
|
|
|
* @return bool|void |
93
|
|
|
*/ |
94
|
|
|
public function forget($key) |
95
|
|
|
{ |
96
|
|
|
return $this->memcache->delete($this->key . $key); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|