-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikos M
committed
Oct 24, 2018
1 parent
16d20ae
commit f80adb9
Showing
17 changed files
with
735 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Unicache | ||
* An agnostic caching framework for PHP, Python, Node/JS | ||
* | ||
* @version: 1.0.0 | ||
* https://github.com/foo123/Unicache | ||
* | ||
**/ | ||
|
||
if ( !class_exists('UNICACHE_Cache', false) ) | ||
{ | ||
abstract class UNICACHE_Cache | ||
{ | ||
abstract function get( $key ); | ||
abstract function put( $key, $data, $ttl ); | ||
abstract function remove( $key ); | ||
} | ||
|
||
class UNICACHE_Factory | ||
{ | ||
const VERSION = '1.0.0'; | ||
public static function getCache( $config ) | ||
{ | ||
$backend = strtoupper($config['cacheType']); | ||
$cache = null; | ||
switch( $backend ) | ||
{ | ||
case 'FILE': | ||
require_once(dirname(__FILE__).'/adapters/UnicacheFile.php'); | ||
$cache = new UNICACHE_FileCache(); | ||
$cache->setCacheDir( $config['FILE']['cacheDir'] ); | ||
break; | ||
case 'APC': | ||
require_once(dirname(__FILE__).'/adapters/UnicacheApc.php'); | ||
$cache = new UNICACHE_APCCache(); | ||
break; | ||
case 'APCU': | ||
require_once(dirname(__FILE__).'/adapters/UnicacheApcu.php'); | ||
$cache = new UNICACHE_APCUCache(); | ||
break; | ||
case 'XCACHE': | ||
require_once(dirname(__FILE__).'/adapters/UnicacheXCache.php'); | ||
$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) | ||
{ | ||
$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'] ); | ||
break; | ||
default: | ||
// default in-memory cache | ||
require_once(dirname(__FILE__).'/adapters/UnicacheMemory.php'); | ||
$cache = new UNICACHE_MemoryCache(); | ||
break; | ||
} | ||
return $cache; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
class UNICACHE_APCCache extends UNICACHE_Cache | ||
{ | ||
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class UNICACHE_APCUCache extends UNICACHE_Cache | ||
{ | ||
public function get( $key ) | ||
{ | ||
$data = apcu_fetch( $key, $success ); | ||
return $success ? $data : false | ||
} | ||
|
||
public function put( $key, $data, $ttl ) | ||
{ | ||
|
||
return apcu_store( $key, $data, $ttl ); | ||
} | ||
|
||
public function remove( $key ) | ||
{ | ||
return apcu_delete( $key ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
class UNICACHE_FileCache extends UNICACHE_Cache | ||
{ | ||
private $cachedir = ''; | ||
|
||
public function put( $key, $data, $ttl ) | ||
{ | ||
// Opening the file in read/write mode | ||
$ch = fopen($this->getFileName($key),'a+'); | ||
if (!$ch) | ||
throw new \Exception('UNICACHE: Could not save to cache'); | ||
|
||
flock($ch,LOCK_EX); // exclusive lock, will get released when the file is closed | ||
fseek($ch,0); // go to the start of the file | ||
// truncate the file | ||
ftruncate($ch,0); | ||
|
||
// Serializing along with the TTL | ||
$data = serialize(array(time()+(int)$ttl,$data)); | ||
if (false === fwrite($ch,$data)) | ||
throw new \Exception('UNICACHE: Could not save to cache'); | ||
fclose($ch); | ||
} | ||
|
||
public function get( $key ) | ||
{ | ||
$filename = $this->getFileName($key); | ||
if (!file_exists($filename)) return false; | ||
$ch = fopen($filename,'r'); | ||
|
||
if (!$ch) return false; | ||
|
||
// Getting a shared lock | ||
flock($ch,LOCK_SH); | ||
|
||
$data = file_get_contents($filename); | ||
fclose($ch); | ||
|
||
$data = @unserialize($data); | ||
if (!$data) | ||
{ | ||
unlink($filename); | ||
return false; | ||
} | ||
|
||
if (time() > $data[0]) | ||
{ | ||
// Unlinking when the file was expired | ||
unlink($filename); | ||
return false; | ||
} | ||
return $data[1]; | ||
} | ||
|
||
public function remove( $key ) | ||
{ | ||
$filename = $this->getFileName($key); | ||
if (file_exists($filename)) | ||
return unlink($filename); | ||
else | ||
return false; | ||
} | ||
|
||
public function setCacheDir( $dir ) | ||
{ | ||
$this->cachedir = rtrim((string)$dir, '/\\'); | ||
if ( !(file_exists($this->cachedir) && is_dir($this->cachedir)) ) | ||
@mkdir($this->cachedir); | ||
} | ||
|
||
protected function getFileName( $key ) | ||
{ | ||
return $this->cachedir . DIRECTORY_SEPARATOR . md5($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
class UNICACHE_MemcachedCache extends UNICACHE_Cache | ||
{ | ||
|
||
// Memcache object | ||
private $connection; | ||
|
||
public function __construct() | ||
{ | ||
$this->connection = new MemCache( ); | ||
} | ||
|
||
public function put( $key, $data, $ttl ) | ||
{ | ||
return $this->connection->set( $key, $data, 0, $ttl); | ||
} | ||
|
||
public function get( $key ) | ||
{ | ||
return $this->connection->get( $key ); | ||
} | ||
|
||
public function remove( $key ) | ||
{ | ||
return $this->connection->delete( $key ); | ||
} | ||
|
||
public function addServer( $host, $port=11211, $weight=10 ) | ||
{ | ||
$this->connection->addServer( $host, $port, true, $weight ); | ||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
class UNICACHE_MemoryCache extends UNICACHE_Cache | ||
{ | ||
private $_cache = array(); | ||
|
||
public function put( $key, $data, $ttl ) | ||
{ | ||
$this->_cache[$key] = array(time()+(int)$ttl,$data); | ||
} | ||
|
||
public function get( $key ) | ||
{ | ||
|
||
if ( !isset($this->_cache[$key]) ) return false; | ||
|
||
$data = $this->_cache[$key]; | ||
if ( !$data ) return false; | ||
|
||
if ( time() > $data[0] ) return false; | ||
return $data[1]; | ||
} | ||
|
||
public function remove( $key ) | ||
{ | ||
if ( !isset($this->_cache[$key]) ) return false; | ||
unset($this->_cache[$key]); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
require_once(dirname(__FILE__).'/drivers/redis.php'); | ||
|
||
class UNICACHE_RedisCache extends UNICACHE_Cache | ||
{ | ||
|
||
// Lightweight Redis client | ||
private $redis; | ||
|
||
public function put( $key, $data, $ttl ) | ||
{ | ||
$data = serialize(array(time()+(int)$ttl, $data)); | ||
return $this->redis->cmd('SET', $key, $data)->cmd('EXPIRE', $key, (int)$ttl)->set(); | ||
} | ||
|
||
public function get( $key ) | ||
{ | ||
$data = $this->redis->cmd('GET', $key)->get(); | ||
if ( !$data ) return false; | ||
$data = @unserialize($data); | ||
if ( !$data ) return false; | ||
if ( time() > $data[0] ) | ||
{ | ||
$this->redis->cmd('DEL', $key)->set(); | ||
return false; | ||
} | ||
return $data[1]; | ||
} | ||
|
||
public function remove( $key ) | ||
{ | ||
return $this->redis->cmd('DEL', $key)->set(); | ||
} | ||
|
||
public function server( $host, $port=6379 ) | ||
{ | ||
$this->redis = new redis_cli( $host, $port ); | ||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class UNICACHE_XCache extends UNICACHE_Cache | ||
{ | ||
public function get( $key ) | ||
{ | ||
$data = xcache_get( $key ); | ||
return null !== $data ? $data : false | ||
} | ||
|
||
public function put( $key, $data, $ttl ) | ||
{ | ||
|
||
return xcache_set( $key, $data, $ttl ); | ||
} | ||
|
||
public function remove( $key ) | ||
{ | ||
return xcache_unset( $key ); | ||
} | ||
} |
Oops, something went wrong.