handler = new \Memcached(); $this->handler->addServer($option['host'] ?? '127.0.0.1', $option['port'] ?? '11211'); $this->prefix = $option['prefix'] ?? Config::get('cache.prefix', ''); } public function set(string $key, $value, ?int $time = 0) { return $this->handler->set($this->prefix . $key, Serialize::encode($value), $time); } public function get(string $key) { $return = $this->handler->get($this->prefix . $key); if ($return === false) { return null; } elseif (is_string($return)) { return Serialize::decode($return); } else { return $return; } } public function remove(string $key) { return $this->handler->delete($this->prefix . $key); } public function inc(string $key, int $num = 1) { $key = $this->prefix . $key; if ($this->handler->get($key)) { return $this->handler->increment($key, $num); } $this->handler->set($key, $num); return $num; } public function dec(string $key, int $num = 1) { $key = $this->prefix . $key; if ($this->handler->get($key)) { return $this->handler->decrement($key, $num); } else { $this->handler->set($key, 0); return 0; } } public function clear() { $this->handler->flush(); } }