Completed
Push — 2.0-dev ( 6772a9...05b98e )
by Michael
03:21
created

Memcached::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Adapter;
10
11
use Joomla\Cache\AbstractCacheItemPool;
12
use Joomla\Cache\Exception\RuntimeException;
13
use Joomla\Cache\Item\HasExpirationDateInterface;
14
use Joomla\Cache\Item\Item;
15
use Psr\Cache\CacheItemInterface;
16
17
/**
18
 * Memcached cache driver for the Joomla Framework.
19
 *
20
 * @since  1.0
21
 */
22
class Memcached extends AbstractCacheItemPool
23
{
24
	/**
25
	 * The Memcached driver
26
	 *
27
	 * @var    \Memcached
28
	 * @since  1.0
29
	 */
30
	protected $driver;
31
32
	/**
33
	 * Constructor.
34
	 *
35
	 * @param   \Memcached          $memcached  The Memcached driver being used for this pool
36
	 * @param   array|\ArrayAccess  $options    An options array, or an object that implements \ArrayAccess
37
	 *
38
	 * @since   1.0
39
	 */
40 19
	public function __construct(\Memcached $memcached, $options = [])
41
	{
42
		// Parent sets up the caching options and checks their type
43 19
		parent::__construct($options);
44
45 19
		$this->driver = $memcached;
46 19
	}
47
48
	/**
49
	 * This will wipe out the entire cache's keys
50
	 *
51
	 * @return  boolean  True if the pool was successfully cleared. False if there was an error.
52
	 *
53
	 * @since   1.0
54
	 */
55 19
	public function clear()
56
	{
57 19
		return $this->driver->flush();
58
	}
59
60
	/**
61
	 * Returns a Cache Item representing the specified key.
62
	 *
63
	 * @param   string  $key  The key for which to return the corresponding Cache Item.
64
	 *
65
	 * @return  CacheItemInterface  The corresponding Cache Item.
66
	 *
67
	 * @since   __DEPLOY_VERSION__
68
	 */
69 9 View Code Duplication
	public function getItem($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
	{
71 9
		$value = $this->driver->get($key);
72 9
		$code = $this->driver->getResultCode();
73 9
		$item = new Item($key);
74
75 9
		if ($code === \Memcached::RES_SUCCESS)
76
		{
77 7
			$item->set($value);
78
		}
79
80 9
		return $item;
81
	}
82
83
	/**
84
	 * Returns a traversable set of cache items.
85
	 *
86
	 * @param   string[]  $keys  An indexed array of keys of items to retrieve.
87
	 *
88
	 * @return  array  A traversable collection of Cache Items keyed by the cache keys of each item.
89
	 *                 A Cache item will be returned for each key, even if that key is not found.
90
	 *
91
	 * @since   __DEPLOY_VERSION__
92
	 */
93 4
	public function getItems(array $keys = [])
94
	{
95 4
		$data = $this->driver->getMulti($keys);
96
97 4
		$result = [];
98
99 4
		foreach ($keys as $key)
100
		{
101 4
			$item = new Item($key);
102
103
			// On some platforms $data may be a boolean false
104 4
			if (is_array($data) && array_key_exists($key, $data))
105
			{
106 2
				$item->set($data[$key]);
107
			}
108
109 4
			$result[$key] = $item;
110
		}
111
112 4
		return $result;
113
	}
114
115
	/**
116
	 * Removes the item from the pool.
117
	 *
118
	 * @param   string  $key  The key to delete.
119
	 *
120
	 * @return  boolean  True if the item was successfully removed. False if there was an error.
121
	 *
122
	 * @since   __DEPLOY_VERSION__
123
	 * @throws  RuntimeException
124
	 */
125 2
	public function deleteItem($key)
126
	{
127 2
		if ($this->hasItem($key))
128
		{
129 2
			$this->driver->delete($key);
130
131 2
			$rc = $this->driver->getResultCode();
132
133
			// If the item was not successfully removed nor did not exist then raise an error
134 2
			if (($rc !== \Memcached::RES_SUCCESS))
135
			{
136
				throw new RuntimeException(
137
					sprintf(
138
						'Unable to remove cache entry for %s. Error message `%s`.',
139
						$key,
140
						$this->driver->getResultMessage()
141
					)
142
				);
143
			}
144
		}
145
146 2
		return true;
147
	}
148
149
	/**
150
	 * Removes multiple items from the pool.
151
	 *
152
	 * @param   array  $keys  An array of keys that should be removed from the pool.
153
	 *
154
	 * @return  boolean
155
	 *
156
	 * @since   __DEPLOY_VERSION__
157
	 */
158 2
	public function deleteItems(array $keys)
159
	{
160
		// HHVM doesn't support deleteMulti
161 2
		if (!method_exists($this->driver, 'deleteMulti'))
162
		{
163
			return parent::deleteItems($keys);
164
		}
165
166 2
		$deleted = $this->driver->deleteMulti($keys);
0 ignored issues
show
Bug introduced by
The method deleteMulti() does not exist on Memcached. Did you maybe mean delete()?

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.

Loading history...
167
168 2
		foreach ($deleted as $key => $value)
169
		{
170
			/*
171
			 * The return of deleteMulti is not consistent with the documentation for error cases,
172
			 * so check for an explicit boolean true for successful deletion
173
			 */
174 2
			if ($value !== true && $value !== \Memcached::RES_NOTFOUND)
175
			{
176 2
				return false;
177
			}
178
		}
179
180 2
		return true;
181
	}
182
183
	/**
184
	 * Persists a cache item immediately.
185
	 *
186
	 * @param   CacheItemInterface  $item  The cache item to save.
187
	 *
188
	 * @return  boolean  True if the item was successfully persisted. False if there was an error.
189
	 *
190
	 * @since   __DEPLOY_VERSION__
191
	 */
192 16
	public function save(CacheItemInterface $item)
193
	{
194 16
		if ($item instanceof HasExpirationDateInterface)
195
		{
196 8
			$ttl = $this->convertItemExpiryToSeconds($item);
197
		}
198
		else
199
		{
200 8
			$ttl = 0;
201
		}
202
203 16
		$this->driver->set($item->getKey(), $item->get(), $ttl);
204
205 16
		return $this->driver->getResultCode() === \Memcached::RES_SUCCESS;
206
	}
207
208
	/**
209
	 * Confirms if the cache contains specified cache item.
210
	 *
211
	 * @param   string  $key  The key for which to check existence.
212
	 *
213
	 * @return  boolean  True if item exists in the cache, false otherwise.
214
	 *
215
	 * @since   1.0
216
	 */
217 6
	public function hasItem($key)
218
	{
219 6
		$this->driver->get($key);
220
221 6
		return $this->driver->getResultCode() !== \Memcached::RES_NOTFOUND;
222
	}
223
224
	/**
225
	 * Test to see if the CacheItemPoolInterface is available
226
	 *
227
	 * @return  boolean  True on success, false otherwise
228
	 *
229
	 * @since   __DEPLOY_VERSION__
230
	 */
231 19
	public static function isSupported(): bool
232
	{
233
		/*
234
		 * GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded.
235
		 * If the class is there, we can assume it works.
236
		 */
237 19
		return (class_exists('Memcached'));
238
	}
239
}
240