|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Soupmix\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Memcached; |
|
6
|
|
|
|
|
7
|
|
|
class MemcachedCache implements CacheInterface |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
public $handler = null; |
|
12
|
|
|
/** |
|
13
|
|
|
* Connect to Memcached service |
|
14
|
|
|
* |
|
15
|
|
|
* @param Memcached $handler Memcached handler object |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
4 |
|
public function __construct(Memcached $handler) |
|
19
|
|
|
{ |
|
20
|
4 |
|
$this->handler = $handler; |
|
21
|
4 |
|
if(Memcached::HAVE_IGBINARY){ |
|
22
|
|
|
ini_set("memcached.serializer", "igbinary"); |
|
23
|
|
|
} |
|
24
|
4 |
|
} |
|
25
|
|
|
/** |
|
26
|
|
|
* Fetch a value from the cache. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $key The unique key of this item in the cache |
|
29
|
|
|
* |
|
30
|
|
|
* @return mixed The value of the item from the cache, or null in case of cache miss |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function get($key) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$value = $this->handler->get($key); |
|
35
|
1 |
|
return ($value) ? $value : null; |
|
36
|
|
|
} |
|
37
|
|
|
/** |
|
38
|
|
|
* Persist data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $key The key of the item to store |
|
41
|
|
|
* @param mixed $value The value of the item to store |
|
42
|
|
|
* @param null|integer|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and the driver supports TTL |
|
43
|
|
|
* then the library may set a default value for it or let the driver take care of that. |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool True on success and false on failure |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function set($key, $value, $ttl = null){ |
|
48
|
2 |
|
return $this->handler->set($key, $value, intval($ttl)); |
|
49
|
|
|
} |
|
50
|
|
|
/** |
|
51
|
|
|
* Delete an item from the cache by its unique key |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $key The unique cache key of the item to delete |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool True on success and false on failure |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function delete($key){ |
|
58
|
1 |
|
return (bool) $this->handler->delete($key); |
|
59
|
|
|
} |
|
60
|
|
|
/** |
|
61
|
|
|
* Wipe clean the entire cache's keys |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool True on success and false on failure |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function clear(){ |
|
66
|
4 |
|
return $this->handler->flush(); |
|
67
|
|
|
} |
|
68
|
|
|
/** |
|
69
|
|
|
* Obtain multiple cache items by their unique keys |
|
70
|
|
|
* |
|
71
|
|
|
* @param array|Traversable $keys A list of keys that can obtained in a single operation. |
|
72
|
|
|
* |
|
73
|
|
|
* @return array An array of key => value pairs. Cache keys that do not exist or are stale will have a value of null. |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getMultiple($keys) |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->handler->getMulti($keys); |
|
78
|
|
|
} |
|
79
|
|
|
/** |
|
80
|
|
|
* Persisting a set of key => value pairs in the cache, with an optional TTL. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array|Traversable $items An array of key => value pairs for a multiple-set operation. |
|
83
|
|
|
* @param null|integer|DateInterval $ttl Optional. The amount of seconds from the current time that the item will exist in the cache for. |
|
84
|
|
|
* If this is null then the cache backend will fall back to its own default behaviour. |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool True on success and false on failure |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function setMultiple($items, $ttl = null) |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $this->handler->setMulti($items, intval($ttl)); |
|
91
|
|
|
} |
|
92
|
|
|
/** |
|
93
|
|
|
* Delete multiple cache items in a single operation |
|
94
|
|
|
* |
|
95
|
|
|
* @param array|Traversable $keys The array of string-based keys to be deleted |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool True on success and false on failure |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function deleteMultiple($keys) |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->handler->deleteMulti($keys); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
/** |
|
104
|
|
|
* Increment a value atomically in the cache by its step value, which defaults to 1 |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $key The cache item key |
|
107
|
|
|
* @param integer $step The value to increment by, defaulting to 1 |
|
108
|
|
|
* |
|
109
|
|
|
* @return int|bool The new value on success and false on failure |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function increment($key, $step = 1) |
|
112
|
|
|
{ |
|
113
|
1 |
|
return $this->handler->increment($key, $step); |
|
114
|
|
|
} |
|
115
|
|
|
/** |
|
116
|
|
|
* Decrement a value atomically in the cache by its step value, which defaults to 1 |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $key The cache item key |
|
119
|
|
|
* @param integer $step The value to decrement by, defaulting to 1 |
|
120
|
|
|
* |
|
121
|
|
|
* @return int|bool The new value on success and false on failure |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function decrement($key, $step = 1) |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->handler->decrement($key, $step); |
|
126
|
|
|
} |
|
127
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.