Memcached::deleteItem()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.8449

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 6
cts 11
cp 0.5455
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.8449
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
 * @deprecated  The joomla/cache package is deprecated
22
 */
23
class Memcached extends AbstractCacheItemPool
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Cache\AbstractCacheItemPool has been deprecated with message: The joomla/cache package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
24
{
25
	/**
26
	 * The Memcached driver
27
	 *
28
	 * @var    \Memcached
29
	 * @since  1.0
30
	 */
31
	protected $driver;
32
33
	/**
34
	 * Constructor.
35
	 *
36
	 * @param   \Memcached          $memcached  The Memcached driver being used for this pool
37
	 * @param   array|\ArrayAccess  $options    An options array, or an object that implements \ArrayAccess
38
	 *
39
	 * @since   1.0
40
	 */
41 19
	public function __construct(\Memcached $memcached, $options = [])
42
	{
43
		// Parent sets up the caching options and checks their type
44 19
		parent::__construct($options);
45
46 19
		$this->driver = $memcached;
47 19
	}
48
49
	/**
50
	 * This will wipe out the entire cache's keys
51
	 *
52
	 * @return  boolean  True if the pool was successfully cleared. False if there was an error.
53
	 *
54
	 * @since   1.0
55
	 */
56 19
	public function clear()
57
	{
58 19
		return $this->driver->flush();
59
	}
60
61
	/**
62
	 * Returns a Cache Item representing the specified key.
63
	 *
64
	 * @param   string  $key  The key for which to return the corresponding Cache Item.
65
	 *
66
	 * @return  CacheItemInterface  The corresponding Cache Item.
67
	 *
68
	 * @since   __DEPLOY_VERSION__
69
	 */
70 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...
71
	{
72 9
		$value = $this->driver->get($key);
73 9
		$code = $this->driver->getResultCode();
74 9
		$item = new Item($key);
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Cache\Item\Item has been deprecated with message: The joomla/cache package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
75
76 9
		if ($code === \Memcached::RES_SUCCESS)
77
		{
78 7
			$item->set($value);
79
		}
80
81 9
		return $item;
82
	}
83
84
	/**
85
	 * Returns a traversable set of cache items.
86
	 *
87
	 * @param   string[]  $keys  An indexed array of keys of items to retrieve.
88
	 *
89
	 * @return  array  A traversable collection of Cache Items keyed by the cache keys of each item.
90
	 *                 A Cache item will be returned for each key, even if that key is not found.
91
	 *
92
	 * @since   __DEPLOY_VERSION__
93
	 */
94 4
	public function getItems(array $keys = [])
95
	{
96 4
		$data = $this->driver->getMulti($keys);
97
98 4
		$result = [];
99
100 4
		foreach ($keys as $key)
101
		{
102 4
			$item = new Item($key);
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Cache\Item\Item has been deprecated with message: The joomla/cache package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
103
104
			// On some platforms $data may be a boolean false
105 4
			if (is_array($data) && array_key_exists($key, $data))
106
			{
107 2
				$item->set($data[$key]);
108
			}
109
110 4
			$result[$key] = $item;
111
		}
112
113 4
		return $result;
114
	}
115
116
	/**
117
	 * Removes the item from the pool.
118
	 *
119
	 * @param   string  $key  The key to delete.
120
	 *
121
	 * @return  boolean  True if the item was successfully removed. False if there was an error.
122
	 *
123
	 * @since   __DEPLOY_VERSION__
124
	 * @throws  RuntimeException
125
	 */
126 2
	public function deleteItem($key)
127
	{
128 2
		if ($this->hasItem($key))
129
		{
130 2
			$this->driver->delete($key);
131
132 2
			$rc = $this->driver->getResultCode();
133
134
			// If the item was not successfully removed nor did not exist then raise an error
135 2
			if (($rc !== \Memcached::RES_SUCCESS))
136
			{
137
				throw new RuntimeException(
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Cache\Exception\RuntimeException has been deprecated with message: The joomla/cache package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
138
					sprintf(
139
						'Unable to remove cache entry for %s. Error message `%s`.',
140
						$key,
141
						$this->driver->getResultMessage()
142
					)
143
				);
144
			}
145
		}
146
147 2
		return true;
148
	}
149
150
	/**
151
	 * Removes multiple items from the pool.
152
	 *
153
	 * @param   array  $keys  An array of keys that should be removed from the pool.
154
	 *
155
	 * @return  boolean
156
	 *
157
	 * @since   __DEPLOY_VERSION__
158
	 */
159 2
	public function deleteItems(array $keys)
160
	{
161
		// HHVM doesn't support deleteMulti
162 2
		if (!method_exists($this->driver, 'deleteMulti'))
163
		{
164
			return parent::deleteItems($keys);
165
		}
166
167 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...
168
169 2
		foreach ($deleted as $key => $value)
170
		{
171
			/*
172
			 * The return of deleteMulti is not consistent with the documentation for error cases,
173
			 * so check for an explicit boolean true for successful deletion
174
			 */
175 2
			if ($value !== true && $value !== \Memcached::RES_NOTFOUND)
176
			{
177 2
				return false;
178
			}
179
		}
180
181 2
		return true;
182
	}
183
184
	/**
185
	 * Persists a cache item immediately.
186
	 *
187
	 * @param   CacheItemInterface  $item  The cache item to save.
188
	 *
189
	 * @return  boolean  True if the item was successfully persisted. False if there was an error.
190
	 *
191
	 * @since   __DEPLOY_VERSION__
192
	 */
193 16
	public function save(CacheItemInterface $item)
194
	{
195 16
		if ($item instanceof HasExpirationDateInterface)
196
		{
197 8
			$ttl = $this->convertItemExpiryToSeconds($item);
198
		}
199
		else
200
		{
201 8
			$ttl = 0;
202
		}
203
204 16
		$this->driver->set($item->getKey(), $item->get(), $ttl);
205
206 16
		return $this->driver->getResultCode() === \Memcached::RES_SUCCESS;
207
	}
208
209
	/**
210
	 * Confirms if the cache contains specified cache item.
211
	 *
212
	 * @param   string  $key  The key for which to check existence.
213
	 *
214
	 * @return  boolean  True if item exists in the cache, false otherwise.
215
	 *
216
	 * @since   1.0
217
	 */
218 6
	public function hasItem($key)
219
	{
220 6
		$this->driver->get($key);
221
222 6
		return $this->driver->getResultCode() !== \Memcached::RES_NOTFOUND;
223
	}
224
225
	/**
226
	 * Test to see if the CacheItemPoolInterface is available
227
	 *
228
	 * @return  boolean  True on success, false otherwise
229
	 *
230
	 * @since   __DEPLOY_VERSION__
231
	 */
232 19
	public static function isSupported(): bool
233
	{
234
		/*
235
		 * GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded.
236
		 * If the class is there, we can assume it works.
237
		 */
238 19
		return (class_exists('Memcached'));
239
	}
240
}
241