|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Journey\Cache\Adapters; |
|
4
|
|
|
|
|
5
|
|
|
use Journey\Cache\CacheAdapterInterface; |
|
6
|
|
|
use Memcached; |
|
7
|
|
|
|
|
8
|
|
|
class MemcachedAdapter implements CacheAdapterInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Instance of memcached. |
|
12
|
|
|
* |
|
13
|
|
|
* @var \Memcached |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $instance; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Initialize a new localcache. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $filepath |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
6 |
|
public function __construct(array $config) |
|
23
|
|
|
{ |
|
24
|
6 |
|
$this->config = $config; |
|
|
|
|
|
|
25
|
6 |
|
$this->instance = new Memcached(); |
|
26
|
6 |
|
if ($config['servers']) { |
|
27
|
6 |
|
foreach ($config['servers'] as $server) { |
|
28
|
6 |
|
$this->instance->addServer($server['host'], $server['port']); |
|
29
|
6 |
|
} |
|
30
|
6 |
|
} |
|
31
|
6 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Must implement a set method. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $key key to set as the cache value. |
|
37
|
|
|
* @param mixed $value returns the value of the cached item. |
|
38
|
|
|
* @param integer $expiration when the cache should expire (unix timestamp) |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
4 |
|
public function set($key, $value, $expiration = 0) |
|
42
|
|
|
{ |
|
43
|
4 |
|
$key = $this->createKey($key); |
|
44
|
4 |
|
$this->instance->set($key, $value, $expiration); |
|
45
|
4 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Must implement a get method. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $key Get a cached item by key. |
|
52
|
|
|
* @return mixed Returns cached item or false. |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function get($key) |
|
55
|
|
|
{ |
|
56
|
4 |
|
$key = $this->createKey($key); |
|
57
|
4 |
|
return $this->instance->get($key); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Must implement a delete method. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $key delete a specific cached item by key. |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function delete($key) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->instance->delete($this->createKey($key)); |
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Clear all of the values set by this cache instance. |
|
74
|
|
|
* |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
6 |
|
public function clear() |
|
78
|
|
|
{ |
|
79
|
6 |
|
$this->instance->delete('memcached_adapter_namespace'); |
|
80
|
6 |
|
$this->key(true); |
|
81
|
6 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Return the Memcached instance. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Memcached |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getInstance() |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->instance; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get the value of a namespace key. |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
6 |
|
public function key($reset = false) |
|
100
|
|
|
{ |
|
101
|
6 |
|
static $key; |
|
102
|
6 |
|
$key = $reset ? false : $key; |
|
103
|
6 |
|
if (!$key) { |
|
104
|
6 |
|
$key = $this->instance->get('memcached_adapter_namespace', function ($memc, $key, &$value) { |
|
105
|
6 |
|
$value = bin2hex(openssl_random_pseudo_bytes(8)); |
|
106
|
6 |
|
return true; |
|
107
|
6 |
|
}); |
|
108
|
6 |
|
} |
|
109
|
6 |
|
return $key; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Given a user provided key, return a namespaced key. |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
4 |
|
public function createKey($key) |
|
118
|
|
|
{ |
|
119
|
4 |
|
return $this->key() . "-" . $key; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.