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\Item; |
13
|
|
|
use Psr\Cache\CacheItemInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Runtime memory cache driver. |
17
|
|
|
* |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class Runtime extends AbstractCacheItemPool |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Database of cached items, we use ArrayObject so it can be easily passed by reference |
24
|
|
|
* |
25
|
|
|
* @var \ArrayObject |
26
|
|
|
* @since __DEPLOY_VERSION__ |
27
|
|
|
*/ |
28
|
|
|
protected $db; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param mixed $options An options array, or an object that implements \ArrayAccess |
34
|
|
|
* |
35
|
|
|
* @since __DEPLOY_VERSION__ |
36
|
|
|
*/ |
37
|
18 |
|
public function __construct($options = array()) |
38
|
|
|
{ |
39
|
18 |
|
parent::__construct($options); |
40
|
|
|
|
41
|
18 |
|
$this->db = new \ArrayObject; |
42
|
18 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* This will wipe out the entire cache's keys |
46
|
|
|
* |
47
|
|
|
* @return boolean True if the pool was successfully cleared. False if there was an error. |
48
|
|
|
* |
49
|
|
|
* @since 1.0 |
50
|
|
|
*/ |
51
|
18 |
|
public function clear() |
52
|
|
|
{ |
53
|
|
|
// Replace the db with a new blank array |
54
|
18 |
|
$clearData = $this->db->exchangeArray(array()); |
55
|
18 |
|
unset($clearData); |
56
|
|
|
|
57
|
18 |
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns a Cache Item representing the specified key. |
62
|
|
|
* |
63
|
|
|
* @param string $key The key for which to return the corresponding Cache Item. |
64
|
|
|
* |
65
|
|
|
* @return CacheItemInterface The corresponding Cache Item. |
66
|
|
|
* |
67
|
|
|
* @since __DEPLOY_VERSION__ |
68
|
|
|
*/ |
69
|
11 |
|
public function getItem($key) |
70
|
|
|
{ |
71
|
11 |
|
$item = new Item($key); |
72
|
|
|
|
73
|
11 |
|
if ($this->hasItem($key)) |
74
|
|
|
{ |
75
|
9 |
|
$item->set($this->db[$key]); |
76
|
|
|
} |
77
|
|
|
|
78
|
11 |
|
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
|
|
|
{ |
94
|
4 |
|
$newCache = array_diff_key($this->db->getArrayCopy(), array($key => $key)); |
95
|
4 |
|
$this->db->exchangeArray($newCache); |
96
|
|
|
} |
97
|
|
|
|
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
|
15 |
|
public function save(CacheItemInterface $item) |
111
|
|
|
{ |
112
|
15 |
|
$this->db[$item->getKey()] = $item->get(); |
113
|
|
|
|
114
|
15 |
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Confirms if the cache contains specified cache item. |
119
|
|
|
* |
120
|
|
|
* @param string $key The key for which to check existence. |
121
|
|
|
* |
122
|
|
|
* @return boolean True if item exists in the cache, false otherwise. |
123
|
|
|
* |
124
|
|
|
* @since 1.0 |
125
|
|
|
*/ |
126
|
15 |
|
public function hasItem($key) |
127
|
|
|
{ |
128
|
15 |
|
return array_key_exists($key, $this->db); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Test to see if the CacheItemPoolInterface is available |
133
|
|
|
* |
134
|
|
|
* @return boolean True on success, false otherwise |
135
|
|
|
* |
136
|
|
|
* @since __DEPLOY_VERSION__ |
137
|
|
|
*/ |
138
|
|
|
public static function isSupported(): bool |
139
|
|
|
{ |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|