1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Memcached.php |
4
|
|
|
* |
5
|
|
|
* @category AngryBytes |
6
|
|
|
* @package Cache |
7
|
|
|
* @subpackage Adapter |
8
|
|
|
* @copyright Copyright (c) 2010 Angry Bytes BV (http://www.angrybytes.com) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace AngryBytes\Cache\Adapter; |
12
|
|
|
|
13
|
|
|
use AngryBytes\Cache\Adapter; |
14
|
|
|
use AngryBytes\Cache\ResultNotFound; |
15
|
|
|
|
16
|
|
|
use AngryBytes\Cache\Adapter\Memcached\AdapterInterface as MemcachedAdapterInterface; |
17
|
|
|
use AngryBytes\Cache\Adapter\Memcached\Adapter as MemcachedAdapter; |
18
|
|
|
|
19
|
|
|
use \Exception as Exception; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Memcached |
24
|
|
|
* |
25
|
|
|
* Simple memcached adapter for AngryBytes\Cache. |
26
|
|
|
* |
27
|
|
|
* Does not use any of the more advanced functions such as CAS and deferred |
28
|
|
|
* loading. Allows multiple servers to be added through addServer(). |
29
|
|
|
* |
30
|
|
|
* @category AngryBytes |
31
|
|
|
* @package Cache |
32
|
|
|
* @subpackage Adapter |
33
|
|
|
*/ |
34
|
|
|
class Memcached extends Adapter |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Memcached adapter |
38
|
|
|
* |
39
|
|
|
* @var MemcachedAdapterInterface |
40
|
|
|
**/ |
41
|
|
|
private $memcached; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param mixed $persistentId By default the Memcached instances are destroyed at the end of the request. |
47
|
|
|
* To create an instance that persists between requests, use $persistentId to |
48
|
|
|
* specify a unique ID for the instance. All instances created with the same |
49
|
|
|
* $persistentId will share the same connection. |
50
|
|
|
* @return void |
|
|
|
|
51
|
|
|
**/ |
52
|
|
|
public function __construct($persistentId = null) |
53
|
|
|
{ |
54
|
|
|
// Init the adapter |
55
|
|
|
$this->setMemcached( |
56
|
|
|
new MemcachedAdapter($persistentId) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the memcached adapter |
62
|
|
|
* |
63
|
|
|
* @return MemcachedAdapterInterface |
64
|
|
|
*/ |
65
|
|
|
public function getMemcached() |
66
|
|
|
{ |
67
|
|
|
return $this->memcached; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the memcached adapter |
72
|
|
|
* |
73
|
|
|
* @param MemcachedAdapterInterface $memcached |
74
|
|
|
* @return Memcached |
75
|
|
|
*/ |
76
|
|
|
public function setMemcached(MemcachedAdapterInterface $memcached) |
77
|
|
|
{ |
78
|
|
|
$this->memcached = $memcached; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add a server |
85
|
|
|
* |
86
|
|
|
* @param string $host |
87
|
|
|
* @param int $port |
88
|
|
|
* @param int $weight |
89
|
|
|
* @return Memcached |
90
|
|
|
**/ |
91
|
|
|
public function addServer($host, $port, $weight = 0) |
92
|
|
|
{ |
93
|
|
|
$this->getMemcached()->addServer($host, $port, $weight); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Save some data |
100
|
|
|
* |
101
|
|
|
* @param string $data |
102
|
|
|
* @param string $id |
103
|
|
|
* @param int $lifeTime lifetime in seconds |
104
|
|
|
* @return bool |
105
|
|
|
**/ |
106
|
|
|
public function save($data, $id, $lifeTime) |
107
|
|
|
{ |
108
|
|
|
return $this->getMemcached()->set($id, $data, $lifeTime); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Load an item from the cache |
113
|
|
|
* |
114
|
|
|
* @param string $id |
115
|
|
|
* @return mixed|ResultNotFound |
116
|
|
|
**/ |
117
|
|
|
public function load($id) |
118
|
|
|
{ |
119
|
|
|
$result = $this->getMemcached()->get($id); |
120
|
|
|
|
121
|
|
|
if ($this->getMemcached()->getResultCode() !== MemcachedAdapter::RES_SUCCESS) { |
122
|
|
|
return new ResultNotFound($id); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Delete an item from the cache |
130
|
|
|
* |
131
|
|
|
* @param string $id |
132
|
|
|
* @return bool |
133
|
|
|
**/ |
134
|
|
|
public function delete($id) |
135
|
|
|
{ |
136
|
|
|
return $this->getMemcached()->delete($id); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
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.