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 Psr\Cache\CacheItemInterface; |
12
|
|
|
use Joomla\Cache\Item\Item; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Runtime memory cache driver. |
16
|
|
|
* |
17
|
|
|
* @since 1.0 |
18
|
|
|
*/ |
19
|
|
|
class Runtime extends Cache |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \ArrayObject Database of cached items, we use ArrayObject so it can be easily |
23
|
|
|
* passed by reference |
24
|
|
|
* |
25
|
|
|
* @since 2.0 |
26
|
|
|
*/ |
27
|
|
|
private $db; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @param mixed $options An options array, or an object that implements \ArrayAccess |
33
|
|
|
* |
34
|
|
|
* @since 2.0 |
35
|
|
|
* @throws \RuntimeException |
36
|
1 |
|
*/ |
37
|
|
|
public function __construct($options = array()) |
38
|
1 |
|
{ |
39
|
|
|
parent::__construct($options); |
40
|
1 |
|
$this->db = new \ArrayObject; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This will wipe out the entire cache's keys |
45
|
|
|
* |
46
|
|
|
* @return boolean The result of the clear operation. |
47
|
|
|
* |
48
|
|
|
* @since 1.0 |
49
|
|
|
*/ |
50
|
|
|
public function clear() |
51
|
|
|
{ |
52
|
1 |
|
// Replace the db with a new blank array |
53
|
|
|
$clearData = $this->db->exchangeArray(array()); |
54
|
1 |
|
unset($clearData); |
55
|
|
|
|
56
|
1 |
|
return true; |
57
|
1 |
|
} |
58
|
1 |
|
|
59
|
1 |
|
/** |
60
|
|
|
* Method to get a storage entry value from a key. |
61
|
1 |
|
* |
62
|
|
|
* @param string $key The storage entry identifier. |
63
|
|
|
* |
64
|
|
|
* @return CacheItemInterface |
65
|
|
|
* |
66
|
|
|
* @since 1.0 |
67
|
|
|
*/ |
68
|
|
|
public function getItem($key) |
69
|
|
|
{ |
70
|
|
|
$item = new Item($key); |
71
|
|
|
|
72
|
|
|
try |
73
|
1 |
|
{ |
74
|
|
|
$item->set($this->db[$key]); |
75
|
1 |
|
} |
76
|
|
|
catch (\Exception $e) |
|
|
|
|
77
|
1 |
|
{ |
78
|
|
|
/** |
79
|
|
|
* Backend caching mechanisms may throw exceptions |
80
|
|
|
* to indicate missing data. Catch all exceptions |
81
|
|
|
* so program flow is uninterrupted. For misses |
82
|
|
|
* we can safely do nothing and return the |
83
|
|
|
* CacheItem we created since it flags itself as |
84
|
|
|
* a miss when constructed. Specific cache classes |
85
|
|
|
* should override this method and deal with |
86
|
|
|
* exceptions appropriately. |
87
|
1 |
|
*/ |
88
|
|
|
} |
89
|
1 |
|
|
90
|
|
|
return $item; |
91
|
1 |
|
} |
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 deleteItem($key) |
103
|
1 |
|
{ |
104
|
|
|
$oldCache = $this->db->getArrayCopy(); |
105
|
1 |
|
|
106
|
|
|
if (array_key_exists($key, $oldCache)) |
107
|
|
|
{ |
108
|
|
|
$keyArray = array($key => $key); |
109
|
|
|
$newCache = array_diff_key($oldCache, $keyArray); |
110
|
|
|
$this->db->exchangeArray($newCache); |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Method to set a value for a storage entry. |
120
|
|
|
* |
121
|
|
|
* @param CacheItemInterface $item The cache item to save. |
122
|
|
|
* |
123
|
|
|
* @return boolean |
124
|
|
|
* |
125
|
|
|
* @since 1.0 |
126
|
|
|
*/ |
127
|
|
|
public function save(CacheItemInterface $item) |
128
|
|
|
{ |
129
|
|
|
$this->db[$item->getKey()] = $item->get(); |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Method to determine whether a storage entry has been set for a key. |
136
|
|
|
* |
137
|
|
|
* @param string $key The storage entry identifier. |
138
|
|
|
* |
139
|
|
|
* @return boolean |
140
|
|
|
* |
141
|
|
|
* @since 1.0 |
142
|
|
|
*/ |
143
|
|
|
public function hasItem($key) |
144
|
|
|
{ |
145
|
|
|
return array_key_exists($key, $this->db); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Test to see if the CacheItemPoolInterface is available |
150
|
|
|
* |
151
|
|
|
* @return boolean True on success, false otherwise |
152
|
|
|
* |
153
|
|
|
* @since __DEPLOY_VERSION__ |
154
|
|
|
*/ |
155
|
|
|
public static function isSupported() |
156
|
|
|
{ |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|