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
|
|
|
private $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
|
|
|
* @throws \RuntimeException |
39
|
|
|
*/ |
40
|
12 |
|
public function __construct(\Redis $redis, $options = []) |
41
|
|
|
{ |
42
|
|
|
// Parent sets up the caching options and checks their type |
43
|
12 |
|
parent::__construct($options); |
44
|
|
|
|
45
|
12 |
|
$this->driver = $redis; |
46
|
12 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This will wipe out the entire cache's keys |
50
|
|
|
* |
51
|
|
|
* @return boolean The result of the clear operation. |
52
|
|
|
* |
53
|
|
|
* @since 1.0 |
54
|
|
|
*/ |
55
|
12 |
|
public function clear() |
56
|
|
|
{ |
57
|
12 |
|
return $this->driver->flushDB(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Method to get a storage entry value from a key. |
62
|
|
|
* |
63
|
|
|
* @param string $key The storage entry identifier. |
64
|
|
|
* |
65
|
|
|
* @return CacheItemInterface |
66
|
|
|
* |
67
|
|
|
* @since 1.0 |
68
|
|
|
*/ |
69
|
5 |
View Code Duplication |
public function getItem($key) |
|
|
|
|
70
|
|
|
{ |
71
|
5 |
|
$value = $this->driver->get($key); |
72
|
5 |
|
$item = new Item($key); |
73
|
|
|
|
74
|
5 |
|
if ($value !== false) |
75
|
5 |
|
{ |
76
|
4 |
|
$item->set($value); |
77
|
4 |
|
} |
78
|
|
|
|
79
|
5 |
|
return $item; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Method to remove a storage entry for a key. |
84
|
|
|
* |
85
|
|
|
* @param string $key The storage entry identifier. |
86
|
|
|
* |
87
|
|
|
* @return boolean |
88
|
|
|
* |
89
|
|
|
* @since 1.0 |
90
|
|
|
*/ |
91
|
2 |
|
public function deleteItem($key) |
92
|
|
|
{ |
93
|
2 |
|
if ($this->hasItem($key)) |
94
|
2 |
|
{ |
95
|
2 |
|
return (bool) $this->driver->del($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// If the item doesn't exist, no error |
99
|
2 |
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Persists a cache item immediately. |
104
|
|
|
* |
105
|
|
|
* @param CacheItemInterface $item The cache item to save. |
106
|
|
|
* |
107
|
|
|
* @return bool True if the item was successfully persisted. False if there was an error. |
108
|
|
|
*/ |
109
|
9 |
|
public function save(CacheItemInterface $item) |
110
|
|
|
{ |
111
|
9 |
|
if ($item instanceof HasExpirationDateInterface) |
112
|
9 |
|
{ |
113
|
1 |
|
$ttl = $this->convertItemExpiryToSeconds($item); |
114
|
|
|
|
115
|
1 |
|
if ($ttl > 0) |
116
|
1 |
|
{ |
117
|
1 |
|
return $this->driver->setex($item->getKey(), $ttl, $item->get()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
8 |
|
return $this->driver->set($item->getKey(), $item->get()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Method to determine whether a storage entry has been set for a key. |
126
|
|
|
* |
127
|
|
|
* @param string $key The storage entry identifier. |
128
|
|
|
* |
129
|
|
|
* @return boolean |
130
|
|
|
* |
131
|
|
|
* @since 1.0 |
132
|
|
|
*/ |
133
|
5 |
|
public function hasItem($key) |
134
|
|
|
{ |
135
|
5 |
|
return $this->driver->exists($key); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Test to see if the CacheItemPoolInterface is available |
140
|
|
|
* |
141
|
|
|
* @return boolean True on success, false otherwise |
142
|
|
|
* |
143
|
|
|
* @since __DEPLOY_VERSION__ |
144
|
|
|
*/ |
145
|
12 |
|
public static function isSupported() |
146
|
|
|
{ |
147
|
12 |
|
return (extension_loaded('redis') && class_exists('Redis')); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.