1: <?php
2:
3: namespace ngatngay;
4:
5: class cache
6: {
7: /**
8: * @var bool
9: */
10: private static $debug = false;
11:
12: /**
13: * @var int|null
14: */
15: private static $expire = null;
16:
17: /**
18: * @var string
19: */
20: private static $prefix = '';
21:
22: /**
23: * @var mixed
24: */
25: private static $adapter;
26:
27: /**
28: * @var array
29: */
30: private static $adapters = [];
31:
32: // common
33:
34: /**
35: * @param bool $debug
36: * @return void
37: */
38: public static function set_debug($debug)
39: {
40: self::$debug = $debug;
41: }
42:
43: /**
44: * @param string $prefix
45: * @return void
46: */
47: public static function set_prefix($prefix)
48: {
49: self::$prefix = $prefix;
50: }
51:
52: /**
53: * @param int|null $ttl
54: * @return void
55: */
56: public static function set_expire($ttl = null)
57: {
58: self::$expire = $ttl;
59: }
60:
61: // adapter
62:
63: /**
64: * @param string $key
65: * @return void
66: */
67: public static function set_adapter($key)
68: {
69: self::$adapter = self::$adapters[$key];
70: }
71:
72: /**
73: * @param string $key
74: * @return mixed
75: */
76: public static function get_adapter($key)
77: {
78: return self::$adapters[$key];
79: }
80:
81: /**
82: * @param string $key
83: * @param mixed $adapter
84: * @return void
85: */
86: public static function add_adapter($key, $adapter)
87: {
88: self::$adapters = array_merge(self::$adapters, [$key => $adapter]);
89: }
90:
91: /**
92: * @param string $key
93: * @return void
94: */
95: public function remove_adapter($key)
96: {
97: unset(self::$adapters[$key]);
98: }
99:
100: /**
101: * @return array
102: */
103: public static function get_adapters()
104: {
105: return self::$adapters;
106: }
107:
108: // cache
109:
110: /**
111: * @param string $key
112: * @return bool
113: */
114: public static function has($key)
115: {
116: return self::$adapter->hasItem(self::$prefix . $key);
117: }
118:
119: // truyen 1 tham so - lay binh thuong
120: // truyen 2 tham so tro len - luu cache cho lan sau
121: /**
122: * @param string $key
123: * @param mixed $default
124: * @param array $opt
125: * @return mixed
126: */
127: public static function get($key, $default = null, $opt = [])
128: {
129: $opt += [
130: 'expire' => self::$expire,
131: 'debug' => false,
132: 'save' => true,
133: 'save_if' => null, // ?callable
134: ];
135:
136: if (self::$debug || $opt['debug']) {
137: self::unset(self::$prefix . $key);
138: }
139:
140: // get cache
141: $item = self::$adapter->getItem(self::$prefix . $key);
142:
143: if ($item->isHit()) {
144: return $item->get();
145: } else {
146: if (is_callable($default)) {
147: $default = call_user_func($default, $opt);
148: }
149:
150: if ($opt['save']) {
151: $save = false;
152:
153: if (is_callable($opt['save_if'])) {
154: if (call_user_func($opt['save_if'], $default)) {
155: $save = true;
156: }
157: } else {
158: $save = true;
159: }
160:
161: if ($save) {
162: self::set($key, $default, $opt['expire']);
163: }
164: }
165:
166: return $default;
167: }
168: }
169:
170: /**
171: * @param string $key
172: * @param mixed $value
173: * @param int|null $expire
174: * @return bool
175: */
176: public static function set($key, $value, $expire = null)
177: {
178: $item = self::$adapter->getItem(self::$prefix . $key);
179:
180: if ($expire !== null) {
181: $item->expiresAfter($expire);
182: } elseif (self::$expire !== null) {
183: $item->expiresAfter(self::$expire);
184: }
185:
186: $item->set($value);
187: return self::$adapter->save($item);
188: }
189:
190: /**
191: * @param string $key
192: * @return bool
193: */
194: public static function unset($key)
195: {
196: return self::$adapter->deleteItem(self::$prefix . $key);
197: }
198:
199: /**
200: * @param string $prefix
201: * @return bool
202: */
203: public static function clear($prefix = '')
204: {
205: return self::$adapter->clear($prefix);
206: }
207: }
208: