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
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Redis cache driver for the Joomla Framework. |
17
|
|
|
* |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class Redis extends Cache |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The redis driver. |
24
|
|
|
* |
25
|
|
|
* @var \Redis |
26
|
|
|
* @since 1.0 |
27
|
|
|
*/ |
28
|
|
|
private $driver; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param \Redis $redis The Redis driver being used for this pool |
34
|
|
|
* @param array|\ArrayAccess $options An options array, or an object that implements \ArrayAccess |
35
|
|
|
* |
36
|
|
|
* @since __DEPLOY_VERSION__ |
37
|
|
|
* @throws \RuntimeException |
38
|
|
|
*/ |
39
|
10 |
|
public function __construct(\Redis $redis, $options = []) |
40
|
|
|
{ |
41
|
|
|
// Parent sets up the caching options and checks their type |
42
|
10 |
|
parent::__construct($options); |
43
|
|
|
|
44
|
10 |
|
$this->driver = $redis; |
45
|
10 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* This will wipe out the entire cache's keys |
49
|
|
|
* |
50
|
|
|
* @return boolean The result of the clear operation. |
51
|
|
|
* |
52
|
|
|
* @since 1.0 |
53
|
|
|
*/ |
54
|
10 |
|
public function clear() |
55
|
|
|
{ |
56
|
10 |
|
return $this->driver->flushDB(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Method to get a storage entry value from a key. |
61
|
|
|
* |
62
|
|
|
* @param string $key The storage entry identifier. |
63
|
|
|
* |
64
|
|
|
* @return CacheItemInterface |
65
|
|
|
* |
66
|
|
|
* @since 1.0 |
67
|
|
|
*/ |
68
|
5 |
|
public function getItem($key) |
69
|
|
|
{ |
70
|
5 |
|
$value = $this->driver->get($key); |
71
|
5 |
|
$item = new Item($key); |
72
|
|
|
|
73
|
5 |
|
if ($value !== false) |
74
|
5 |
|
{ |
75
|
4 |
|
$item->set($value); |
76
|
4 |
|
} |
77
|
|
|
|
78
|
5 |
|
return $item; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Method to remove a storage entry for a key. |
83
|
|
|
* |
84
|
|
|
* @param string $key The storage entry identifier. |
85
|
|
|
* |
86
|
|
|
* @return boolean |
87
|
|
|
* |
88
|
|
|
* @since 1.0 |
89
|
|
|
*/ |
90
|
2 |
|
public function deleteItem($key) |
91
|
|
|
{ |
92
|
2 |
|
if ($this->hasItem($key)) |
93
|
2 |
|
{ |
94
|
2 |
|
return (bool) $this->driver->del($key); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// If the item doesn't exist, no error |
98
|
2 |
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Persists a cache item immediately. |
103
|
|
|
* |
104
|
|
|
* @param CacheItemInterface $item The cache item to save. |
105
|
|
|
* |
106
|
|
|
* @return bool True if the item was successfully persisted. False if there was an error. |
107
|
|
|
*/ |
108
|
8 |
|
public function save(CacheItemInterface $item) |
109
|
|
|
{ |
110
|
8 |
|
if ($item instanceof HasExpirationDateInterface) |
111
|
8 |
|
{ |
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
|
7 |
|
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
|
4 |
|
public function hasItem($key) |
133
|
|
|
{ |
134
|
4 |
|
return $this->driver->exists($key); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Test to see if the CacheItemPoolInterface is available |
139
|
|
|
* |
140
|
|
|
* @return boolean True on success, false otherwise |
141
|
|
|
* |
142
|
|
|
* @since __DEPLOY_VERSION__ |
143
|
|
|
*/ |
144
|
10 |
|
public static function isSupported() |
145
|
|
|
{ |
146
|
10 |
|
return (extension_loaded('redis') && class_exists('Redis')); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|