blob: 97f294c9dc2c262c1f376bb08b51d697b8401c25 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
<?php
namespace Studip\Cache;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
/**
* An abstract class which has to be extended by instances returned from
* \Studip\Cache\Factory#getCache
*
* @author Marco Diedrich (mdiedric@uos)
* @author Marcus Lunzenauer (mlunzena@uos.de)
* @author Moritz Strohm <strohm@data-quest.de>
* @copyright (c) Authors
* @since 1.6
* @license GPL2 or any later version
*/
abstract class Cache implements CacheItemPoolInterface
{
const DEFAULT_EXPIRATION = 12 * 60 * 60; // 12 hours
/**
* @return string A translateable display name for this cache class.
*/
abstract public static function getDisplayName(): string;
/**
* Get some statistics from cache, like number of entries, hit rate or
* whatever the underlying cache provides.
* Results are returned in form of an array like
* "[
* [
* 'name' => <displayable name>
* 'value' => <value of the current stat>
* ]
* ]"
*
* @return array
*/
abstract public function getStats(): array;
/**
* Return the Vue component name and props that handle configuration.
* The associative array is of the form
* [
* 'component' => <Vue component name>,
* 'props' => <Properties for component>
* ]
*
* @return array
*/
abstract public static function getConfig(): array;
/**
* Expire item from the cache.
*
* Example:
*
* # expires foo
* $cache->expire('foo');
*
* @param string $arg a single key
*/
abstract public function expire($arg);
/**
* Expire all items from the cache.
*/
abstract public function flush();
/**
* @see CacheItemPoolInterface::getItem
*/
abstract public function getItem(string $key): CacheItemInterface;
/**
* @see CacheItemPoolInterface::hasItem
*/
abstract public function hasItem(string $key): bool;
/**
* @var array An array of deferred items that shall be saved only
* when commit() is called. This is only used in PSR-6 cache methods.
*/
protected array $deferred_items = [];
/**
* Retrieve item from the server.
*
* Example:
*
* # reads foo
* $foo = $cache->reads('foo');
*
* @param string $arg a single key
*
* @return mixed the previously stored data if an item with such a key
* exists on the server or FALSE on failure.
*/
public function read($arg)
{
$item = $this->getItem($arg);
if ($item->isHit()) {
return $item->get();
}
return false;
}
/**
* Store data at the server.
*
* @param string $name the item's key.
* @param mixed $content the item's content (will be serialized if necessary).
* @param int $expires the item's expiry time in seconds. Optional, defaults to 12h.
*
* @return bool returns TRUE on success or FALSE on failure.
*/
public function write($name, $content, $expires = self::DEFAULT_EXPIRATION)
{
$item = new Item($name, $content, $expires);
return $this->save($item);
}
/**
* Calculates the expiration by a cache item. If that cannot be determined,
* the default expiration period is returned.
*
* @param Item $item The item from which to get the expiration time.
*
* @return int The time from now until the expiration in seconds.
*/
public function getExpiration(CacheItemInterface $item) : int
{
$expiration = self::DEFAULT_EXPIRATION;
if ($item instanceof Item) {
$expiration = $item->getExpirationInSeconds();
}
return $expiration;
}
// PSR-6 CacheItemPoolInterface:
/**
* @see CacheItemPoolInterface::getItems
*/
public function getItems(array $keys = []): iterable
{
$items = [];
foreach ($keys as $key) {
$items[$key] = $this->getItem($key);
}
return $items;
}
/**
* @see CacheItemPoolInterface::clear
*/
public function clear(): bool
{
$this->deferred_items = [];
$this->flush();
return true;
}
/**
* @see CacheItemPoolInterface::deleteItem
*/
public function deleteItem($key): bool
{
$this->expire($key);
return true;
}
/**
* @see CacheItemPoolInterface::deleteItems
*/
public function deleteItems(array $keys): bool
{
foreach ($keys as $key) {
$this->expire($key);
}
return true;
}
/**
* @see CacheItemPoolInterface::saveDeferred
*/
public function saveDeferred(CacheItemInterface $item): bool
{
$this->deferred_items[] = $item;
return true;
}
/**
* @see CacheItemPoolInterface::commit
*/
public function commit(): bool
{
foreach ($this->deferred_items as $item) {
$this->save($item);
}
return true;
}
}
|