enabled = $enabled; $this->server = $server; $this->port = $port; $this->prefix = $prefix; if ($enabled) { $this->memcache = new Memcached(); $this->memcache->addServer($this->server, $this->port); } $this->dummycache = []; } /** * Check if the memcache is actually setup or if it's a dummy shim. * @return bool true if memcache is enabled, false if only dummy cache enabled */ function enabled(): bool { return $this->enabled; } /** * Get the value for key. * @param string $key * @return value or false if not found */ function get(string $key) { if ($this->enabled) { $key = $this->prefix . $key; return $this->memcache->get($key); } else { if (!empty($this->dummycache[$key])) { return $this->dummycache[$key]; } return false; } } /** * Set the value of $key to $val * @param string $key * @param type $val * @param int $expires number of seconds (max 60*60*24*30) or UNIX timestamp for cache expiry * Default expiration of 0 means no expiration */ function set(string $key, $val, int $expires = 0) { if ($expires < 0) { $expires = 0; } if ($this->enabled) { $key = $this->prefix . $key; $this->memcache->set($key, $val, $expires); } else { $this->dummycache[$key] = $val; } } /** * Get the cache key prefix string * @return string */ function getPrefix(): string { return $this->prefix; } function getMemcached() { return $this->memcache; } }