-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheable.hh
170 lines (145 loc) · 3.95 KB
/
Cacheable.hh
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
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Common;
/**
* The Cacheable trait provides functionality to cache any data from the class layer.
* All data is unique and represented by a generated cache key.
*
* @package Titon\Common
*/
trait Cacheable {
/**
* Cached items indexed by key.
*
* @var \Titon\Common\CacheMap
*/
protected CacheMap $cache = Map {};
/**
* Is cache on or off?
*
* @var bool
*/
protected bool $cacheEnabled = true;
/**
* Return all the current cached items.
*
* @return \Titon\Common\CacheMap
*/
public function allCache(): CacheMap {
return $this->cache;
}
/**
* Dynamically read and write from the cache at once. If the cache exists with the key return it, else execute and save the result.
* If the value happens to be a closure, evaluate the closure and save the result.
*
* @param mixed $key
* @param (function(this): mixed) $callback
* @return mixed
*/
public function cache(mixed $key,/* HH_FIXME[2043]: this type hint */ (function(this): mixed) $callback): mixed {
$key = $this->createCacheKey($key);
if ($this->hasCache($key)) {
return $this->getCache($key);
}
return $this->setCache($key, $callback($this));
}
/**
* Generate a cache key. If an array is passed, drill down and form a key.
*
* @param mixed $keys
* @return string
*/
public function createCacheKey(mixed $keys): string {
if ($keys instanceof Traversable) {
$key = '';
foreach ($keys as $value) {
if ($value instanceof Traversable) {
$key .= '-' . md5(serialize($value));
} else if ($value) {
$key .= '-' . $value;
}
}
} else {
$key = $keys;
}
return trim((string) $key, '-');
}
/**
* Empty the cache.
*
* @return $this
*/
public function flushCache(): this {
$this->allCache()->clear();
return $this;
}
/**
* Return a cached item if it exists, else return null.
*
* @param mixed $key
* @return mixed
*/
public function getCache(mixed $key): mixed {
if (!$this->isCacheEnabled()) {
return null;
}
return $this->allCache()->get($this->createCacheKey($key));
}
/**
* Check to see if the cache key exists.
*
* @param mixed $key
* @return bool
*/
public function hasCache(mixed $key): bool {
return $this->allCache()->contains($this->createCacheKey($key));
}
/**
* Return true if caching is enabled.
*
* @return bool
*/
public function isCacheEnabled(): bool {
return $this->cacheEnabled;
}
/**
* Remove an item from the cache. Return true if the item was removed.
*
* @param mixed $key
* @return $this
*/
public function removeCache(mixed $key): this {
$this->allCache()->remove($this->createCacheKey($key));
return $this;
}
/**
* Set a value to the cache with the defined key.
* This will overwrite any data with the same key.
* The value being saved will be returned.
*
* @param mixed $key
* @param mixed $value
* @return mixed
*/
public function setCache(mixed $key, mixed $value): mixed {
if (!$this->isCacheEnabled()) {
return $value;
}
$this->allCache()->set($this->createCacheKey($key), $value);
return $value;
}
/**
* Toggle cache on and off.
*
* @param bool $on
* @return $this
*/
public function toggleCache(bool $on = true): this {
$this->cacheEnabled = $on;
return $this;
}
}