| 1: | <?php |
| 2: | |
| 3: | namespace ngatngay; |
| 4: | |
| 5: | class cache |
| 6: | { |
| 7: | |
| 8: | |
| 9: | |
| 10: | private static $debug = false; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | private static $expire = null; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | private static $prefix = ''; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | private static $adapter; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | private static $adapters = []; |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | public static function set_debug($debug) |
| 39: | { |
| 40: | self::$debug = $debug; |
| 41: | } |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | public static function set_prefix($prefix) |
| 48: | { |
| 49: | self::$prefix = $prefix; |
| 50: | } |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public static function set_expire($ttl = null) |
| 57: | { |
| 58: | self::$expire = $ttl; |
| 59: | } |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public static function set_adapter($key) |
| 68: | { |
| 69: | self::$adapter = self::$adapters[$key]; |
| 70: | } |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public static function get_adapter($key) |
| 77: | { |
| 78: | return self::$adapters[$key]; |
| 79: | } |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | public static function add_adapter($key, $adapter) |
| 87: | { |
| 88: | self::$adapters = array_merge(self::$adapters, [$key => $adapter]); |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | public function remove_adapter($key) |
| 96: | { |
| 97: | unset(self::$adapters[$key]); |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | public static function get_adapters() |
| 104: | { |
| 105: | return self::$adapters; |
| 106: | } |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | public static function has($key) |
| 115: | { |
| 116: | return self::$adapter->hasItem(self::$prefix . $key); |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 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, |
| 134: | ]; |
| 135: | |
| 136: | if (self::$debug || $opt['debug']) { |
| 137: | self::unset(self::$prefix . $key); |
| 138: | } |
| 139: | |
| 140: | |
| 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: | |
| 172: | |
| 173: | |
| 174: | |
| 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: | |
| 192: | |
| 193: | |
| 194: | public static function unset($key) |
| 195: | { |
| 196: | return self::$adapter->deleteItem(self::$prefix . $key); |
| 197: | } |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | public static function clear($prefix = '') |
| 204: | { |
| 205: | return self::$adapter->clear($prefix); |
| 206: | } |
| 207: | } |
| 208: | |