1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Memcache Store. This store is deprecated. Use Memchached store instead. |
4
|
|
|
* |
5
|
|
|
* @package SugiPHP.Cache |
6
|
|
|
* @subpackage Cache |
7
|
|
|
* @author Plamen Popov <[email protected]> |
8
|
|
|
* @license http://opensource.org/licenses/mit-license.php (MIT License) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace SugiPHP\Cache; |
12
|
|
|
|
13
|
|
|
use Memcache; |
14
|
|
|
|
15
|
|
|
class MemcacheStore implements StoreInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Memcache instance |
19
|
|
|
*/ |
20
|
|
|
protected $memcache; |
21
|
|
|
protected $connected = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Creates a Memcache store |
25
|
|
|
* |
26
|
|
|
* @param Memcache $memcache |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Memcache $memcache) |
29
|
|
|
{ |
30
|
|
|
$this->memcache = $memcache; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates MemcacheStore instance. |
35
|
|
|
* |
36
|
|
|
* @param array $config Server Configurations |
37
|
|
|
* |
38
|
|
|
* @return MemcacheStore |
39
|
|
|
*/ |
40
|
|
|
public static function factory(array $config = array()) |
41
|
|
|
{ |
42
|
|
|
$memcache = new Memcache(); |
43
|
|
|
|
44
|
|
|
$host = empty($config["host"]) ? "127.0.0.1" : $config["host"]; |
45
|
|
|
$port = empty($config["port"]) ? 11211 : $config["port"]; |
46
|
|
|
|
47
|
|
|
$connected = $memcache->connect($host, $port); |
48
|
|
|
|
49
|
|
|
// The code using a store should work no matter if the store is running or not |
50
|
|
|
// Check is the memcache store is working with checkRunning() method |
51
|
|
|
$store = new MemcacheStore($memcache); |
52
|
|
|
$store->connected = $connected; |
53
|
|
|
|
54
|
|
|
return $store; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function add($key, $value, $ttl = 0) |
61
|
|
|
{ |
62
|
|
|
return $this->memcache->add($key, $value, 0, $ttl); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function set($key, $value, $ttl = 0) |
69
|
|
|
{ |
70
|
|
|
return $this->memcache->set($key, $value, 0, $ttl); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function get($key) |
77
|
|
|
{ |
78
|
|
|
$result = $this->memcache->get($key); |
79
|
|
|
|
80
|
|
|
return ($result === false) ? null : $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function has($key) |
87
|
|
|
{ |
88
|
|
|
return (!is_null($this->memcache->get($key))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function delete($key) |
95
|
|
|
{ |
96
|
|
|
$this->memcache->delete($key); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function flush() |
103
|
|
|
{ |
104
|
|
|
$this->memcache->flush(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Checks is the memcache server is running |
109
|
|
|
* |
110
|
|
|
* @return boolean |
111
|
|
|
*/ |
112
|
|
|
public function checkRunning() |
113
|
|
|
{ |
114
|
|
|
return $this->connected; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|