1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Cache Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Cache; |
10
|
|
|
|
11
|
|
|
use Joomla\Cache\Item\HasExpirationDateInterface; |
12
|
|
|
use Psr\Cache\CacheItemInterface; |
13
|
|
|
use Joomla\Cache\Exception\RuntimeException; |
14
|
|
|
use Joomla\Cache\Item\Item; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Memcached cache driver for the Joomla Framework. |
18
|
|
|
* |
19
|
|
|
* @since 1.0 |
20
|
|
|
*/ |
21
|
|
|
class Memcached extends Cache |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The Memcached driver |
25
|
|
|
* |
26
|
|
|
* @var \Memcached |
27
|
|
|
* @since 1.0 |
28
|
|
|
*/ |
29
|
|
|
private $driver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param \Memcached $memcached The Memcached driver being used for this pool |
35
|
|
|
* @param array|\ArrayAccess $options An options array, or an object that implements \ArrayAccess |
36
|
|
|
* |
37
|
|
|
* @since 1.0 |
38
|
|
|
* @throws \RuntimeException |
39
|
|
|
*/ |
40
|
10 |
|
public function __construct(\Memcached $memcached, $options = []) |
41
|
|
|
{ |
42
|
|
|
// Parent sets up the caching options and checks their type |
43
|
10 |
|
parent::__construct($options); |
44
|
|
|
|
45
|
10 |
|
$this->driver = $memcached; |
46
|
10 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This will wipe out the entire cache's keys |
50
|
|
|
* |
51
|
|
|
* @return boolean The result of the clear operation. |
52
|
|
|
* |
53
|
|
|
* @since 1.0 |
54
|
|
|
*/ |
55
|
10 |
|
public function clear() |
56
|
|
|
{ |
57
|
10 |
|
return $this->driver->flush(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Method to get a storage entry value from a key. |
62
|
|
|
* |
63
|
|
|
* @param string $key The storage entry identifier. |
64
|
|
|
* |
65
|
|
|
* @return CacheItemInterface |
66
|
|
|
* |
67
|
|
|
* @since 1.0 |
68
|
|
|
*/ |
69
|
5 |
|
public function getItem($key) |
70
|
|
|
{ |
71
|
5 |
|
$value = $this->driver->get($key); |
72
|
5 |
|
$code = $this->driver->getResultCode(); |
73
|
5 |
|
$item = new Item($key); |
74
|
|
|
|
75
|
5 |
|
if ($code === \Memcached::RES_SUCCESS) |
76
|
5 |
|
{ |
77
|
4 |
|
$item->set($value); |
78
|
4 |
|
} |
79
|
|
|
|
80
|
5 |
|
return $item; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Method to remove a storage entry for a key. |
85
|
|
|
* |
86
|
|
|
* @param string $key The storage entry identifier. |
87
|
|
|
* |
88
|
|
|
* @return boolean |
89
|
|
|
* |
90
|
|
|
* @since 1.0 |
91
|
|
|
*/ |
92
|
2 |
|
public function deleteItem($key) |
93
|
|
|
{ |
94
|
2 |
|
if ($this->hasItem($key)) |
95
|
2 |
|
{ |
96
|
2 |
|
$this->driver->delete($key); |
97
|
|
|
|
98
|
2 |
|
$rc = $this->driver->getResultCode(); |
99
|
|
|
|
100
|
2 |
|
if ( ($rc != \Memcached::RES_SUCCESS)) |
101
|
2 |
|
{ |
102
|
|
|
throw new RuntimeException(sprintf('Unable to remove cache entry for %s. Error message `%s`.', $key, $this->driver->getResultMessage())); |
103
|
|
|
} |
104
|
2 |
|
} |
105
|
|
|
|
106
|
2 |
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Persists a cache item immediately. |
111
|
|
|
* |
112
|
|
|
* @param CacheItemInterface $item The cache item to save. |
113
|
|
|
* |
114
|
|
|
* @return static The invoked object. |
115
|
|
|
*/ |
116
|
8 |
|
public function save(CacheItemInterface $item) |
117
|
|
|
{ |
118
|
8 |
|
if ($item instanceof HasExpirationDateInterface) |
119
|
8 |
|
{ |
120
|
1 |
|
$ttl = $this->convertItemExpiryToSeconds($item); |
121
|
1 |
|
} |
122
|
|
|
else |
123
|
|
|
{ |
124
|
7 |
|
$ttl = 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
8 |
|
$this->driver->set($item->getKey(), $item->get(), $ttl); |
128
|
|
|
|
129
|
8 |
|
return (bool) ($this->driver->getResultCode() == \Memcached::RES_SUCCESS); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Method to determine whether a storage entry has been set for a key. |
134
|
|
|
* |
135
|
|
|
* @param string $key The storage entry identifier. |
136
|
|
|
* |
137
|
|
|
* @return boolean |
138
|
|
|
* |
139
|
|
|
* @since 1.0 |
140
|
|
|
*/ |
141
|
4 |
|
public function hasItem($key) |
142
|
|
|
{ |
143
|
4 |
|
$this->driver->get($key); |
144
|
|
|
|
145
|
4 |
|
return ($this->driver->getResultCode() != \Memcached::RES_NOTFOUND); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Test to see if the CacheItemPoolInterface is available |
150
|
|
|
* |
151
|
|
|
* @return boolean True on success, false otherwise |
152
|
|
|
* |
153
|
|
|
* @since __DEPLOY_VERSION__ |
154
|
|
|
*/ |
155
|
10 |
|
public static function isSupported() |
156
|
|
|
{ |
157
|
|
|
/* |
158
|
|
|
* GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded. |
159
|
|
|
* If the class is there, we can assume it works. |
160
|
|
|
*/ |
161
|
10 |
|
return (class_exists('Memcached')); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|