-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticCacheable.hh
127 lines (110 loc) · 3.28 KB
/
StaticCacheable.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
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Common;
/**
* The StaticCacheable trait provides functionality to cache any data from the static class layer.
* All data is unique and represented by a generated cache key.
*
* @package Titon\Common
*/
trait StaticCacheable {
/**
* Cached items indexed by key.
*
* @var \Titon\Common\CacheMap
*/
protected static CacheMap $cache = Map {};
/**
* Return all the current cached items.
*
* @return \Titon\Common\CacheMap
*/
public static function allCache(): CacheMap {
return static::$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(): mixed) $callback
* @return mixed
*/
public static function cache(mixed $key, (function(): mixed) $callback): mixed {
$key = static::createCacheKey($key);
if (static::hasCache($key)) {
return static::getCache($key);
}
return static::setCache($key, call_user_func($callback));
}
/**
* Generate a cache key. If an array is passed, drill down and form a key.
*
* @param mixed $keys
* @return string
*/
public static 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.
*/
public static function flushCache(): void {
static::allCache()->clear();
}
/**
* Return a cached item if it exists, else return null.
*
* @param mixed $key
* @return mixed
*/
public static function getCache(mixed $key): mixed {
return static::allCache()->get(static::createCacheKey($key));
}
/**
* Check to see if the cache key exists.
*
* @param mixed $key
* @return bool
*/
public static function hasCache(mixed $key): bool {
return static::allCache()->contains(static::createCacheKey($key));
}
/**
* Remove an item from the cache. Return true if the item was removed.
*
* @param mixed $key
*/
public static function removeCache(mixed $key): void {
static::allCache()->remove(static::createCacheKey($key));
}
/**
* 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 static function setCache(mixed $key, mixed $value): mixed {
static::allCache()->set(static::createCacheKey($key), $value);
return $value;
}
}