From bbbf11bdc9d9d3d92c0941c28b70f41c1b8b4974 Mon Sep 17 00:00:00 2001 From: Nikos M Date: Fri, 26 Oct 2018 14:48:47 +0300 Subject: [PATCH] v.1.1.0 * add cache::clear method to clear all cache, all keys * add Cache::isSupported static method and throw exception otherwise * use Memcache or Memcached extension for memcache cache adapter * minor changes --- src/php/Unicache.php | 78 +++++++++++++++++++++----- src/php/adapters/UnicacheApc.php | 11 +++- src/php/adapters/UnicacheApcu.php | 23 +++++++- src/php/adapters/UnicacheFile.php | 27 ++++++++- src/php/adapters/UnicacheMemcached.php | 42 ++++++++++++-- src/php/adapters/UnicacheMemory.php | 19 ++++++- src/php/adapters/UnicacheRedis.php | 20 ++++++- src/php/adapters/UnicacheXCache.php | 16 +++++- 8 files changed, 207 insertions(+), 29 deletions(-) diff --git a/src/php/Unicache.php b/src/php/Unicache.php index 9a4896d..f565f1b 100644 --- a/src/php/Unicache.php +++ b/src/php/Unicache.php @@ -3,7 +3,7 @@ * Unicache * An agnostic caching framework for PHP, Python, Node/JS * -* @version: 1.0.0 +* @version: 1.1.0 * https://github.com/foo123/Unicache * **/ @@ -15,51 +15,101 @@ abstract class UNICACHE_Cache abstract function get( $key ); abstract function put( $key, $data, $ttl ); abstract function remove( $key ); + abstract function clear( ); } class UNICACHE_Factory { - const VERSION = '1.0.0'; + const VERSION = '1.1.0'; public static function getCache( $config ) { - $backend = strtoupper($config['cacheType']); + $backend = isset($config['cacheType']) ? strtoupper((string)$config['cacheType']) : 'MEMORY'; $cache = null; switch( $backend ) { case 'FILE': require_once(dirname(__FILE__).'/adapters/UnicacheFile.php'); - $cache = new UNICACHE_FileCache(); - $cache->setCacheDir( $config['FILE']['cacheDir'] ); + if ( !UNICACHE_FileCache::isSupported() ) + { + throw new \Exception('UNICACHE: Cache "'.$backend.'" is NOT supported!'); + } + else + { + $cache = new UNICACHE_FileCache(); + $cache->setCacheDir( $config['FILE']['cacheDir'] ); + } break; case 'APC': require_once(dirname(__FILE__).'/adapters/UnicacheApc.php'); - $cache = new UNICACHE_APCCache(); + if ( !UNICACHE_APCCache::isSupported() ) + { + throw new \Exception('UNICACHE: Cache "'.$backend.'" is NOT supported!'); + } + else + { + $cache = new UNICACHE_APCCache(); + } break; case 'APCU': require_once(dirname(__FILE__).'/adapters/UnicacheApcu.php'); - $cache = new UNICACHE_APCUCache(); + if ( !UNICACHE_APCUCache::isSupported() ) + { + throw new \Exception('UNICACHE: Cache "'.$backend.'" is NOT supported!'); + } + else + { + $cache = new UNICACHE_APCUCache(); + } break; case 'XCACHE': require_once(dirname(__FILE__).'/adapters/UnicacheXCache.php'); - $cache = new UNICACHE_XCache(); + if ( !UNICACHE_XCache::isSupported() ) + { + throw new \Exception('UNICACHE: Cache "'.$backend.'" is NOT supported!'); + } + else + { + $cache = new UNICACHE_XCache(); + } break; case 'MEMCACHED': require_once(dirname(__FILE__).'/adapters/UnicacheMemcached.php'); - $cache = new UNICACHE_MemcachedCache(); - foreach ((array)$config['MEMCACHED']['servers'] as $srv) + if ( !UNICACHE_MemcachedCache::isSupported() ) { - $cache->addServer( $srv['host'], $srv['port'], $srv['weight'] ); + throw new \Exception('UNICACHE: Cache "'.$backend.'" is NOT supported!'); + } + else + { + $cache = new UNICACHE_MemcachedCache(); + foreach ((array)$config['MEMCACHED']['servers'] as $srv) + { + $cache->addServer( $srv['host'], $srv['port'], $srv['weight'] ); + } } break; case 'REDIS': require_once(dirname(__FILE__).'/adapters/UnicacheRedis.php'); - $cache = new UNICACHE_RedisCache(); - $cache->server( $config['REDIS']['server']['host'], $config['REDIS']['server']['port'] ); + if ( !UNICACHE_RedisCache::isSupported() ) + { + throw new \Exception('UNICACHE: Cache "'.$backend.'" is NOT supported!'); + } + else + { + $cache = new UNICACHE_RedisCache(); + $cache->server( $config['REDIS']['server']['host'], $config['REDIS']['server']['port'] ); + } break; default: // default in-memory cache require_once(dirname(__FILE__).'/adapters/UnicacheMemory.php'); - $cache = new UNICACHE_MemoryCache(); + if ( !UNICACHE_MemoryCache::isSupported() ) + { + throw new \Exception('UNICACHE: Cache "MEMORY" is NOT supported!'); + } + else + { + $cache = new UNICACHE_MemoryCache(); + } break; } return $cache; diff --git a/src/php/adapters/UnicacheApc.php b/src/php/adapters/UnicacheApc.php index 7fbbdb8..7c0fdde 100644 --- a/src/php/adapters/UnicacheApc.php +++ b/src/php/adapters/UnicacheApc.php @@ -2,6 +2,11 @@ class UNICACHE_APCCache extends UNICACHE_Cache { + public static function isSupported( ) + { + return (function_exists('apc_fetch') && function_exists('apc_store') && function_exists('apc_delete')); + } + public function get( $key ) { return apc_fetch( $key ); @@ -9,7 +14,6 @@ public function get( $key ) public function put( $key, $data, $ttl ) { - return apc_store( $key, $data, $ttl ); } @@ -17,4 +21,9 @@ public function remove( $key ) { return apc_delete( $key ); } + + public function clear( ) + { + return false; + } } diff --git a/src/php/adapters/UnicacheApcu.php b/src/php/adapters/UnicacheApcu.php index 444fee0..39af697 100644 --- a/src/php/adapters/UnicacheApcu.php +++ b/src/php/adapters/UnicacheApcu.php @@ -2,6 +2,11 @@ class UNICACHE_APCUCache extends UNICACHE_Cache { + public static function isSupported( ) + { + return (function_exists('apcu_fetch') && function_exists('apcu_store') && function_exists('apcu_delete')); + } + public function get( $key ) { $data = apcu_fetch( $key, $success ); @@ -10,7 +15,6 @@ public function get( $key ) public function put( $key, $data, $ttl ) { - return apcu_store( $key, $data, $ttl ); } @@ -18,4 +22,21 @@ public function remove( $key ) { return apcu_delete( $key ); } + + public function clear( ) + { + if (class_exists('APCuIterator', false)) + { + // http://php.net/manual/en/apcuiterator.construct.php + apcu_delete(new APCuIterator(null, APC_ITER_NONE)); + return true; + } + + $cache = @apcu_cache_info(); // Raises warning by itself already + foreach ($cache['cache_list'] as $key) + { + apcu_delete($key['info']); + } + return true; + } } diff --git a/src/php/adapters/UnicacheFile.php b/src/php/adapters/UnicacheFile.php index 7b086e3..64a7807 100644 --- a/src/php/adapters/UnicacheFile.php +++ b/src/php/adapters/UnicacheFile.php @@ -2,6 +2,11 @@ class UNICACHE_FileCache extends UNICACHE_Cache { + public static function isSupported( ) + { + return true; + } + private $cachedir = ''; public function put( $key, $data, $ttl ) @@ -40,14 +45,14 @@ public function get( $key ) $data = @unserialize($data); if (!$data) { - unlink($filename); + @unlink($filename); return false; } if (time() > $data[0]) { // Unlinking when the file was expired - unlink($filename); + @unlink($filename); return false; } return $data[1]; @@ -57,16 +62,32 @@ public function remove( $key ) { $filename = $this->getFileName($key); if (file_exists($filename)) - return unlink($filename); + return @unlink($filename); else return false; } + public function clear( ) + { + if ($handle = opendir($this->cachedir)) + { + while (false !== ($file=readdir($handle))) + { + if( is_file($file) ) + @unlink($file); + } + closedir($handle); + return true; + } + return false; + } + public function setCacheDir( $dir ) { $this->cachedir = rtrim((string)$dir, '/\\'); if ( !(file_exists($this->cachedir) && is_dir($this->cachedir)) ) @mkdir($this->cachedir); + return $this; } protected function getFileName( $key ) diff --git a/src/php/adapters/UnicacheMemcached.php b/src/php/adapters/UnicacheMemcached.php index 0cc46aa..0fd9c76 100644 --- a/src/php/adapters/UnicacheMemcached.php +++ b/src/php/adapters/UnicacheMemcached.php @@ -2,18 +2,39 @@ class UNICACHE_MemcachedCache extends UNICACHE_Cache { - + public static function isSupported( ) + { + return (extension_loaded('memcached') || extension_loaded('memcache')); + //return class_exists('Memcached', false) || class_exists('Memcache', false); + } + // Memcache object private $connection; public function __construct() { - $this->connection = new MemCache( ); + $this->connection = null; + if (class_exists('Memcached', false)) + { + $this->connection = new \Memcached(); + } + elseif (class_exists('Memcache', false)) + { + $this->connection = new \Memcache(); + } } public function put( $key, $data, $ttl ) { - return $this->connection->set( $key, $data, 0, $ttl); + if (get_class($this->connection) === 'Memcached') + { + return $this->connection->set($key, $value, $ttl); + } + elseif (get_class($this->connection) === 'Memcache') + { + return $this->connection->set($key, $value, 0, $ttl); + } + return false; } public function get( $key ) @@ -26,10 +47,21 @@ public function remove( $key ) return $this->connection->delete( $key ); } + public function clear( ) + { + return $this->connection->flush( ); + } + public function addServer( $host, $port=11211, $weight=10 ) { - $this->connection->addServer( $host, $port, true, $weight ); + if (get_class($this->connection) === 'Memcache') + { + $this->connection->addServer( $host, $port, true, $weight ); + } + else + { + $this->connection->addServer( $host, $port, $weight ); + } return $this; } - } diff --git a/src/php/adapters/UnicacheMemory.php b/src/php/adapters/UnicacheMemory.php index 4a869af..b7239d1 100644 --- a/src/php/adapters/UnicacheMemory.php +++ b/src/php/adapters/UnicacheMemory.php @@ -2,7 +2,17 @@ class UNICACHE_MemoryCache extends UNICACHE_Cache { - private $_cache = array(); + public static function isSupported( ) + { + return true; + } + + private $_cache; + + public function __construct() + { + $this->_cache = array(); + } public function put( $key, $data, $ttl ) { @@ -11,7 +21,6 @@ public function put( $key, $data, $ttl ) public function get( $key ) { - if ( !isset($this->_cache[$key]) ) return false; $data = $this->_cache[$key]; @@ -27,4 +36,10 @@ public function remove( $key ) unset($this->_cache[$key]); return true; } + + public function remove( $key ) + { + $this->_cache = array(); + return true; + } } diff --git a/src/php/adapters/UnicacheRedis.php b/src/php/adapters/UnicacheRedis.php index f27c218..e6a1aaf 100644 --- a/src/php/adapters/UnicacheRedis.php +++ b/src/php/adapters/UnicacheRedis.php @@ -3,7 +3,12 @@ class UNICACHE_RedisCache extends UNICACHE_Cache { - + public static function isSupported( ) + { + //return extension_loaded('redis'); + return true; + } + // Lightweight Redis client private $redis; @@ -32,10 +37,21 @@ public function remove( $key ) return $this->redis->cmd('DEL', $key)->set(); } + public function clear( ) + { + $keys = $this->redis->cmd('KEYS', '*')->get(); + if ( !$keys ) return true; + foreach($keys as $key) + { + $this->redis->cmd('DEL', $key); + } + $this->redis->set(); + return true; + } + public function server( $host, $port=6379 ) { $this->redis = new redis_cli( $host, $port ); return $this; } - } diff --git a/src/php/adapters/UnicacheXCache.php b/src/php/adapters/UnicacheXCache.php index e81ea75..36bd187 100644 --- a/src/php/adapters/UnicacheXCache.php +++ b/src/php/adapters/UnicacheXCache.php @@ -2,6 +2,11 @@ class UNICACHE_XCache extends UNICACHE_Cache { + public static function isSupported( ) + { + return (function_exists('xcache_get') && function_exists('xcache_set') && function_exists('xcache_unset')); + } + public function get( $key ) { $data = xcache_get( $key ); @@ -10,7 +15,6 @@ public function get( $key ) public function put( $key, $data, $ttl ) { - return xcache_set( $key, $data, $ttl ); } @@ -18,4 +22,14 @@ public function remove( $key ) { return xcache_unset( $key ); } + + public function clear( ) + { + $max = xcache_count(XC_TYPE_VAR); + for ($i=0; $i<$max; $i++) + { + xcache_clear_cache(XC_TYPE_VAR, $i); + } + return true; + } }