diff --git a/common/framework/drivers/cache/apc.php b/common/framework/drivers/cache/apc.php new file mode 100644 index 000000000..8b23b45e9 --- /dev/null +++ b/common/framework/drivers/cache/apc.php @@ -0,0 +1,156 @@ +_dir = \RX_BASEDIR . 'files/cache/store'; + if (!Storage::isDirectory($this->_dir)) + { + Storage::createDirectory($this->_dir); + } + } + + /** + * Check if the current cache driver is supported on this server. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public function isSupported() + { + return true; + } + + /** + * Validate cache settings. + * + * This method returns true on success and false on failure. + * + * @param mixed $config + * @return bool + */ + public function validateSettings($config) + { + return true; + } + + /** + * Get the value of a key. + * + * This method returns null if the key was not found. + * + * @param string $key + * @return mixed + */ + public function get($key) + { + $filename = $this->_getFilename($key); + $data = Storage::readPHPData($filename); + + if ($data === false) + { + return null; + } + elseif (!is_array($data) || count($data) < 2 || ($data[0] > 0 && $data[0] < time())) + { + Storage::delete($filename); + return null; + } + else + { + return $data[1]; + } + } + + /** + * Set the value to a key. + * + * This method returns true on success and false on failure. + * $ttl is measured in seconds. If it is zero, the key should not expire. + * + * @param string $key + * @param mixed $value + * @param int $ttl + * @return bool + */ + public function set($key, $value, $ttl) + { + return Storage::writePHPData($this->_getFilename($key), array($ttl ? (time() + $ttl) : 0, $value)); + } + + /** + * Delete a key. + * + * This method returns true on success and false on failure. + * If the key does not exist, it should return false. + * + * @param string $key + * @return bool + */ + public function delete($key) + { + return Storage::delete($this->_getFilename($key)); + } + + /** + * Check if a key exists. + * + * This method returns true on success and false on failure. + * + * @param string $key + * @return bool + */ + public function exists($key) + { + return $this->get($key) !== null; + } + + /** + * Increase the value of a key by $amount. + * + * If the key does not exist, this method assumes that the current value is zero. + * This method returns the new value. + * + * @param string $key + * @param int $amount + * @return int + */ + public function incr($key, $amount) + { + $value = intval($this->get($key)); + $success = $this->set($key, $value + $amount); + return $success ? ($value + $amount) : false; + } + + /** + * Decrease the value of a key by $amount. + * + * If the key does not exist, this method assumes that the current value is zero. + * This method returns the new value. + * + * @param string $key + * @param int $amount + * @return int + */ + public function decr($key, $amount) + { + return $this->incr($key, 0 - $amount); + } + + /** + * Clear all keys from the cache. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public function clear() + { + Storage::deleteDirectory($this->_dir); + } + + /** + * Get the filename to store a key. + * + * @param string $key + * @return string + */ + protected function _getFilename($key) + { + $hash = sha1($key); + return $this->_dir . '/' . substr($hash, 0, 2) . '/' . substr($hash, 2, 2) . '/' . $hash . '.php'; + } +} diff --git a/common/framework/drivers/cache/memcached.php b/common/framework/drivers/cache/memcached.php new file mode 100644 index 000000000..08eb54760 --- /dev/null +++ b/common/framework/drivers/cache/memcached.php @@ -0,0 +1,228 @@ +_conn = new \Memcached; + $this->_ext = 'Memcached'; + } + elseif (class_exists('\\Memcache')) + { + $this->_conn = new \Memcache; + $this->_ext = 'Memcache'; + } + else + { + return; + } + + foreach ($config as $url) + { + $info = parse_url($url); + if (isset($info['host']) && isset($info['port'])) + { + $this->_conn->addServer($info['host'], $info['port']); + } + } + } + + /** + * Check if the current cache driver is supported on this server. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public function isSupported() + { + return class_exists('\\Memcached') || class_exists('\\Memcache'); + } + + /** + * Validate cache settings. + * + * This method returns true on success and false on failure. + * + * @param mixed $config + * @return bool + */ + public function validateSettings($config) + { + if (class_exists('\\Memcached')) + { + $conn = new \Memcached; + $ext = 'Memcached'; + } + elseif (class_exists('\\Memcache')) + { + $conn = new \Memcache; + $ext = 'Memcache'; + } + else + { + return false; + } + + foreach ($config as $url) + { + $info = parse_url($url); + if (isset($info['host']) && isset($info['port'])) + { + $conn->addServer($info['host'], $info['port']); + } + } + + for ($i = 0; $i < 5; $i++) + { + $key = 'rhymix:test:' . md5($i); + $status = ($ext === 'Memcached') ? $conn->set($key, $i, 2) : $conn->set($key, $i, 0, 2); + if (!$status) return false; + } + return true; + } + + /** + * Get the value of a key. + * + * This method returns null if the key was not found. + * + * @param string $key + * @return mixed + */ + public function get($key) + { + $value = $this->_conn->get($key); + if ($value === false) + { + return null; + } + else + { + return $value; + } + } + + /** + * Set the value to a key. + * + * This method returns true on success and false on failure. + * $ttl is measured in seconds. If it is zero, the key should not expire. + * + * @param string $key + * @param mixed $value + * @param int $ttl + * @return bool + */ + public function set($key, $value, $ttl) + { + if ($this->_ext === 'Memcached') + { + return $this->_conn->set($key, $value, $ttl); + } + else + { + return $this->_conn->set($key, $value, MEMCACHE_COMPRESSED, $ttl); + } + } + + /** + * Delete a key. + * + * This method returns true on success and false on failure. + * If the key does not exist, it should return false. + * + * @param string $key + * @return bool + */ + public function delete($key) + { + return $this->_conn->delete($key); + } + + /** + * Check if a key exists. + * + * This method returns true on success and false on failure. + * + * @param string $key + * @return bool + */ + public function exists($key) + { + return $this->_conn->get($key) !== false; + } + + /** + * Increase the value of a key by $amount. + * + * If the key does not exist, this method assumes that the current value is zero. + * This method returns the new value. + * + * @param string $key + * @param int $amount + * @return int + */ + public function incr($key, $amount) + { + $result = $this->_conn->increment($key, $amount); + if ($result === false) + { + $this->set($key, $amount); + $result = $amount; + } + return $result; + } + + /** + * Decrease the value of a key by $amount. + * + * If the key does not exist, this method assumes that the current value is zero. + * This method returns the new value. + * + * @param string $key + * @param int $amount + * @return int + */ + public function decr($key, $amount) + { + $result = $this->_conn->decrement($key, $amount); + if ($result === false) + { + $this->set($key, 0 - $amount); + $result = 0 - $amount; + } + return $result; + } + + /** + * Clear all keys from the cache. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public function clear() + { + return $this->_conn->flush(); + } +} diff --git a/common/framework/drivers/cache/redis.php b/common/framework/drivers/cache/redis.php new file mode 100644 index 000000000..de304c853 --- /dev/null +++ b/common/framework/drivers/cache/redis.php @@ -0,0 +1,259 @@ +_conn = new \Redis; + foreach ($config as $url) + { + $info = parse_url($url); + if (isset($info['host']) && isset($info['port'])) + { + $this->_conn->connect($info['host'], $info['port'], 0.15); + if(isset($info['user']) || isset($info['pass'])) + { + $this->_conn->auth(isset($info['user']) ? $info['user'] : $info['pass']); + } + if(isset($info['path']) && $dbnum = intval(substr($info['path'], 1))) + { + $this->_conn->select($dbnum); + } + break; + } + } + $this->_conn = null; + } + catch (\RedisException $e) + { + $this->_conn = null; + } + } + + /** + * Check if the current cache driver is supported on this server. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public function isSupported() + { + return class_exists('\\Redis'); + } + + /** + * Validate cache settings. + * + * This method returns true on success and false on failure. + * + * @param mixed $config + * @return bool + */ + public function validateSettings($config) + { + try + { + $conn = new \Redis; + foreach ($config as $url) + { + $info = parse_url($url); + if (isset($info['host']) && isset($info['port'])) + { + $conn->connect($info['host'], $info['port'], 0.15); + if(isset($info['user']) || isset($info['pass'])) + { + $conn->auth(isset($info['user']) ? $info['user'] : $info['pass']); + } + if(isset($info['path']) && $dbnum = intval(substr($info['path'], 1))) + { + $conn->select($dbnum); + } + return true; + } + } + return false; + } + catch (\RedisException $e) + { + return false; + } + } + + /** + * Get the value of a key. + * + * This method returns null if the key was not found. + * + * @param string $key + * @return mixed + */ + public function get($key) + { + try + { + $value = $this->_conn->get($key); + } + catch (\RedisException $e) + { + return null; + } + + if ($value === false) + { + return null; + } + + $value = unserialize($value); + if ($value === false) + { + return null; + } + return $value; + } + + /** + * Set the value to a key. + * + * This method returns true on success and false on failure. + * $ttl is measured in seconds. If it is zero, the key should not expire. + * + * @param string $key + * @param mixed $value + * @param int $ttl + * @return bool + */ + public function set($key, $value, $ttl) + { + try + { + return $this->_conn->setex($key, $ttl, serialize($value)) ? true : false; + } + catch (\RedisException $e) + { + return false; + } + } + + /** + * Delete a key. + * + * This method returns true on success and false on failure. + * If the key does not exist, it should return false. + * + * @param string $key + * @return bool + */ + public function delete($key) + { + try + { + return $this->_conn->del($key) ? true : false; + } + catch (\RedisException $e) + { + return false; + } + } + + /** + * Check if a key exists. + * + * This method returns true on success and false on failure. + * + * @param string $key + * @return bool + */ + public function exists($key) + { + try + { + return $this->_conn->exists($key); + } + catch (\RedisException $e) + { + return false; + } + } + + /** + * Increase the value of a key by $amount. + * + * If the key does not exist, this method assumes that the current value is zero. + * This method returns the new value. + * + * @param string $key + * @param int $amount + * @return int + */ + public function incr($key, $amount) + { + try + { + return $this->_conn->incrBy($key, $amount); + } + catch (\RedisException $e) + { + return false; + } + } + + /** + * Decrease the value of a key by $amount. + * + * If the key does not exist, this method assumes that the current value is zero. + * This method returns the new value. + * + * @param string $key + * @param int $amount + * @return int + */ + public function decr($key, $amount) + { + try + { + return $this->_conn->decrBy($key, $amount); + } + catch (\RedisException $e) + { + return false; + } + } + + /** + * Clear all keys from the cache. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public function clear() + { + try + { + return $this->_conn->flushDB() ? true : false; + } + catch (\RedisException $e) + { + return false; + } + } +} diff --git a/common/framework/drivers/cache/wincache.php b/common/framework/drivers/cache/wincache.php new file mode 100644 index 000000000..769343298 --- /dev/null +++ b/common/framework/drivers/cache/wincache.php @@ -0,0 +1,156 @@ +