-
Notifications
You must be signed in to change notification settings - Fork 0
/
transient-cache.php
123 lines (94 loc) · 3.01 KB
/
transient-cache.php
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
<?php
// A nested cache using the WordPress transients system.
namespace HitchinHackspace\SlackViewer;
use ArrayAccess;
// Utility methods for things that have a getCache() method.
trait HasCache {
abstract function getCache();
function cacheGet($key) {
$cache = $this->getCache();
return $cache->offsetExists($key) ? $cache->offsetGet($key) : null;
}
function cacheSet($key, $value) {
$this->getCache()->offsetSet($key, $value);
}
function getSubcache($key) {
return new SubCache($this->getCache(), $key);
}
function cached($key, $computeFn) {
$value = $this->cacheGet($key);
if ($value === null) {
$value = $computeFn();
$this->cacheSet($key, $value);
}
return $value;
}
}
// A cache that saves its values to a (single) transient in the database.
class TransientCache implements ArrayAccess {
// The transient key used.
private $key;
// The loaded transient.
private $transient;
function __construct($key) {
$this->key = $key;
$this->load();
}
private function load() {
$this->transient = get_transient($this->key);
if (!is_array($this->transient))
$this->transient = [];
}
public function offsetExists($offset) {
return array_key_exists($offset, $this->transient);
}
public function offsetGet($offset) {
return $this->transient[$offset];
}
public function offsetSet($offset, $value) {
$this->transient[$offset] = $value;
}
public function persist() {
set_transient($this->key, $this->transient, 3600);
}
public function offsetUnset($offset) {
unset($this->transient[$offset]);
}
}
// A cache that represents a part of a larger backing cache.
class SubCache implements ArrayAccess {
// The backing cache.
private $backing;
// The subkey used.
private $key;
function __construct($backing, $key) {
$this->backing = $backing;
$this->key = $key;
}
public function offsetExists($offset) {
if (!$this->backing->offsetExists($this->key))
return false;
$cache = $this->backing->offsetGet($this->key);
return array_key_exists($offset, $cache);
}
public function offsetGet($offset) {
if (!$this->backing->offsetExists($this->key))
return null;
$cache = $this->backing->offsetGet($this->key);
return $cache[$offset];
}
public function offsetSet($offset, $value) {
$cache = [];
if ($this->backing->offsetExists($this->key))
$cache = $this->backing->offsetGet($this->key);
$cache[$offset] = $value;
$this->backing->offsetSet($this->key, $cache);
}
public function offsetUnset($offset) {
if (!$this->backing->offsetExists($this->key))
return;
$cache = $this->backing->offsetGet($this->key);
unset($cache[$offset]);
$this->backing->offsetSet($this->key, $cache);
}
}