Completed
Push — 2.0-dev ( 574eeb...e9e84c )
by George
12s
created

Redis::hasItem()   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 1
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\Item\HasExpirationDateInterface;
13
use Joomla\Cache\Item\Item;
14
use Psr\Cache\CacheItemInterface;
15
16
/**
17
 * Redis cache driver for the Joomla Framework.
18
 *
19
 * @since  1.0
20
 */
21
class Redis extends AbstractCacheItemPool
22
{
23
	/**
24
	 * The redis driver.
25
	 *
26
	 * @var    \Redis
27
	 * @since  1.0
28
	 */
29
	protected $driver;
30
31
	/**
32
	 * Constructor.
33
	 *
34
	 * @param   \Redis              $redis    The Redis driver being used for this pool
35
	 * @param   array|\ArrayAccess  $options  An options array, or an object that implements \ArrayAccess
36
	 *
37
	 * @since   __DEPLOY_VERSION__
38
	 */
39 19
	public function __construct(\Redis $redis, $options = [])
40
	{
41
		// Parent sets up the caching options and checks their type
42 19
		parent::__construct($options);
43
44 19
		$this->driver = $redis;
45 19
	}
46
47
	/**
48
	 * This will wipe out the entire cache's keys
49
	 *
50
	 * @return  boolean  True if the pool was successfully cleared. False if there was an error.
51
	 *
52
	 * @since   1.0
53
	 */
54 19
	public function clear()
55
	{
56 19
		return $this->driver->flushDB();
57
	}
58
59
	/**
60
	 * Returns a Cache Item representing the specified key.
61
	 *
62
	 * @param   string  $key  The key for which to return the corresponding Cache Item.
63
	 *
64
	 * @return  CacheItemInterface  The corresponding Cache Item.
65
	 *
66
	 * @since   __DEPLOY_VERSION__
67
	 */
68 12 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...
69
	{
70 12
		$value = $this->driver->get($key);
71 12
		$item = new Item($key);
72
73 12
		if ($value !== false)
74 12
		{
75 10
			$item->set($value);
76 10
		}
77
78 12
		return $item;
79
	}
80
81
	/**
82
	 * Removes the item from the pool.
83
	 *
84
	 * @param   string  $key  The key to delete.
85
	 *
86
	 * @return  boolean  True if the item was successfully removed. False if there was an error.
87
	 *
88
	 * @since   __DEPLOY_VERSION__
89
	 */
90 4
	public function deleteItem($key)
91
	{
92 4
		if ($this->hasItem($key))
93 4
		{
94 4
			return (bool) $this->driver->del($key);
95
		}
96
97
		// If the item doesn't exist, no error
98 4
		return true;
99
	}
100
101
	/**
102
	 * Persists a cache item immediately.
103
	 *
104
	 * @param   CacheItemInterface  $item  The cache item to save.
105
	 *
106
	 * @return  boolean  True if the item was successfully persisted. False if there was an error.
107
	 *
108
	 * @since   __DEPLOY_VERSION__
109
	 */
110 16
	public function save(CacheItemInterface $item)
111
	{
112 16
		if ($item instanceof HasExpirationDateInterface)
113 16
		{
114 8
			$ttl = $this->convertItemExpiryToSeconds($item);
115
116 8
			if ($ttl > 0)
117 8
			{
118 7
				return $this->driver->setex($item->getKey(), $ttl, $item->get());
119
			}
120 1
		}
121
122 9
		return $this->driver->set($item->getKey(), $item->get());
123
	}
124
125
	/**
126
	 * Confirms if the cache contains specified cache item.
127
	 *
128
	 * @param   string  $key  The key for which to check existence.
129
	 *
130
	 * @return  boolean  True if item exists in the cache, false otherwise.
131
	 *
132
	 * @since   1.0
133
	 */
134 8
	public function hasItem($key)
135
	{
136 8
		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 19
	public static function isSupported()
147
	{
148 19
		return (extension_loaded('redis') && class_exists('Redis'));
149
	}
150
}
151