1: | <?php |
2: | |
3: | namespace ngatngay; |
4: | |
5: | class cache |
6: | { |
7: | private static bool $debug = false; |
8: | private static ?int $expire = null; |
9: | private static string $prefix = ''; |
10: | |
11: | private static mixed $adapter; |
12: | private static array $adapters = []; |
13: | |
14: | |
15: | |
16: | public static function set_debug(bool $debug): void |
17: | { |
18: | self::$debug = $debug; |
19: | } |
20: | |
21: | public static function set_prefix(string $prefix): void |
22: | { |
23: | self::$prefix = $prefix; |
24: | } |
25: | |
26: | public static function set_expire(?int $ttl = null): void |
27: | { |
28: | self::$expire = $ttl; |
29: | } |
30: | |
31: | |
32: | |
33: | public static function set_adapter(string $key): void |
34: | { |
35: | self::$adapter = self::$adapters[$key]; |
36: | } |
37: | |
38: | public static function get_adapter(string $key): mixed |
39: | { |
40: | return self::$adapters[$key]; |
41: | } |
42: | |
43: | public static function add_adapter(string $key, mixed $adapter): void |
44: | { |
45: | self::$adapters = array_merge(self::$adapters, [$key => $adapter]); |
46: | } |
47: | |
48: | public function remove_adapter(string $key): void |
49: | { |
50: | unset(self::$adapters[$key]); |
51: | } |
52: | |
53: | public static function get_adapters(): array |
54: | { |
55: | return self::$adapters; |
56: | } |
57: | |
58: | |
59: | |
60: | public static function has(string $key): bool |
61: | { |
62: | return self::$adapter->hasItem(self::$prefix . $key); |
63: | } |
64: | |
65: | |
66: | |
67: | public static function get(string $key, mixed $default = null, array $opt = []): mixed |
68: | { |
69: | $opt += [ |
70: | 'empty' => true, |
71: | 'expire' => self::$expire, |
72: | 'refresh' => false, |
73: | ]; |
74: | |
75: | if (self::$debug || $opt['refresh']) { |
76: | self::remove(self::$prefix . $key); |
77: | } |
78: | |
79: | $item = self::$adapter->getItem(self::$prefix . $key); |
80: | |
81: | if ($item->isHit()) { |
82: | return $item->get(); |
83: | } else { |
84: | if (is_callable($default)) { |
85: | $default = call_user_func($default, $opt); |
86: | } |
87: | |
88: | if ($opt['empty'] || (!$opt['empty'] && !empty($default))) { |
89: | self::set($key, $default, $opt['expire']); |
90: | } |
91: | |
92: | return $default; |
93: | } |
94: | } |
95: | |
96: | public static function set(string $key, mixed $value, ?int $expire = null): mixed |
97: | { |
98: | $item = self::$adapter->getItem(self::$prefix . $key); |
99: | |
100: | if ($expire !== null) { |
101: | $item->expiresAfter($expire); |
102: | } elseif (self::$expire !== null) { |
103: | $item->expiresAfter(self::$expire); |
104: | } |
105: | |
106: | $item->set($value); |
107: | return self::$adapter->save($item); |
108: | } |
109: | |
110: | |
111: | public static function update(string $key, mixed $value): mixed { |
112: | |
113: | $item = self::$adapter->getItem($key); |
114: | |
115: | if (!$item->isHit()) { |
116: | return false; |
117: | } |
118: | |
119: | $item->set($value); |
120: | |
121: | |
122: | |
123: | |
124: | $expiration = $item->getExpiration(); |
125: | $ttl = $expiration ? $expiration->getTimestamp() - time() : null; |
126: | |
127: | if ($ttl !== null) { |
128: | $item->expiresAfter($ttl); |
129: | } elseif (self::$expire !== null) { |
130: | $item->expiresAfter(self::$expire); |
131: | } |
132: | |
133: | |
134: | return self::$adapter->save($item); |
135: | } |
136: | |
137: | public static function remove(string $key): bool |
138: | { |
139: | return self::$adapter->deleteItem(self::$prefix . $key); |
140: | } |
141: | |
142: | public static function clear(string $prefix = ''): bool |
143: | { |
144: | return self::$adapter->clear($prefix); |
145: | } |
146: | } |
147: | |