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

Redis::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 16
ccs 8
cts 10
cp 0.8
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3.072
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 Joomla\Cache\Item\Item;
13
use Psr\Cache\CacheItemInterface;
14
use Redis as RedisDriver;
15
16
/**
17
 * Redis cache driver for the Joomla Framework.
18
 *
19
 * @since  1.0
20
 */
21
class Redis extends Cache
22
{
23
	/**
24
	 * Default hostname of redis server
25
	 */
26
	const REDIS_HOST = '127.0.0.1';
27
28
	/**
29
	 * Default port of redis server
30
	 */
31
	const REDIS_PORT = 6379;
32
33
	/**
34
	 * @var    \Redis  The redis driver.
35
	 * @since  1.0
36
	 */
37
	private $driver;
38
39
	/**
40
	 * This will wipe out the entire cache's keys
41
	 *
42
	 * @return  boolean  The result of the clear operation.
43
	 *
44
	 * @since   1.0
45
	 */
46 1
	public function clear()
47
	{
48 1
		$this->connect();
49
50 1
		return $this->driver->flushDB();
51
	}
52
53
	/**
54
	 * Method to get a storage entry value from a key.
55
	 *
56
	 * @param   string  $key  The storage entry identifier.
57
	 *
58
	 * @return  CacheItemInterface
59
	 *
60
	 * @since   1.0
61
	 */
62 1 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...
63
	{
64 1
		$this->connect();
65
66 1
		$value = $this->driver->get($key);
67 1
		$item = new Item($key);
68
69 1
		if ($value !== false)
70 1
		{
71
			$item->set($value);
72
		}
73
74 1
		return $item;
75
	}
76
77
	/**
78
	 * Method to remove a storage entry for a key.
79
	 *
80
	 * @param   string  $key  The storage entry identifier.
81
	 *
82
	 * @return  boolean
83
	 *
84
	 * @since   1.0
85
	 */
86
	public function deleteItem($key)
87
	{
88
		$this->connect();
89
90
		if ($this->hasItem($key))
91
		{
92
			return (bool) $this->driver->del($key);
93
		}
94
95
		// If the item doesn't exist, no error
96
		return true;
97
	}
98
99
	/**
100
	 * Persists a cache item immediately.
101
	 *
102
	 * @param   CacheItemInterface  $item  The cache item to save.
103
	 *
104
	 * @return  bool  True if the item was successfully persisted. False if there was an error.
105
	 */
106 1
	public function save(CacheItemInterface $item)
107
	{
108 1
		$this->connect();
109
110 1
		if ($item instanceof HasExpirationDateInterface)
111 1
		{
112 1
			$ttl = $this->convertItemExpiryToSeconds($item);
113
114 1
			if ($ttl > 0)
115 1
			{
116 1
				return $this->driver->setex($item->getKey(), $ttl, $item->get());
117
			}
118
		}
119
120
		return $this->driver->set($item->getKey(), $item->get());
121
	}
122
123
	/**
124
	 * Method to determine whether a storage entry has been set for a key.
125
	 *
126
	 * @param   string  $key  The storage entry identifier.
127
	 *
128
	 * @return  boolean
129
	 *
130
	 * @since   1.0
131
	 */
132
	public function hasItem($key)
133
	{
134
		$this->connect();
135
136
		return $this->driver->exists($key);
137
	}
138
139
	/**
140
	 * Test to see if the CacheItemPoolInterface is available
141
	 *
142
	 * @return  boolean  True on success, false otherwise
143
	 *
144
	 * @since   __DEPLOY_VERSION__
145
	 */
146 1
	public static function isSupported()
147
	{
148 1
		return (extension_loaded('redis') && class_exists('Redis'));
149
	}
150
151
	/**
152
	 * Connect to the Redis servers if the connection does not already exist.
153
	 *
154
	 * @return  void
155
	 *
156
	 * @since   1.0
157
	 */
158 1
	private function connect()
159
	{
160
		// We want to only create the driver once.
161 1
		if (isset($this->driver))
162 1
		{
163 1
			return;
164
		}
165
166 1
		$host = isset($this->options['redis.host'])? $this->options['redis.host'] : self::REDIS_HOST;
167 1
		$port = isset($this->options['redis.port'])? $this->options['redis.port'] : self::REDIS_PORT;
168
169 1
		$this->driver = new RedisDriver;
170
171 1
		if (($host == 'localhost' || filter_var($host, FILTER_VALIDATE_IP)))
172 1
		{
173 1
			$this->driver->connect('tcp://' . $host . ':' . $port, $port);
174 1
		}
175
		else
176
		{
177
			$this->driver->connect($host, null);
178
		}
179 1
	}
180
}
181