Completed
Push — 2.0-dev ( ccac7d...2a4498 )
by George
04:04
created

Memcached::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 2
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
	 * @var    \Memcached  The memcached driver, initialize to false to avoid having to repeated run isset before using it..
25
	 * @since  1.0
26
	 */
27
	private $driver = false;
28
29
	/**
30
	 * Constructor.
31
	 *
32
	 * @param   mixed  $options  An options array, or an object that implements \ArrayAccess
33
	 *
34
	 * @since   1.0
35
	 * @throws  \RuntimeException
36
	 */
37 3
	public function __construct($options = array())
38
	{
39
		// Parent sets up the caching options and checks their type
40 3
		parent::__construct($options);
41
42
		// These options must be set to something, so if not set make them false
43 3
		if (!isset($this->options['memcache.pool']))
44 3
		{
45 3
			$this->options['memcache.pool'] = false;
46 3
		}
47
48 3
		if (!isset($this->options['memcache.compress']))
49 3
		{
50 3
			$this->options['memcache.compress'] = false;
51 3
		}
52
53 3
		if (!isset($this->options['memcache.servers']))
54 3
		{
55
			$this->options['memcache.servers'] = false;
56
		}
57 3
	}
58
59
	/**
60
	 * This will wipe out the entire cache's keys
61
	 *
62
	 * @return  boolean  The result of the clear operation.
63
	 *
64
	 * @since   1.0
65
	 */
66 2
	public function clear()
67
	{
68 2
		$this->connect();
69
70 2
		return $this->driver->flush();
71
	}
72
73
	/**
74
	 * Method to get a storage entry value from a key.
75
	 *
76
	 * @param   string  $key  The storage entry identifier.
77
	 *
78
	 * @return  CacheItemInterface
79
	 *
80
	 * @since   1.0
81
	 */
82 2 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...
83
	{
84 2
		$this->connect();
85 2
		$value = $this->driver->get($key);
86 2
		$code = $this->driver->getResultCode();
87 2
		$item = new Item($key);
88
89 2
		if ($code === \Memcached::RES_SUCCESS)
90 2
		{
91 1
			$item->set($value);
92 1
		}
93
94 2
		return $item;
95
	}
96
97
	/**
98
	 * Method to remove a storage entry for a key.
99
	 *
100
	 * @param   string  $key  The storage entry identifier.
101
	 *
102
	 * @return  boolean
103
	 *
104
	 * @since   1.0
105
	 */
106 1
	public function deleteItem($key)
107
	{
108 1
		$this->connect();
109
110 1
		if ($this->hasItem($key))
111 1
		{
112 1
			$this->driver->delete($key);
113
114 1
			$rc = $this->driver->getResultCode();
115
116 1
			if ( ($rc != \Memcached::RES_SUCCESS))
117 1
			{
118
				throw new RuntimeException(sprintf('Unable to remove cache entry for %s. Error message `%s`.', $key, $this->driver->getResultMessage()));
119
			}
120 1
		}
121
122 1
		return true;
123
	}
124
125
	/**
126
	 * Persists a cache item immediately.
127
	 *
128
	 * @param   CacheItemInterface  $item  The cache item to save.
129
	 *
130
	 * @return  static  The invoked object.
131
	 */
132 2
	public function save(CacheItemInterface $item)
133
	{
134 2
		$this->connect();
135
136 2
		if ($item instanceof HasExpirationDateInterface)
137 2
		{
138 1
			$ttl = $this->convertItemExpiryToSeconds($item);
139 1
		}
140
		else
141
		{
142 1
			$ttl = 0;
143
		}
144
145 2
		$this->driver->set($item->getKey(), $item->get(), $ttl);
146
147 2
		return (bool) ($this->driver->getResultCode() == \Memcached::RES_SUCCESS);
148
	}
149
150
	/**
151
	 * Method to determine whether a storage entry has been set for a key.
152
	 *
153
	 * @param   string  $key  The storage entry identifier.
154
	 *
155
	 * @return  boolean
156
	 *
157
	 * @since   1.0
158
	 */
159 1
	public function hasItem($key)
160
	{
161 1
		$this->connect();
162
163 1
		$this->driver->get($key);
164
165 1
		return ($this->driver->getResultCode() != \Memcached::RES_NOTFOUND);
166
	}
167
168
	/**
169
	 * Test to see if the CacheItemPoolInterface is available
170
	 *
171
	 * @return  boolean  True on success, false otherwise
172
	 *
173
	 * @since   __DEPLOY_VERSION__
174
	 */
175 1
	public static function isSupported()
176
	{
177
		/*
178
		 * GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded.
179
		 * If the class is there, we can assume it works.
180
		 */
181 1
		return (class_exists('Memcached'));
182
	}
183
184
	/**
185
	 * Connect to the Memcached servers if the connection does not already exist.
186
	 *
187
	 * @return  void
188
	 *
189
	 * @since   1.0
190
	 */
191 4
	private function connect()
192
	{
193
		// We want to only create the driver once.
194 4
		if ($this->driver)
195 4
		{
196 4
			return;
197
		}
198
199 4
		$pool = $this->options['memcache.pool'];
200
201
		if ($pool)
202 4
		{
203
			$this->driver = new \Memcached($pool);
204
		}
205
		else
206
		{
207 4
			$this->driver = new \Memcached;
208
		}
209
210 4
		$this->driver->setOption(\Memcached::OPT_COMPRESSION, $this->options['memcache.compress'] ?: false);
211 4
		$this->driver->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
212 4
		$serverList = $this->driver->getServerList();
213
214
		// If we are using a persistent pool we don't want to add the servers again.
215 4
		if (empty($serverList))
216 4
		{
217 4
			$servers = $this->options['memcache.servers'] ?: array();
218
219 4
			foreach ($servers as $server)
220
			{
221 4
				$this->driver->addServer($server->host, $server->port);
222 4
			}
223 4
		}
224 4
	}
225
}
226