Completed
Push — master ( 356022...cdc0e1 )
by Michael
01:23
created

Redis::connect()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 5
nop 0
1
<?php
2
/**
3
 * Part of the Joomla Framework Cache Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2016 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 Psr\Cache\CacheItemInterface;
12
use Redis as RedisDriver;
13
14
/**
15
 * Redis cache driver for the Joomla Framework.
16
 *
17
 * @since  1.0
18
 */
19
class Redis extends Cache
20
{
21
	/**
22
	 * Default hostname of redis server
23
	 */
24
	const REDIS_HOST = 'localhost';
25
26
	/**
27
	 * Default port of redis server
28
	 */
29
	const REDIS_PORT = 6379;
30
31
	/**
32
	 * @var    \Redis  The redis driver.
33
	 * @since  1.0
34
	 */
35
	private $driver;
36
37
	/**
38
	 * Constructor.
39
	 *
40
	 * @param   mixed  $options  An options array, or an object that implements \ArrayAccess
41
	 *
42
	 * @since   1.0
43
	 * @throws  \RuntimeException
44
	 */
45
	public function __construct($options = array())
46
	{
47
		if (!extension_loaded('redis') || !class_exists('\Redis'))
48
		{
49
			throw new \RuntimeException('Redis not supported.');
50
		}
51
52
		parent::__construct($options);
53
	}
54
55
	/**
56
	 * This will wipe out the entire cache's keys
57
	 *
58
	 * @return  boolean  The result of the clear operation.
59
	 *
60
	 * @since   1.0
61
	 */
62
	public function clear()
63
	{
64
		$this->connect();
65
66
		return $this->driver->flushAll();
67
	}
68
69
	/**
70
	 * Method to get a storage entry value from a key.
71
	 *
72
	 * @param   string  $key  The storage entry identifier.
73
	 *
74
	 * @return  CacheItemInterface
75
	 *
76
	 * @since   1.0
77
	 */
78 View Code Duplication
	public function get($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...
79
	{
80
		$this->connect();
81
82
		$value = $this->driver->get($key);
83
		$item = new Item($key);
84
85
		if ($value !== false)
86
		{
87
			$item->setValue($value);
88
		}
89
90
		return $item;
91
	}
92
93
	/**
94
	 * Method to remove a storage entry for a key.
95
	 *
96
	 * @param   string  $key  The storage entry identifier.
97
	 *
98
	 * @return  boolean
99
	 *
100
	 * @since   1.0
101
	 */
102
	public function remove($key)
103
	{
104
		$this->connect();
105
106
		return (bool) $this->driver->del($key);
107
	}
108
109
	/**
110
	 * Method to set a value for a storage entry.
111
	 *
112
	 * @param   string   $key    The storage entry identifier.
113
	 * @param   mixed    $value  The data to be stored.
114
	 * @param   integer  $ttl    The number of seconds before the stored data expires.
115
	 *
116
	 * @return  boolean
117
	 *
118
	 * @since   1.0
119
	 */
120
	public function set($key, $value, $ttl = null)
121
	{
122
		$this->connect();
123
124
		if (!$this->driver->set($key, $value))
125
		{
126
			return false;
127
		}
128
129
		if ($ttl && !$this->driver->expire($key, $ttl))
0 ignored issues
show
Bug Best Practice introduced by
The expression $ttl of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
130
		{
131
			return false;
132
		}
133
134
		return true;
135
	}
136
137
	/**
138
	 * Method to determine whether a storage entry has been set for a key.
139
	 *
140
	 * @param   string  $key  The storage entry identifier.
141
	 *
142
	 * @return  boolean
143
	 *
144
	 * @since   1.0
145
	 */
146
	public function exists($key)
147
	{
148
		$this->connect();
149
150
		return $this->driver->exists($key);
151
	}
152
153
	/**
154
	 * Connect to the Redis servers if the connection does not already exist.
155
	 *
156
	 * @return  void
157
	 *
158
	 * @since   1.0
159
	 */
160
	private function connect()
161
	{
162
		// We want to only create the driver once.
163
		if ($this->driver !== null)
164
		{
165
			return;
166
		}
167
168
		$host = isset($this->options['redis.host'])? $this->options['redis.host'] : self::REDIS_HOST;
169
		$port = isset($this->options['redis.port'])? $this->options['redis.port'] : self::REDIS_PORT;
170
171
		$this->driver = new RedisDriver;
172
		$this->driver->connect($host, $port);
173
	}
174
}
175