Skip to content

Commit

Permalink
v.1.1.0
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Nikos M committed Oct 26, 2018
1 parent f80adb9 commit bbbf11b
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 29 deletions.
78 changes: 64 additions & 14 deletions src/php/Unicache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
**/
Expand All @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion src/php/adapters/UnicacheApc.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@

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 );
}

public function put( $key, $data, $ttl )
{

return apc_store( $key, $data, $ttl );
}

public function remove( $key )
{
return apc_delete( $key );
}

public function clear( )
{
return false;
}
}
23 changes: 22 additions & 1 deletion src/php/adapters/UnicacheApcu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -10,12 +15,28 @@ public function get( $key )

public function put( $key, $data, $ttl )
{

return apcu_store( $key, $data, $ttl );
}

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;
}
}
27 changes: 24 additions & 3 deletions src/php/adapters/UnicacheFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

class UNICACHE_FileCache extends UNICACHE_Cache
{
public static function isSupported( )
{
return true;
}

private $cachedir = '';

public function put( $key, $data, $ttl )
Expand Down Expand Up @@ -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];
Expand All @@ -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 )
Expand Down
42 changes: 37 additions & 5 deletions src/php/adapters/UnicacheMemcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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;
}

}
19 changes: 17 additions & 2 deletions src/php/adapters/UnicacheMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand All @@ -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];
Expand All @@ -27,4 +36,10 @@ public function remove( $key )
unset($this->_cache[$key]);
return true;
}

public function remove( $key )
{
$this->_cache = array();
return true;
}
}
Loading

0 comments on commit bbbf11b

Please sign in to comment.