Initial commit

This commit is contained in:
Wruczek
2016-06-28 23:18:59 +02:00
parent e321e10551
commit e49c2c27ad
390 changed files with 22561 additions and 0 deletions

View File

@ -0,0 +1,15 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
require_once __DIR__."/phpFastCache/phpFastCache.php";

View File

@ -0,0 +1,3 @@
order deny,allow
deny from all
allow from 127.0.0.1

View File

@ -0,0 +1,173 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache;
use phpFastCache\Core\phpFastCache;
use phpFastCache\Core\DriverAbstract;
/**
* Class CacheManager
* @package phpFastCache
*
* @method static DriverAbstract Apc() Apc($config = array()) Return a driver "apc" instance
* @method static DriverAbstract Cookie() Cookie($config = array()) Return a driver "cookie" instance
* @method static DriverAbstract Files() Files($config = array()) Return a driver "files" instance
* @method static DriverAbstract Memcache() Memcache($config = array()) Return a driver "memcache" instance
* @method static DriverAbstract Memcached() Memcached($config = array()) Return a driver "memcached" instance
* @method static DriverAbstract Predis() Predis($config = array()) Return a driver "predis" instance
* @method static DriverAbstract Redis() Redis($config = array()) Return a driver "redis" instance
* @method static DriverAbstract Sqlite() Sqlite($config = array()) Return a driver "sqlite" instance
* @method static DriverAbstract Ssdb() Ssdb($config = array()) Return a driver "ssdb" instance
* @method static DriverAbstract Wincache() Wincache($config = array()) Return a driver "wincache" instance
* @method static DriverAbstract Xcache() Xcache($config = array()) Return a driver "xcache" instance
*
*/
class CacheManager
{
public static $instances = array();
public static $memory = array();
public static $hit = array();
/**
* @param string $storage
* @param array $config
* @return DriverAbstract
*/
public static function getInstance($storage = 'auto', $config = array())
{
$storage = strtolower($storage);
if (empty($config)) {
$config = phpFastCache::$config;
}
if (!isset($config[ 'cache_method' ])) {
$config[ 'cache_method' ] = phpFastCache::$config[ 'cache_method' ];
}
if (!isset($config[ 'limited_memory_each_object' ])) {
$config[ 'limited_memory_each_object' ] = phpFastCache::$config[ 'limited_memory_each_object' ];
}
if (isset(phpFastCache::$config[ 'overwrite' ]) && !in_array(phpFastCache::$config[ 'overwrite' ], array('auto', ''), true)) {
phpFastCache::$config[ 'storage' ] = phpFastCache::$config[ 'overwrite' ];
$storage = phpFastCache::$config[ 'overwrite' ];
} else if (isset(phpFastCache::$config[ 'storage' ]) && !in_array(phpFastCache::$config[ 'storage' ], array('auto', ''), true)) {
$storage = phpFastCache::$config[ 'storage' ];
} else if (in_array($storage, array('auto', ''), true)) {
$storage = phpFastCache::getAutoClass($config);
}
// echo $storage."<br>";
$instance = md5(serialize($config) . $storage);
if (!isset(self::$instances[ $instance ]) || is_null(self::$instances[ $instance ])) {
$class = '\phpFastCache\Drivers\\' . $storage;
$config[ 'storage' ] = $storage;
$config[ 'instance' ] = $instance;
$config[ 'class' ] = $class;
if (!isset(self::$memory[ $instance ])) {
self::$memory[ $instance ] = array();
}
if (!isset(self::$hit[ $instance ])) {
self::$hit[ $instance ] = array(
"class" => $class,
"storage" => $storage,
"data" => array(),
);
if ($config[ 'cache_method' ] == 4) {
register_shutdown_function('phpFastCache\CacheManager::cleanCachingMethod', null);
}
}
self::$instances[ $instance ] = new $class($config);
}
return self::$instances[ $instance ];
}
/**
* Setup Method
* @param string $string | traditional(normal), memory (fast), phpfastcache (fastest)
*/
public static function CachingMethod($string = "phpFastCache")
{
$string = strtolower($string);
if (in_array($string, array("normal", "traditional"))) {
phpFastCache::$config[ 'cache_method' ] = 1;
} else if (in_array($string, array("fast", "memory"))) {
phpFastCache::$config[ 'cache_method' ] = 2;
} else if (in_array($string, array("fastest", "phpfastcache"))) {
phpFastCache::$config[ 'cache_method' ] = 3;
} else if (in_array($string, array("superfast", "phpfastcachex"))) {
phpFastCache::$config[ 'cache_method' ] = 4;
}
}
/**
* CacheManager::Files();
* CacheManager::Memcached();
* CacheManager::get($keyword);
* CacheManager::set(), touch, other @method supported
*/
public static function __callStatic($name, $arguments)
{
$driver = strtolower($name);
if (!isset(self::$instances[ 'loaded' ][ $driver ]) && class_exists("\\phpFastCache\\Drivers\\{$driver}")) {
self::$instances[ 'loaded' ][ $driver ] = true;
}
if (isset(self::$instances[ 'loaded' ][ $driver ])) {
return self::getInstance($name, (isset($arguments[ 0 ]) ? $arguments[ 0 ] : array()));
} else {
return call_user_func_array(array(self::getInstance(), $name), $arguments);
}
}
/**
* Shortcut to phpFastCache::setup()
*/
public static function setup($name, $value = '')
{
phpFastCache::setup($name, $value);
}
/**
* @param string $instance
*/
public static function cleanCachingMethod($instance = null)
{
if (is_null($instance)) {
foreach (self::$instances as $instance => $data) {
self::__CleanCachingMethod($instance);
unset($data);
}
} else {
self::__CleanCachingMethod($instance);
}
}
/**
* @param string $instance
*/
protected static function __CleanCachingMethod($instance)
{
if(is_array(self::$memory[ $instance ]) && !empty(self::$memory[ $instance ])) {
$old = self::$instances[$instance]->config['cache_method'];
self::$instances[$instance]->config['cache_method'] = 1;
foreach (self::$memory[$instance] as $keyword => $object) {
self::$instances[$instance]->set($keyword, $object['value'], $object['expired_in']);
}
self::$instances[$instance]->config['cache_method'] = $old;
self::$memory[$instance] = array();
}
}
}

View File

@ -0,0 +1,846 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Core;
use phpFastCache\Exceptions\phpFastCacheDriverException;
use phpFastCache\CacheManager;
/**
* Class DriverAbstract
* @package phpFastCache\Core
*/
abstract class DriverAbstract implements DriverInterface
{
/**
* @var array
*/
public $extension_dir = '_extensions';
/**
* @var array
*/
public $tmp = array();
/**
* @var array default options, this will be merge to Driver's Options
*/
public $config = array();
/**
* @var bool
*/
public $fallback = false;
/**
* @var
*/
public $instant;
public function __destruct()
{
// clean up the memory and don't want for PHP clean for caching method "phpfastcache"
if (isset($this->config[ 'instance' ]) && (int)$this->config[ 'cache_method' ] === 3) {
CacheManager::cleanCachingMethod($this->config[ 'instance' ]);
}
}
/**
* @param $keyword
* @return string
*/
protected function encodeFilename($keyword)
{
// return trim(trim(preg_replace('/[^a-zA-Z0-9]+/', '_', $keyword), '_'));
// return rtrim(base64_encode($keyword), '=');
return md5($keyword);
}
/**
* Basic Functions
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool|null
*/
public function set($keyword, $value = '', $time = 0, $option = array())
{
/**
* Infinity Time
* Khoa. B
*/
if ((int)$time <= 0) {
/**
* 5 years, however memcached or memory cached will gone when u restart it
* just recommended for sqlite. files
*/
$time = 3600 * 24 * 365 * 5;
}
/**
* Temporary disabled phpFastCache::$disabled = true
* Khoa. B
*/
if (phpFastCache::$disabled === true) {
return false;
}
$object = array(
"value" => $value,
"write_time" => time(),
"expired_in" => $time,
"expired_time" => time() + (int)$time,
"size" => (is_array($value) || is_object($value)) ? strlen(serialize($value)) : strlen((String)$value),
);
// handle search
if (isset($this->config[ 'allow_search' ]) && $this->config[ 'allow_search' ] == true) {
$option[ 'tags' ][] = "search";
}
// handle tags
if (isset($option[ 'tags' ])) {
$this->_handleTags($keyword, $time, $option[ 'tags' ]);
}
// handle method
if ((int)$this->config[ 'cache_method' ] > 1 && isset($object[ 'size' ]) && (int)$object[ 'size' ] <= (int)$this->config[ 'limited_memory_each_object' ]) {
CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ] = $object;
if (in_array((int)$this->config[ 'cache_method' ], array(3, 4))) {
return true;
}
}
$this->_hit("set", 1);
return $this->driver_set($keyword, $object, $time, $option);
}
/**
* @param $keyword
* @param array $option
* @return mixed
*/
public function get($keyword, $option = array())
{
/**
* Temporary disabled phpFastCache::$disabled = true
* Khoa. B
*/
if (phpFastCache::$disabled === true) {
return null;
}
// handle method
if ((int)$this->config[ 'cache_method' ] > 1) {
if (isset(CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ])) {
$object = CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ];
}
}
if (!isset($object)) {
$this->_hit("get", 1);
$object = $this->driver_get($keyword, $option);
// handle method
if ((int)$this->config[ 'cache_method' ] > 1 && isset($object[ 'size' ]) && (int)$object[ 'size' ] <= (int)$this->config[ 'limited_memory_each_object' ]) {
CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ] = $object;
}
// end handle method
}
if ($object == null) {
return null;
}
$value = isset($object[ 'value' ]) ? $object[ 'value' ] : null;
return isset($option[ 'all_keys' ]) && $option[ 'all_keys' ] ? $object : $value;
}
/**
* @param $keyword
* @param array $option
* @return null|object
*/
public function getInfo($keyword, $option = array())
{
if ((int)$this->config[ 'cache_method' ] > 1) {
if (isset(CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ])) {
$object = CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ];
}
}
if (!isset($object)) {
$object = $this->driver_get($keyword, $option);
}
if ($object == null) {
return null;
}
return $object;
}
/**
* @param $keyword
* @param array $option
* @return mixed
*/
public function delete($keyword, array $option = array())
{
// handle method
if ((int)$this->config[ 'cache_method' ] > 1) {
// use memory
unset(CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ]);
}
// end handle method
return $this->driver_delete($keyword, $option);
}
/**
* @param array $option
* @return mixed
*/
public function stats(array $option = array())
{
return $this->driver_stats($option);
}
/**
* @param array $option
* @return mixed
*/
public function clean(array $option = array())
{
// handle method
if ((int)$this->config[ 'cache_method' ] > 1) {
// use memory
CacheManager::$memory[ $this->config[ 'instance' ] ] = array();
}
// end handle method
return $this->driver_clean($option);
}
/**
* @param $keyword
* @return bool
*/
public function isExisting($keyword)
{
if (method_exists($this, 'driver_isExisting')) {
return $this->driver_isExisting($keyword);
}
$data = $this->get($keyword);
if ($data == null) {
return false;
} else {
return true;
}
}
/**
* Searches though the cache for keys that match the given query.
* @param $query_as_regex_or_string
* @param bool $search_in_value
* @return mixed
* @throws phpFastCacheDriverException
*/
public function search($query_as_regex_or_string, $search_in_value = false)
{
if ($this->config[ 'allow_search' ] != true) {
throw new phpFastCacheDriverException('Please setup allow_search = true');
} else {
$list = $this->getTags("search", $search_in_value);
$tmp = explode("/", $query_as_regex_or_string, 2);
$regex = isset($tmp[ 1 ]) ? true : false;
$return_list = array();
foreach ($list as $tag) {
foreach ($tag as $keyword => $value) {
$gotcha = false;
if ($search_in_value == true) {
$value = $this->get($keyword);
}
if ($regex == true && $gotcha == false) { // look in keyword
if (preg_match($query_as_regex_or_string, $keyword)) {
$return_list[ $keyword ] = $value;
$gotcha = true;
}
}
if ($gotcha == false) {
if (strpos($keyword, $query_as_regex_or_string) !== false) {
$return_list[ $keyword ] = $value;
$gotcha = true;
}
}
if ($search_in_value == true && $gotcha == false) { // value search
if ($regex == true && $gotcha == false) {
if (preg_match($query_as_regex_or_string, $value)) {
$return_list[ $keyword ] = $value;
$gotcha = true;
}
}
if ($gotcha == false) {
if (strpos($value, $query_as_regex_or_string) !== false) {
$return_list[ $keyword ] = $value;
$gotcha = true;
}
}
}
} // each tags loop
} // end foreach
return $return_list;
}
}
/**
* @param $keyword
* @param int $step
* @param array $option
* @return bool
*/
public function increment($keyword, $step = 1, array $option = array())
{
$object = $this->get($keyword, array('all_keys' => true));
if ($object == null) {
return false;
} else {
$value = (int)$object[ 'value' ] + (int)$step;
$time = $object[ 'expired_time' ] - time();
$this->set($keyword, $value, $time, $option);
return true;
}
}
/**
* @param $keyword
* @param int $step
* @param array $option
* @return bool
*/
public function decrement($keyword, $step = 1, array $option = array())
{
$object = $this->get($keyword, array('all_keys' => true));
if ($object == null) {
return false;
} else {
$value = (int)$object[ 'value' ] - (int)$step;
$time = $object[ 'expired_time' ] - time();
$this->set($keyword, $value, $time, $option);
return true;
}
}
/**
* Extend more time
* @param $keyword
* @param int $time
* @param array $option
* @return bool
*/
public function touch($keyword, $time = 300, array $option = array())
{
$object = $this->get($keyword, array('all_keys' => true));
if ($object == null) {
return false;
} else {
$value = $object[ 'value' ];
$time = $object[ 'expired_time' ] - time() + $time;
$this->set($keyword, $value, $time, $option);
return true;
}
}
/**
* Other Functions Built-int for phpFastCache since 1.3
*/
/**
* @param array $list
*/
public function setMulti(array $list = array())
{
foreach ($list as $array) {
$this->set($array[ 0 ], isset($array[ 1 ]) ? $array[ 1 ] : 0,
isset($array[ 2 ]) ? $array[ 2 ] : array());
}
}
/**
* @param array $list
* @return array
*/
public function getMulti(array $list = array())
{
$res = array();
foreach ($list as $array) {
$name = $array[ 0 ];
$res[ $name ] = $this->get($name,
isset($array[ 1 ]) ? $array[ 1 ] : array());
}
return $res;
}
/**
* @param array $list
* @return array
*/
public function getInfoMulti(array $list = array())
{
$res = array();
foreach ($list as $array) {
$name = $array[ 0 ];
$res[ $name ] = $this->getInfo($name,
isset($array[ 1 ]) ? $array[ 1 ] : array());
}
return $res;
}
/**
* @param array $list
* @param array $option
*/
public function deleteMulti(array $list = array(), array $option = array())
{
foreach ($list as $item) {
if (is_array($item) && count($item) === 2) {
$this->delete($item[ 0 ], $item[ 1 ]);
}
}
}
/**
* @param array $list
* @return array
*/
public function isExistingMulti(array $list = array())
{
$res = array();
foreach ($list as $array) {
$name = $array[ 0 ];
$res[ $name ] = $this->isExisting($name);
}
return $res;
}
/**
* @param array $list
* @return array
*/
public function incrementMulti(array $list = array())
{
$res = array();
foreach ($list as $array) {
$name = $array[ 0 ];
$res[ $name ] = $this->increment($name, $array[ 1 ],
isset($array[ 2 ]) ? $array[ 2 ] : array());
}
return $res;
}
/**
* @param array $list
* @return array
*/
public function decrementMulti(array $list = array())
{
$res = array();
foreach ($list as $array) {
$name = $array[ 0 ];
$res[ $name ] = $this->decrement($name, $array[ 1 ],
isset($array[ 2 ]) ? $array[ 2 ] : array());
}
return $res;
}
/**
* @param array $list
* @return array
*/
public function touchMulti(array $list = array())
{
$res = array();
foreach ($list as $array) {
$name = $array[ 0 ];
$res[ $name ] = $this->touch($name, $array[ 1 ],
isset($array[ 2 ]) ? $array[ 2 ] : array());
}
return $res;
}
/**
* @param $config_name
* @param string $value
*/
public function setup($config_name, $value = '')
{
/*
* Config for class
*/
if (is_array($config_name)) {
$this->config = array_merge($this->config, $config_name);
} else {
$this->config[ $config_name ] = $value;
}
}
/**
* @param int $time
*/
public function autoCleanExpired($time = 3600)
{
}
/**
* Magic methods
*/
/**
* @param $name
* @return mixed
*/
public function __get($name)
{
return $this->get($name);
}
/**
* @param $name
* @param $v
* @return bool|null
* @throws \Exception
*/
public function __set($name, $v)
{
if (isset($v[ 1 ]) && is_scalar($v[ 1 ])) {
return $this->set($name, $v[ 0 ], $v[ 1 ],
isset($v[ 2 ]) ? $v[ 2 ] : array());
} else {
throw new phpFastCacheDriverException("Example ->$name = array('VALUE', 300);", 98);
}
}
/**
* Base Methods
*/
/**
* @return mixed
*/
protected function backup()
{
return phpFastCache(phpFastCache::$config[ 'fallback' ]);
}
/**
* @param $name
* @return void
*/
protected function required_extension($name)
{
require_once(__DIR__ . '/../' . $this->extension_dir . '/' . $name . '.' . PHP_EXT);
}
/**
* @param $file
* @return string
* @throws \Exception
*/
protected function readfile($file)
{
if (function_exists('file_get_contents')) {
return file_get_contents($file);
} else {
$string = '';
$file_handle = @fopen($file, 'r');
if (!$file_handle) {
throw new phpFastCacheDriverException("Can't Read File", 96);
}
while (!feof($file_handle)) {
$line = fgets($file_handle);
$string .= $line;
}
fclose($file_handle);
return $string;
}
}
/**
* return PATH for Files & PDO only
* @param bool $create_path
* @return string
* @throws \Exception
*/
public function getPath($create_path = false)
{
return phpFastCache::getPath($create_path, $this->config);
}
/**
* Object for Files & SQLite
* @param $data
* @return string
*/
protected function encode($data)
{
return serialize($data);
}
/**
* @param $value
* @return mixed
*/
protected function decode($value)
{
$x = @unserialize($value);
if ($x == false) {
return $value;
} else {
return $x;
}
}
/**
* Check phpModules or CGI
* @return bool
*/
protected function isPHPModule()
{
return phpFastCache::isPHPModule();
}
/**
* @param $class
* @return bool
*/
protected function isExistingDriver($class)
{
return class_exists("\\phpFastCache\\Drivers\\{$class}");
}
/**
* @return int
*/
protected function __setChmodAuto()
{
return phpFastCache::__setChmodAuto($this->config);
}
/**
* @param $tag
* @return string
*/
protected function _getTagName($tag)
{
return "__tag__" . $tag;
}
/**
* @return \phpFastCache\Core\DriverAbstract
*/
protected function _tagCaching()
{
return CacheManager::Sqlite(
array(
"path" => $this->config[ 'path' ],
"cache_method" => 3,
)
);
}
/**
* @param string $keyword
* @param mixed $value
* @param integer $time
* @param array $tags
* @param array $option | $option = array("tags" => array("a","b","c")
* @return mixed
*/
public function setTags($keyword, $value = '', $time = 0, $tags = array(), $option = array())
{
if (!is_array($tags)) {
$tags = array($tags);
}
$option[ 'tags' ] = $tags;
return $this->set($keyword, $value, $time, $option);
}
protected function _handleTags($keyword, $time, $tags)
{
foreach ($tags as $tag) {
$list = $this->_tagCaching()->get($this->_getTagName($tag));
if (is_null($list)) {
$list = array();
}
$list[ $keyword ] = time() + $time;
$this->_tagCaching()->set($this->_getTagName($tag), $list, 3600 * 24 * 30);
}
}
/**
* @param array $tags
* @param bool $return_content
* @param array $option | $option = array("tags" => array("a","b","c")
* @return array
*/
public function getTags($tags = array(), $return_content = true, $option = array())
{
if (!is_array($tags)) {
$tags = array($tags);
}
$keywords = array();
$tmp = 0;
foreach ($tags as $tag) {
$list = $this->_tagCaching()->get($this->_getTagName($tag));
$list_return = array();
if (is_null($list)) {
$list = array();
}
foreach ($list as $keyword => $time) {
if ($time <= time()) {
unset($list[ $keyword ]);
} else {
if ($tmp < $time) {
$tmp = $time;
}
if ($return_content == true) {
$list_return[ $keyword ] = $this->get($keyword);
} else {
$list_return[ $keyword ] = $time;
}
}
}
$this->_tagCaching()->set($this->_getTagName($tag), $list, $tmp);
$keywords[ $tag ] = $list_return;
}
return $keywords;
}
/**
* @param array $tags | array("a","b","c")
* @param int $time
* @param array $options
* @return mixed
* @internal param array $option | $option = array("tags" => array("a","b","c")
*/
public function touchTags($tags = array(), $time = 300, $options = array())
{
if (!is_array($tags)) {
$tags = array($tags);
}
$lists = $this->getTags($tags);
foreach ($lists as $tag => $keywords) {
foreach ($keywords as $keyword => $time) {
$this->touch($keyword, $time, $options);
}
}
return true;
}
/**
* @param array $tags | array("a","b","c")
* @param array $option | $option = array("tags" => array("a","b","c")
* @return mixed
*/
public function deleteTags($tags = array(), $option = array())
{
if (!is_array($tags)) {
$tags = array($tags);
}
$lists = $this->getTags($tags);
foreach ($lists as $tag => $keywords) {
foreach ($keywords as $keyword => $time) {
$this->delete($keyword, $option);
}
}
return true;
}
/**
* @param array $tags | array("a","b","c")
* @param integer
* @param array $option | $option = array("tags" => array("a","b","c")
* @return mixed
*/
public function incrementTags($tags = array(), $step = 1, $option = array())
{
if (!is_array($tags)) {
$tags = array($tags);
}
$lists = $this->getTags($tags);
foreach ($lists as $tag => $keywords) {
foreach ($keywords as $keyword => $time) {
$this->increment($keyword, $step, $option);
}
}
return true;
}
/**
* @param array $tags | array("a","b","c")
* @param integer
* @param array $option | $option = array("tags" => array("a","b","c")
* @return mixed
*/
public function decrementTags($tags = array(), $step = 1, $option = array())
{
if (!is_array($tags)) {
$tags = array($tags);
}
$lists = $this->getTags($tags);
foreach ($lists as $tag => $keywords) {
foreach ($keywords as $keyword => $time) {
$this->decrement($keyword, $step, $option);
}
}
return true;
}
/**
* @param $value
*/
protected function _kbdebug($value)
{
/*
echo "<pre>";
print_r($value);
echo "</pre>";
*/
}
public function _hit($index, $step = 1)
{
$instance = $this->config[ 'instance' ];
$current = isset(CacheManager::$hit[ $instance ][ 'data' ][ $index ]) ? CacheManager::$hit[ $instance ][ 'data' ][ $index ] : 0;
CacheManager::$hit[ $instance ][ 'data' ][ $index ] = $current + ($step);
}
}

View File

@ -0,0 +1,86 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Core;
/**
* Interface DriverInterface
* @package phpFastCache\Core
*/
interface DriverInterface
{
/**
* Check if this Cache driver is available for server or not
* phpFastCache_driver constructor.
* @param array $config
*/
public function __construct($config = array());
/**
* @return mixed
*/
public function checkdriver();
/**
* Set a obj to cache
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return mixed
*/
public function driver_set(
$keyword,
$value = '',
$time = 300,
$option = array()
);
/**
* Return null or value of cache
* @param $keyword
* @param array $option
* @return mixed
*/
public function driver_get($keyword, $option = array());
/**
* Show stats of caching
* Return array("info","size","data")
* @param array $option
* @return mixed
*/
public function driver_stats($option = array());
/**
* Delete a cache
* @param $keyword
* @param array $option
* @return mixed
*/
public function driver_delete($keyword, $option = array());
/**
* Clean up whole cache
* @param array $option
* @return mixed
*/
public function driver_clean($option = array());
/**
* @param $config_name
* @param string $value
*/
public function setup($config_name, $value = '');
}

View File

@ -0,0 +1,354 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Core;
use phpFastCache\CacheManager;
use phpFastCache\Exceptions\phpFastCacheCoreException;
use phpFastCache\Exceptions\phpFastCacheDriverException;
/**
* Class phpFastCache
* @package phpFastCache\Core
*
* Handle methods using annotations for IDE
* because they're handled by __call()
* Check out DriverInterface to see all
* the drivers methods magically implemented
*
* @method get() driver_get($keyword, $option = array()) Return null or value of cache
* @method set() driver_set($keyword, $value = '', $time = 300, $option = array()) Set a obj to cache
* @method delete() delete(string $keyword) Delete key from cache
* @method clean() clean($option = array()) Clean up whole cache
* @method checkdriver() checkdriver() Delete key from cache
* @method stats() stats($option = array()) Show stats of caching
* @method systemInfo() systemInfo() Return System Information
*
*/
class phpFastCache
{
/**
* @var bool
*/
public static $disabled = false;
/**
* @var array
*/
public static $config = array(
'storage' => '', // blank for auto
'default_chmod' => 0777, // 0777 , 0666, 0644
'overwrite' => "", // files, sqlite, etc it will overwrite ur storage and all other caching for waiting u fix ur server
'allow_search' => false, // turn to true will allow $method search("/regex/")
'fallback' => 'files', //Fall back when old driver is not support
'securityKey' => 'auto',
'htaccess' => true,
'path' => '',
'memcache' => array(
array('127.0.0.1', 11211, 1),
// array("new.host.ip",11211,1),
),
'redis' => array(
'host' => '127.0.0.1',
'port' => '',
'password' => '',
'database' => '',
'timeout' => '',
),
'ssdb' => array(
'host' => '127.0.0.1',
'port' => 8888,
'password' => '',
'timeout' => '',
),
'extensions' => array(),
"cache_method" => 1, // 1 = normal, 2 = phpfastcache, 3 = memory
"limited_memory_each_object" => 4000, // maximum size (bytes) of object store in memory
"compress_data" => false // compress stored data, if the backend supports it
);
/**
* @var array
*/
protected static $tmp = array();
/**
* @var DriverAbstract $instance
*/
public $instance;
/**
* phpFastCache constructor.
* @param string $storage
* @param array $config
*/
public function __construct($storage = '', $config = array())
{
if (empty($config)) {
$config = phpFastCache::$config;
}
$config[ 'storage' ] = $storage;
$storage = strtolower($storage);
if ($storage == '' || $storage == 'auto') {
$storage = self::getAutoClass($config);
}
$this->instance = CacheManager::getInstance($storage, $config);
}
/**
* Cores
*/
/**
* @param $config
* @return string
* @throws \Exception
*/
public static function getAutoClass($config)
{
$path = self::getPath(false, $config);
if (is_writable($path)) {
$driver = 'files';
} else if (extension_loaded('apc') && ini_get('apc.enabled') && strpos(PHP_SAPI, 'CGI') === false) {
$driver = 'apc';
} else if (class_exists('memcached')) {
$driver = 'memcached';
} elseif (extension_loaded('wincache') && function_exists('wincache_ucache_set')) {
$driver = 'wincache';
} elseif (extension_loaded('xcache') && function_exists('xcache_get')) {
$driver = 'xcache';
} else if (function_exists('memcache_connect')) {
$driver = 'memcache';
} else if (class_exists('Redis')) {
$driver = 'redis';
} else {
$driver = 'files';
}
return $driver;
}
/**
* @param bool $skip_create_path
* @param $config
* @return string
* @throws \Exception
*/
public static function getPath($skip_create_path = false, $config)
{
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
if (!isset($config[ 'path' ]) || $config[ 'path' ] == '') {
if (self::isPHPModule()) {
$path = $tmp_dir;
} else {
$document_root_path = rtrim($_SERVER[ 'DOCUMENT_ROOT' ], '/') . '/../';
$path = isset($_SERVER[ 'DOCUMENT_ROOT' ]) && is_writable($document_root_path)
? $document_root_path
: rtrim(__DIR__, '/') . '/';
}
if (self::$config[ 'path' ] != '') {
$path = $config[ 'path' ];
}
} else {
$path = $config[ 'path' ];
}
$securityKey = array_key_exists('securityKey',
$config) ? $config[ 'securityKey' ] : '';
if ($securityKey == "" || $securityKey == 'auto') {
$securityKey = self::$config[ 'securityKey' ];
if ($securityKey == 'auto' || $securityKey == '') {
$securityKey = isset($_SERVER[ 'HTTP_HOST' ]) ? preg_replace('/^www./',
'', strtolower(str_replace(':', '_', $_SERVER[ 'HTTP_HOST' ]))) : "default";
}
}
if ($securityKey != '') {
$securityKey .= '/';
}
$securityKey = self::cleanFileName($securityKey);
$full_path = rtrim($path,'/') . '/' . $securityKey;
$full_pathx = md5($full_path);
if ($skip_create_path == false && !isset(self::$tmp[ $full_pathx ])) {
if (!@file_exists($full_path) || !@is_writable($full_path)) {
if (!@file_exists($full_path)) {
@mkdir($full_path, self::__setChmodAuto($config), true);
}
if (!@is_writable($full_path)) {
@chmod($full_path, self::__setChmodAuto($config));
}
if(!@is_writable($full_path)) {
// switch back to tmp dir again if the path is not writeable
$full_path = rtrim($tmp_dir,'/') . '/' . $securityKey;
if (!@file_exists($full_path)) {
@mkdir($full_path, self::__setChmodAuto($config), true);
}
if (!@is_writable($full_path)) {
@chmod($full_path, self::__setChmodAuto($config));
}
}
if (!@file_exists($full_path) || !@is_writable($full_path)) {
throw new phpFastCacheCoreException('PLEASE CREATE OR CHMOD ' . $full_path . ' - 0777 OR ANY WRITABLE PERMISSION!', 92);
}
}
self::$tmp[ $full_pathx ] = true;
self::htaccessGen($full_path, array_key_exists('htaccess', $config) ? $config[ 'htaccess' ] : false);
}
return realpath($full_path);
}
/**
* @param $filename
* @return mixed
*/
public static function cleanFileName($filename)
{
$regex = array(
'/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/',
'/\.$/',
'/^\./',
);
$replace = array('-', '', '');
return trim(preg_replace($regex, $replace, trim($filename)),'-');
}
/**
* @param $config
* @return int
*/
public static function __setChmodAuto($config)
{
if (!isset($config[ 'default_chmod' ]) || $config[ 'default_chmod' ] == '' || is_null($config[ 'default_chmod' ])) {
return 0777;
} else {
return $config[ 'default_chmod' ];
}
}
/**
* @return array
*/
protected static function getOS()
{
$os = array(
'os' => PHP_OS,
'php' => PHP_SAPI,
'system' => php_uname(),
'unique' => md5(php_uname() . PHP_OS . PHP_SAPI),
);
return $os;
}
/**
* @return bool
*/
public static function isPHPModule()
{
if (PHP_SAPI === 'apache2handler') {
return true;
} else {
if (strpos(PHP_SAPI, 'handler') !== false) {
return true;
}
}
return false;
}
/**
* @param $path
* @param bool $create
* @throws \Exception
*/
protected static function htaccessGen($path, $create = true)
{
if ($create == true) {
if (!is_writable($path)) {
try {
chmod($path, 0777);
} catch (phpFastCacheDriverException $e) {
throw new phpFastCacheDriverException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!',
92);
}
}
if(!file_exists($path."/.htaccess")) {
// echo "write me";
$html = "order deny, allow \r\n
deny from all \r\n
allow from 127.0.0.1";
$f = @fopen($path . '/.htaccess', 'w+');
if (!$f) {
throw new phpFastCacheDriverException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', 92);
}
fwrite($f, $html);
fclose($f);
}
}
}
/**
* @param $name
* @param string $value
*/
public static function setup($name, $value = '')
{
if (is_array($name)) {
self::$config = array_merge(self::$config,$name);
} else {
self::$config[ $name ] = $value;
}
}
/**
* @param $something
*/
public static function debug($something)
{
echo "Starting Debugging ...<br>\r\n ";
if (is_array($something)) {
echo '<pre>';
print_r($something);
echo '</pre>';
var_dump($something);
} else {
echo $something;
}
echo "\r\n<br> Ended";
exit;
}
}

View File

@ -0,0 +1,31 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Core;
/**
* Class phpFastCacheExtensions
* @package phpFastCache\Core
*/
abstract class phpFastCacheExtensions
{
/**
* @param $name
* @param $agr
*/
public function __call($name, $agr)
{
}
}

View File

@ -0,0 +1,137 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use phpFastCache\Exceptions\phpFastCacheDriverException;
/**
* Class apc
* @package phpFastCache\Drivers
*/
class apc extends DriverAbstract
{
/**
* phpFastCache_apc constructor.
* @param array $config
* @throws phpFastCacheDriverException
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver()) {
throw new phpFastCacheDriverException('APC is not installed, cannot continue.');
}
}
/**
* @return bool
*/
public function checkdriver()
{
if (extension_loaded('apc') && ini_get('apc.enabled')) {
return true;
} else {
$this->fallback = true;
return false;
}
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return array|bool
*/
public function driver_set(
$keyword,
$value = '',
$time = 300,
$option = array()
) {
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
return apc_add($keyword, $value, $time);
} else {
return apc_store($keyword, $value, $time);
}
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
*/
public function driver_get($keyword, $option = array())
{
$data = apc_fetch($keyword, $bo);
if ($bo === false) {
return null;
}
return $data;
}
/**
* @param $keyword
* @param array $option
* @return bool|\string[]
*/
public function driver_delete($keyword, $option = array())
{
return apc_delete($keyword);
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$res = array(
'info' => '',
'size' => '',
'data' => '',
);
try {
$res[ 'data' ] = apc_cache_info('user');
} catch (\Exception $e) {
$res[ 'data' ] = array();
}
return $res;
}
/**
* @param array $option
* @return void
*/
public function driver_clean($option = array())
{
@apc_clear_cache();
@apc_clear_cache('user');
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
return (bool) apc_exists($keyword);
}
}

View File

@ -0,0 +1,156 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
/**
* Class cookie
* @package phpFastCache\Drivers
*/
class cookie extends DriverAbstract
{
/**
* phpFastCache_cookie constructor.
* @param array $config
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
$this->fallback = true;
}
}
/**
* @return bool
*/
public function checkdriver()
{
// Check memcache
if (function_exists('setcookie')) {
return true;
}
$this->fallback = true;
return false;
}
/**
*
*/
public function connectServer()
{
// for cookie check output
if (!isset($_COOKIE[ 'phpFastCache' ])) {
if (!@setcookie('phpFastCache', 1, 10)) {
$this->fallback = true;
}
}
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool
*/
public function driver_set($keyword, $value = '', $time = 300, $option = array())
{
$this->connectServer();
$keyword = 'phpFastCache_' . $keyword;
$v = $this->encode($value);
if(isset($this->config['limited_memory_each_object'])
&& strlen($v) > $this->config['limited_memory_each_object']) {
return false;
}
return setcookie($keyword, $v, time() + ($time ? (int)$time : 300), '/');
}
/**
* @param $keyword
* @param array $option
* @return bool|mixed|null
*/
public function driver_get($keyword, $option = array())
{
$this->connectServer();
// return null if no caching
// return value if in caching
$keyword = 'phpFastCache_' . $keyword;
$x = isset($_COOKIE[ $keyword ]) ? $this->decode($_COOKIE[ $keyword ]) : false;
if ($x == false) {
return null;
} else {
return $x;
}
}
/**
* @param $keyword
* @param array $option
*/
public function driver_delete($keyword, $option = array())
{
$this->connectServer();
$keyword = 'phpFastCache_' . $keyword;
@setcookie($keyword, null, -10);
$_COOKIE[ $keyword ] = null;
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$this->connectServer();
$res = array(
'info' => '',
'size' => '',
'data' => $_COOKIE,
);
return $res;
}
/**
* @param array $option
*/
public function driver_clean($option = array())
{
$this->connectServer();
foreach ($_COOKIE as $keyword => $value) {
if (strpos($keyword, 'phpFastCache') !== false) {
@setcookie($keyword, null, -10);
$_COOKIE[ $keyword ] = null;
}
}
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
$this->connectServer();
$x = $this->get($keyword);
return !($x == null);
}
}

View File

@ -0,0 +1,128 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use phpFastCache\Core\DriverInterface;
use phpFastCache\Exceptions\phpFastCacheDriverException;
/**
* Class example
* @package phpFastCache\Drivers
*/
class example extends DriverAbstract
{
/**
* phpFastCache_example constructor.
* @param array $config
* @throws phpFastCacheDriverException
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
throw new phpFastCacheDriverException("Can't use this driver for your website!");
}
}
/**
* @return bool
*/
public function checkdriver()
{
return false;
}
/**
*
*/
public function connectServer()
{
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return void
*/
public function driver_set($keyword, $value = '', $time = 300, $option = array())
{
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
// skip driver
} else {
// add driver
}
}
/**
* @param $keyword
* @param array $option
* @return null
*/
public function driver_get($keyword, $option = array())
{
// return null if no caching
// return value if in caching
return null;
}
/**
* @param $keyword
* @param array $option
* @return void
*/
public function driver_delete($keyword, $option = array())
{
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$res = array(
'info' => '',
'size' => '',
'data' => '',
);
return $res;
}
/**
* @param array $option
* @return void
*/
public function driver_clean($option = array())
{
}
/**
* @param $keyword
*/
public function driver_isExisting($keyword)
{
}
}

View File

@ -0,0 +1,306 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use phpFastCache\Exceptions\phpFastCacheDriverException;
/**
* Class files
* @package phpFastCache\Drivers
*/
class files extends DriverAbstract
{
/**
* Init Cache Path
* phpFastCache_files constructor.
* @param array $config
* @throws phpFastCacheDriverException
*/
public function __construct($config = array())
{
$this->setup($config);
$this->getPath(); // force create path
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
throw new phpFastCacheDriverException("Can't use this driver for your website!");
}
}
/**
* @return bool
*/
public function checkdriver()
{
if (is_writable($this->getPath())) {
return true;
}/* else {
}*/
return false;
}
/**
* @param $keyword
* @param bool $skip
* @return string
* @throws phpFastCacheDriverException
*/
private function getFilePath($keyword, $skip = false)
{
$path = $this->getPath();
$filename = $this->encodeFilename($keyword);
$folder = substr($filename, 0, 2);
$path = rtrim($path, '/') . '/' . $folder;
/**
* Skip Create Sub Folders;
*/
if ($skip == false) {
if (!file_exists($path)) {
if (!mkdir($path, $this->__setChmodAuto(), true)) {
throw new phpFastCacheDriverException('PLEASE CHMOD ' . $this->getPath() . ' - 0777 OR ANY WRITABLE PERMISSION!', 92);
}
}
}
return $path . '/' . $filename . '.txt';
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool
* @throws \Exception
*/
public function driver_set($keyword, $value = '', $time = 300, $option = array())
{
$file_path = $this->getFilePath($keyword);
$data = $this->encode($value);
$toWrite = true;
/*
* Skip if Existing Caching in Options
*/
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true && file_exists($file_path)) {
$content = $this->readfile($file_path);
$old = $this->decode($content);
$toWrite = false;
if ($this->isExpired($old)) {
$toWrite = true;
}
}
// Force write
try {
if ($toWrite == true) {
$f = fopen($file_path, 'w+');
fwrite($f, $data);
fclose($f);
return true;
}
} catch (\Exception $e) {
return false;
}
return false;
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
* @throws \Exception
*/
public function driver_get($keyword, $option = array())
{
$file_path = $this->getFilePath($keyword);
if (!file_exists($file_path)) {
return null;
}
$content = $this->readfile($file_path);
$object = $this->decode($content);
if ($this->isExpired($object)) {
@unlink($file_path);
$this->autoCleanExpired();
return null;
}
return $object;
}
/**
* @param $keyword
* @param array $option
* @return bool
* @throws \Exception
*/
public function driver_delete($keyword, $option = array())
{
$file_path = $this->getFilePath($keyword, true);
if (file_exists($file_path) && @unlink($file_path)) {
return true;
} else {
return false;
}
}
/**
* Return total cache size + auto removed expired files
* @param array $option
* @return array
* @throws \Exception
*/
public function driver_stats($option = array())
{
$res = array(
'info' => '',
'size' => '',
'data' => '',
);
$path = $this->getPath();
$dir = @opendir($path);
if (!$dir) {
throw new phpFastCacheDriverException("Can't read PATH:" . $path, 94);
}
$total = 0;
$removed = 0;
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..' && is_dir($path . '/' . $file)) {
// read sub dir
$subdir = opendir($path . "/" . $file);
if (!$subdir) {
throw new phpFastCacheDriverException("Can't read path:" . $path . '/' . $file, 93);
}
while ($f = readdir($subdir)) {
if ($f != '.' && $f != '..') {
$file_path = $path . '/' . $file . '/' . $f;
$size = filesize($file_path);
$object = $this->decode($this->readfile($file_path));
if (strpos($f, '.') === false) {
$key = $f;
} else {
//Because PHP 5.3, this cannot be written in single line
$key = explode('.', $f);
$key = $key[ 0 ];
}
$content[ $key ] = array(
'size' => $size,
'write_time' => (isset($object[ 'write_time' ]) ? $object[ 'write_time' ] : null),
);
if ($this->isExpired($object)) {
@unlink($file_path);
$removed += $size;
}
$total += $size;
}
}
}
}
$res[ 'size' ] = $total - $removed;
$res[ 'info' ] = array(
'Total [bytes]' => $total,
'Expired and removed [bytes]' => $removed,
'Current [bytes]' => $res[ 'size' ],
);
$res[ "data" ] = $content;
return $res;
}
/**
* @param int $time
*/
public function autoCleanExpired($time = 3600)
{
$autoclean = $this->get('keyword_clean_up_driver_files');
if ($autoclean == null) {
$this->set('keyword_clean_up_driver_files', $time);
$res = $this->stats();
}
}
/**
* @param array $option
* @throws \Exception
* @return void
*/
public function driver_clean($option = array())
{
$path = $this->getPath();
$dir = @opendir($path);
if (!$dir) {
throw new phpFastCacheDriverException("Can't read PATH:" . $path, 94);
}
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..' && is_dir($path . '/' . $file)) {
// read sub dir
$subdir = @opendir($path . '/' . $file);
if (!$subdir) {
throw new phpFastCacheDriverException("Can't read path:" . $path . '/' . $file, 93);
}
while ($f = readdir($subdir)) {
if ($f != '.' && $f != '..') {
$file_path = $path . '/' . $file . '/' . $f;
@unlink($file_path);
}
}
}
}
}
/**
* @param $keyword
* @return bool
* @throws \Exception
*/
public function driver_isExisting($keyword)
{
$file_path = $this->getFilePath($keyword, true);
if (!file_exists($file_path)) {
return false;
} else {
// check expired or not
$value = $this->get($keyword);
return !($value == null);
}
}
/**
* @param $object
* @return bool
*/
public function isExpired($object)
{
if (isset($object[ 'expired_time' ]) && time() >= $object[ 'expired_time' ]) {
return true;
} else {
return false;
}
}
}

View File

@ -0,0 +1,200 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use Memcache as MemcacheSoftware;
/**
* Class memcache
* @package phpFastCache\Drivers
*/
class memcache extends DriverAbstract
{
/**
* @var \Memcache
*/
public $instant;
/**
* @var int
*/
protected $memcacheFlags = 0;
/**
* phpFastCache_memcache constructor.
* @param array $config
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
$this->fallback = true;
}
if (class_exists('Memcache')) {
$this->instant = new MemcacheSoftware();
if (array_key_exists('compress_data', $config) && $config[ 'compress_data' ] === true) {
$this->memcacheFlags = MEMCACHE_COMPRESSED;
}
} else {
$this->fallback = true;
}
}
/**
* @return bool
*/
public function checkdriver()
{
// Check memcache
if (function_exists('memcache_connect')) {
return true;
}
$this->fallback = true;
return false;
}
/**
*
*/
public function connectServer()
{
$server = $this->config[ 'memcache' ];
if (count($server) < 1) {
$server = array(
array('127.0.0.1', 11211),
);
}
foreach ($server as $s) {
$name = $s[ 0 ] . "_" . $s[ 1 ];
if (!isset($this->checked[ $name ])) {
try {
if (!$this->instant->addserver($s[ 0 ], $s[ 1 ])) {
$this->fallback = true;
}
$this->checked[ $name ] = 1;
} catch (\Exception $e) {
$this->fallback = true;
}
}
}
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return array|bool
*/
public function driver_set(
$keyword,
$value = '',
$time = 300,
$option = array()
) {
$this->connectServer();
// Memcache will only allow a expiration timer less than 2592000 seconds,
// otherwise, it will assume you're giving it a UNIX timestamp.
if ($time > 2592000) {
$time = time() + $time;
}
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
return $this->instant->add($keyword, $value, $this->memcacheFlags, $time);
} else {
return $this->instant->set($keyword, $value, $this->memcacheFlags, $time);
}
}
/**
* @param $keyword
* @param array $option
* @return array|null|string
*/
public function driver_get($keyword, $option = array())
{
$this->connectServer();
// return null if no caching
// return value if in caching
$x = $this->instant->get($keyword);
if ($x == false) {
return null;
} else {
return $x;
}
}
/**
* @param $keyword
* @param array $option
*/
public function driver_delete($keyword, $option = array())
{
$this->connectServer();
$this->instant->delete($keyword);
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$this->connectServer();
$res = array(
'info' => '',
'size' => '',
'data' => $this->instant->getStats(),
);
return $res;
}
/**
* @param array $option
*/
public function driver_clean($option = array())
{
$this->connectServer();
$this->instant->flush();
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
$this->connectServer();
$x = $this->get($keyword);
return !($x == null);
}
}

View File

@ -0,0 +1,200 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use Memcached as MemcachedSoftware;
/**
* Class memcached
* @package phpFastCache\Drivers
*/
class memcached extends DriverAbstract
{
/**
* @var \Memcached
*/
public $instant;
/**
* phpFastCache_memcached constructor.
* @param array $config
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
$this->fallback = true;
}
if (class_exists('Memcached')) {
$this->instant = new MemcachedSoftware();
} else {
$this->fallback = true;
}
}
/**
* @return bool
*/
public function checkdriver()
{
if (class_exists('Memcached')) {
return true;
}
$this->fallback = true;
return false;
}
/**
* @return bool
*/
public function connectServer()
{
if ($this->checkdriver() == false) {
return false;
}
$s = $this->config[ 'memcache' ];
if (count($s) < 1) {
$s = array(
array('127.0.0.1', 11211, 100),
);
}
foreach ($s as $server) {
$name = isset($server[ 0 ]) ? $server[ 0 ] : '127.0.0.1';
$port = isset($server[ 1 ]) ? $server[ 1 ] : 11211;
$sharing = isset($server[ 2 ]) ? $server[ 2 ] : 0;
$checked = $name . '_' . $port;
if (!isset($this->checked[ $checked ])) {
try {
if ($sharing > 0) {
if (!$this->instant->addServer($name, $port,
$sharing)
) {
$this->fallback = true;
}
} else {
if (!$this->instant->addServer($name, $port)) {
$this->fallback = true;
}
}
$this->checked[ $checked ] = 1;
} catch (\Exception $e) {
$this->fallback = true;
}
}
}
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool
*/
public function driver_set($keyword, $value = '', $time = 300, $option = array())
{
$this->connectServer();
// Memcache will only allow a expiration timer less than 2592000 seconds,
// otherwise, it will assume you're giving it a UNIX timestamp.
if ($time > 2592000) {
$time = time() + $time;
}
if (isset($option[ 'isExisting' ]) && $option[ 'isExisting' ] == true) {
return $this->instant->add($keyword, $value, $time);
} else {
return $this->instant->set($keyword, $value, $time);
}
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
*/
public function driver_get($keyword, $option = array())
{
// return null if no caching
// return value if in caching
$this->connectServer();
$x = @$this->instant->get($keyword);// Prevent memcached to return a warning for long keywords
if ($x == false) {
return null;
} else {
return $x;
}
}
/**
* @param $keyword
* @param array $option
* @return void
*/
public function driver_delete($keyword, $option = array())
{
$this->connectServer();
$this->instant->delete($keyword);
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$this->connectServer();
$res = array(
'info' => '',
'size' => '',
'data' => $this->instant->getStats(),
);
return $res;
}
/**
* @param array $option
* @return void
*/
public function driver_clean($option = array())
{
$this->connectServer();
$this->instant->flush();
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
$this->connectServer();
$x = $this->get($keyword);
return !($x == null);
}
}

View File

@ -0,0 +1,125 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use phpFastCache\Exceptions\phpFastCacheDriverException;
/**
* Class mongodb
* @package phpFastCache\Drivers
*/
class mongodb extends DriverAbstract
{
/**
* phpFastCache constructor.
* @param array $config
* @throws phpFastCacheDriverException
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
throw new phpFastCacheDriverException("Can't use this driver for your website!");
}
}
/**
* @return bool
*/
public function checkdriver()
{
// return true;
return false;
}
/**
*
*/
public function connectServer()
{
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
*/
public function driver_set($keyword, $value = '', $time = 300, $option = array())
{
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
// skip driver
} else {
// add driver
}
}
/**
* @param $keyword
* @param array $option
* @return null
*/
public function driver_get($keyword, $option = array())
{
// return null if no caching
// return value if in caching
return null;
}
/**
* @param $keyword
* @param array $option
*/
public function driver_delete($keyword, $option = array())
{
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$res = array(
'info' => '',
'size' => '',
'data' => '',
);
return $res;
}
/**
* @param array $option
*/
public function driver_clean($option = array())
{
}
/**
* @param $keyword
*/
public function driver_isExisting($keyword)
{
}
}

View File

@ -0,0 +1,222 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use Predis\Client as PredisSoftware;
/**
* Class predis
* @package phpFastCache\Drivers
*/
class predis extends DriverAbstract
{
/**
* @var bool
*/
public $checked_redis = false;
/**
* phpFastCache_predis constructor.
* @param array $config
*/
public function __construct($config = array())
{
$this->setup($config);
if (!class_exists("\\Predis\\Client")) {
$this->required_extension("predis-1.0/autoload");
}
}
/**
* @return bool
*/
public function checkdriver()
{
// Check memcache
if (!class_exists("\\Predis\\Client")) {
$this->required_extension("predis-1.0/autoload");
try {
\Predis\Autoloader::register();
} catch (\Exception $e) {
}
}
return true;
}
/**
* @return bool
*/
public function connectServer()
{
$server = isset($this->config[ 'redis' ]) ? $this->config[ 'redis' ] : array(
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'database' => '',
);
if ($this->checked_redis === false) {
$c = array(
'host' => $server[ 'host' ],
);
$port = isset($server[ 'port' ]) ? $server[ 'port' ] : '';
if ($port != '') {
$c[ 'port' ] = $port;
}
$password = isset($server[ 'password' ]) ? $server[ 'password' ] : '';
if ($password != '') {
$c[ 'password' ] = $password;
}
$database = isset($server[ 'database' ]) ? $server[ 'database' ] : '';
if ($database != '') {
$c[ 'database' ] = $database;
}
$timeout = isset($server[ 'timeout' ]) ? $server[ 'timeout' ] : '';
if ($timeout != '') {
$c[ 'timeout' ] = $timeout;
}
$read_write_timeout = isset($server[ 'read_write_timeout' ]) ? $server[ 'read_write_timeout' ] : '';
if ($read_write_timeout != '') {
$c[ 'read_write_timeout' ] = $read_write_timeout;
}
$this->instant = new PredisSoftware($c);
$this->checked_redis = true;
if (!$this->instant) {
$this->fallback = true;
return false;
} else {
return true;
}
}
return true;
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return mixed
*/
public function driver_set($keyword, $value = '', $time = 300, $option = array())
{
if ($this->connectServer()) {
$value = $this->encode($value);
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
return $this->instant->setex($keyword, $time, $value);
} else {
return $this->instant->setex($keyword, $time, $value);
}
} else {
return $this->backup()->set($keyword, $value, $time, $option);
}
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
*/
public function driver_get($keyword, $option = array())
{
if ($this->connectServer()) {
// return null if no caching
// return value if in caching'
$x = $this->instant->get($keyword);
if ($x == false) {
return null;
} else {
return $this->decode($x);
}
} else {
$this->backup()->get($keyword, $option);
}
}
/**
* @param $keyword
* @param array $option
*/
public function driver_delete($keyword, $option = array())
{
if ($this->connectServer()) {
$this->instant->del($keyword);
}
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
if ($this->connectServer()) {
$res = array(
'info' => '',
'size' => '',
'data' => $this->instant->info(),
);
return $res;
}
return array();
}
/**
* @param array $option
* @return void
*/
public function driver_clean($option = array())
{
if ($this->connectServer()) {
$this->instant->flushDB();
}
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
if ($this->connectServer()) {
$x = $this->instant->exists($keyword);
return !($x == null);
} else {
return $this->backup()->isExisting($keyword);
}
}
}

View File

@ -0,0 +1,225 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use Redis as RedisSoftware;
/**
* Class redis
* @package phpFastCache\Drivers
*/
class redis extends DriverAbstract
{
/**
* @var bool
*/
public $checked_redis = false;
/**
* phpFastCache_predis constructor.
* @param array $config
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
$this->fallback = true;
}
if (class_exists('Redis')) {
$this->instant = new RedisSoftware();
}
}
/**
* @return bool
*/
public function checkdriver()
{
// Check memcache
if (class_exists('Redis')) {
return true;
}
$this->fallback = true;
return false;
}
/**
* @return bool
*/
public function connectServer()
{
$server = isset($this->config[ 'redis' ]) ? $this->config[ 'redis' ] : array(
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'database' => '',
'timeout' => '1',
);
if ($this->checked_redis === false) {
$host = $server[ 'host' ];
$port = isset($server[ 'port' ]) ? (int)$server[ 'port' ] : "";
if ($port != '') {
$c[ 'port' ] = $port;
}
$password = isset($server[ 'password' ]) ? $server[ 'password' ] : '';
if ($password != '') {
$c[ 'password' ] = $password;
}
$database = isset($server[ 'database' ]) ? $server[ 'database' ] : '';
if ($database != '') {
$c[ 'database' ] = $database;
}
$timeout = isset($server[ 'timeout' ]) ? $server[ 'timeout' ] : '';
if ($timeout != '') {
$c[ 'timeout' ] = $timeout;
}
$read_write_timeout = isset($server[ 'read_write_timeout' ]) ? $server[ 'read_write_timeout' ] : '';
if ($read_write_timeout != '') {
$c[ 'read_write_timeout' ] = $read_write_timeout;
}
if (!$this->instant->connect($host, (int)$port, (int)$timeout)) {
$this->checked_redis = true;
$this->fallback = true;
return false;
} else {
if ($database != '') {
$this->instant->select((int)$database);
}
$this->checked_redis = true;
return true;
}
}
return true;
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool
*/
public function driver_set($keyword, $value = '', $time = 300, $option = array())
{
if ($this->connectServer()) {
$value = $this->encode($value);
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
return $this->instant->set($keyword, $value,
array('xx', 'ex' => $time));
} else {
return $this->instant->set($keyword, $value, $time);
}
} else {
return $this->backup()->set($keyword, $value, $time, $option);
}
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
*/
public function driver_get($keyword, $option = array())
{
if ($this->connectServer()) {
// return null if no caching
// return value if in caching'
$x = $this->instant->get($keyword);
if ($x == false) {
return null;
} else {
return $this->decode($x);
}
} else {
$this->backup()->get($keyword, $option);
}
}
/**
* @param $keyword
* @param array $option
* @return void
*/
public function driver_delete($keyword, $option = array())
{
if ($this->connectServer()) {
$this->instant->delete($keyword);
}
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
if ($this->connectServer()) {
$res = array(
'info' => '',
'size' => '',
'data' => $this->instant->info(),
);
return $res;
}
return array();
}
/**
* @param array $option
*/
public function driver_clean($option = array())
{
if ($this->connectServer()) {
$this->instant->flushDB();
}
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
if ($this->connectServer()) {
$x = $this->instant->exists($keyword);
if ($x == null) {
return false;
} else {
return true;
}
} else {
return $this->backup()->isExisting($keyword);
}
}
}

View File

@ -0,0 +1,482 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use PDO;
use PDOException;
use phpFastCache\Exceptions\phpFastCacheDriverException;
/**
* Class sqlite
* @package phpFastCache\Drivers
*/
class sqlite extends DriverAbstract
{
/**
*
*/
const SQLITE_DIR = 'sqlite';
/**
*
*/
const INDEXING_FILE = 'indexing';
/**
* @var int
*/
public $max_size = 10; // 10 mb
/**
* @var array
*/
public $instant = array();
/**
* @var null
*/
public $indexing = null;
/**
* @var string
*/
public $path = '';
/**
* @var int
*/
public $currentDB = 1;
/**
* Init Main Database & Sub Database
* phpFastCache_sqlite constructor.
* @param array $config
* @throws phpFastCacheDriverException
*/
public function __construct($config = array())
{
/**
* init the path
*/
$this->setup($config);
if (!$this->checkdriver()) {
throw new phpFastCacheDriverException('SQLITE is not installed, cannot continue.');
}
if (!file_exists($this->getPath() . '/' . self::SQLITE_DIR)) {
if (!mkdir($this->getPath() . '/' . self::SQLITE_DIR, $this->__setChmodAuto(), true)) {
$this->fallback = true;
}
}
$this->path = $this->getPath() . '/' . self::SQLITE_DIR;
}
/**
* INIT NEW DB
* @param \PDO $db
*/
public function initDB(PDO $db)
{
$db->exec('drop table if exists "caching"');
$db->exec('CREATE TABLE "caching" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "keyword" VARCHAR UNIQUE, "object" BLOB, "exp" INTEGER)');
$db->exec('CREATE UNIQUE INDEX "cleanup" ON "caching" ("keyword","exp")');
$db->exec('CREATE INDEX "exp" ON "caching" ("exp")');
$db->exec('CREATE UNIQUE INDEX "keyword" ON "caching" ("keyword")');
}
/**
* INIT Indexing DB
* @param \PDO $db
*/
public function initIndexing(PDO $db)
{
// delete everything before reset indexing
$dir = opendir($this->path);
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..' && $file != 'indexing' && $file != 'dbfastcache') {
unlink($this->path . '/' . $file);
}
}
$db->exec('drop table if exists "balancing"');
$db->exec('CREATE TABLE "balancing" ("keyword" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "db" INTEGER)');
$db->exec('CREATE INDEX "db" ON "balancing" ("db")');
$db->exec('CREATE UNIQUE INDEX "lookup" ON "balancing" ("keyword")');
}
/**
* INIT Instant DB
* Return Database of Keyword
* @param $keyword
* @return int
*/
public function indexing($keyword)
{
if ($this->indexing == null) {
$createTable = false;
if (!file_exists($this->path . '/indexing')) {
$createTable = true;
}
$PDO = new PDO("sqlite:" . $this->path . '/' . self::INDEXING_FILE);
$PDO->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
if ($createTable == true) {
$this->initIndexing($PDO);
}
$this->indexing = $PDO;
unset($PDO);
$stm = $this->indexing->prepare("SELECT MAX(`db`) as `db` FROM `balancing`");
$stm->execute();
$row = $stm->fetch(PDO::FETCH_ASSOC);
if (!isset($row[ 'db' ])) {
$db = 1;
} elseif ($row[ 'db' ] <= 1) {
$db = 1;
} else {
$db = $row[ 'db' ];
}
// check file size
$size = file_exists($this->path . '/db' . $db) ? filesize($this->path . '/db' . $db) : 1;
$size = round($size / 1024 / 1024, 1);
if ($size > $this->max_size) {
$db = $db + 1;
}
$this->currentDB = $db;
}
// look for keyword
$stm = $this->indexing->prepare("SELECT * FROM `balancing` WHERE `keyword`=:keyword LIMIT 1");
$stm->execute(array(
':keyword' => $keyword,
));
$row = $stm->fetch(PDO::FETCH_ASSOC);
if (isset($row[ 'db' ]) && $row[ 'db' ] != '') {
$db = $row[ 'db' ];
} else {
/*
* Insert new to Indexing
*/
$db = $this->currentDB;
$stm = $this->indexing->prepare("INSERT INTO `balancing` (`keyword`,`db`) VALUES(:keyword, :db)");
$stm->execute(array(
':keyword' => $keyword,
':db' => $db,
));
}
return $db;
}
/**
* @param $keyword
* @param bool $reset
* @return mixed
*/
public function db($keyword, $reset = false)
{
/**
* Default is fastcache
*/
$instant = $this->indexing($keyword);
/**
* init instant
*/
if (!isset($this->instant[ $instant ])) {
// check DB Files ready or not
$createTable = false;
if (!file_exists($this->path . '/db' . $instant) || $reset == true) {
$createTable = true;
}
$PDO = new PDO('sqlite:' . $this->path . '/db' . $instant);
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($createTable == true) {
$this->initDB($PDO);
}
$this->instant[ $instant ] = $PDO;
unset($PDO);
}
return $this->instant[ $instant ];
}
/**
* @return bool
*/
public function checkdriver()
{
if (extension_loaded('pdo_sqlite') && is_writable($this->getPath())) {
return true;
}
$this->fallback = true;
return false;
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool
*/
public function driver_set(
$keyword,
$value = '',
$time = 300,
$option = array()
) {
$skipExisting = isset($option[ 'skipExisting' ]) ? $option[ 'skipExisting' ] : false;
$toWrite = true;
// check in cache first
$in_cache = $this->get($keyword, $option);
if ($skipExisting == true) {
if ($in_cache == null) {
$toWrite = true;
} else {
$toWrite = false;
}
}
if ($toWrite == true) {
try {
$stm = $this->db($keyword)
->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)");
$stm->execute(array(
':keyword' => $keyword,
':object' => $this->encode($value),
':exp' => time() + (int)$time,
));
return true;
} catch (\PDOException $e) {
try {
$stm = $this->db($keyword, true)
->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)");
$stm->execute(array(
':keyword' => $keyword,
':object' => $this->encode($value),
':exp' => time() + (int)$time,
));
} catch (PDOException $e) {
return false;
}
}
}
return false;
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
*/
public function driver_get($keyword, $option = array())
{
// return null if no caching
// return value if in caching
try {
$stm = $this->db($keyword)
->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1");
$stm->execute(array(
':keyword' => $keyword,
));
$row = $stm->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
try {
$stm = $this->db($keyword, true)
->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1");
$stm->execute(array(
':keyword' => $keyword,
));
$row = $stm->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return null;
}
}
if ($this->isExpired($row)) {
$this->deleteRow($row);
return null;
}
if (isset($row[ 'id' ])) {
$data = $this->decode($row[ 'object' ]);
return $data;
}
return null;
}
/**
* @param $row
* @return bool
*/
public function isExpired($row)
{
if (isset($row[ 'exp' ]) && time() >= $row[ 'exp' ]) {
return true;
}
return false;
}
/**
* @param $row
* @return bool
*/
public function deleteRow($row)
{
try {
$stm = $this->db($row[ 'keyword' ])
->prepare("DELETE FROM `caching` WHERE (`id`=:id) OR (`exp` <= :U) ");
$stm->execute(array(
':id' => $row[ 'id' ],
':U' => time(),
));
} catch (PDOException $e) {
return false;
}
}
/**
* @param $keyword
* @param array $option
* @return bool
*/
public function driver_delete($keyword, $option = array())
{
try {
$stm = $this->db($keyword)
->prepare("DELETE FROM `caching` WHERE (`keyword`=:keyword) OR (`exp` <= :U)");
$stm->execute(array(
':keyword' => $keyword,
':U' => time(),
));
} catch (PDOException $e) {
return false;
}
}
/**
* Return total cache size + auto removed expired entries
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$res = array(
'info' => '',
'size' => '',
'data' => '',
);
$total = 0;
$optimized = 0;
$dir = opendir($this->path);
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..') {
$file_path = $this->path . "/" . $file;
$size = filesize($file_path);
$total = $total + $size;
try {
$PDO = new PDO("sqlite:" . $file_path);
$PDO->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$stm = $PDO->prepare("DELETE FROM `caching` WHERE `exp` <= :U");
$stm->execute(array(
':U' => date('U'),
));
$PDO->exec('VACUUM;');
$size = filesize($file_path);
$optimized = $optimized + $size;
} catch (PDOException $e) {
$size = 0;
$optimized = 0;
}
}
}
$res[ 'size' ] = $optimized;
$res[ 'info' ] = array(
'total before removing expired entries [bytes]' => $total,
'optimized after removing expired entries [bytes]' => $optimized,
);
return $res;
}
/**
* @param array $option
* @return void
*/
public function driver_clean($option = array())
{
// close connection
$this->instant = array();
$this->indexing = null;
// delete everything before reset indexing
$dir = opendir($this->path);
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..') {
unlink($this->path . '/' . $file);
}
}
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
try {
$stm = $this->db($keyword)
->prepare("SELECT COUNT(`id`) as `total` FROM `caching` WHERE `keyword`=:keyword");
$stm->execute(array(
':keyword' => $keyword,
));
$data = $stm->fetch(PDO::FETCH_ASSOC);
if ($data[ 'total' ] >= 1) {
return true;
} else {
return false;
}
} catch (PDOException $e) {
return false;
}
}
}

View File

@ -0,0 +1,191 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
/**
* Class ssdb
* @package phpFastCache\Drivers
*/
class ssdb extends DriverAbstract
{
/**
* @var bool
*/
private $checked_ssdb = false;
/**
* phpFastCache_ssdb constructor.
* @param array $config
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
$this->fallback = true;
}
}
/**
* @return bool
*/
public function checkdriver()
{
// Check memcache
$this->required_extension('SSDB');
if (class_exists('SimpleSSDB')) {
return true;
}
$this->fallback = true;
return false;
}
/**
* @return bool
*/
public function connectServer()
{
$server = isset($this->config[ 'ssdb' ]) ? $this->config[ 'ssdb' ] : array(
'host' => "127.0.0.1",
'port' => 8888,
'password' => '',
'timeout' => 2000,
);
if ($this->checked_ssdb === false) {
$host = $server[ 'host' ];
$port = isset($server[ 'port' ]) ? (int)$server[ 'port' ] : 8888;
$password = isset($server[ 'password' ]) ? $server[ 'password' ] : '';
$timeout = !empty($server[ 'timeout' ]) ? (int)$server[ 'timeout' ] : 2000;
$this->instant = new \SimpleSSDB($host, $port, $timeout);
if (!empty($password)) {
$this->instant->auth($password);
}
$this->checked_ssdb = true;
if (!$this->instant) {
$this->fallback = true;
return false;
} else {
return true;
}
}
return true;
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool
*/
public function driver_set($keyword, $value = '', $time = 300, $option = array())
{
if ($this->connectServer()) {
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
$x = $this->instant->get($keyword);
if ($x === false) {
return false;
} elseif (!is_null($x)) {
return true;
}
}
$value = $this->encode($value);
return $this->instant->setx($keyword, $value, $time);
} else {
return $this->backup()->set($keyword, $value, $time, $option);
}
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
*/
public function driver_get($keyword, $option = array())
{
if ($this->connectServer()) {
// return null if no caching
// return value if in caching'
$x = $this->instant->get($keyword);
if ($x == false) {
return null;
} else {
return $this->decode($x);
}
} else {
$this->backup()->get($keyword, $option);
}
}
/**
* @param $keyword
* @param array $option
*/
public function driver_delete($keyword, $option = array())
{
if ($this->connectServer()) {
$this->instant->del($keyword);
}
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
if ($this->connectServer()) {
$res = array(
'info' => '',
'size' => $this->instant->dbsize(),
'data' => $this->instant->info(),
);
return $res;
}
return array();
}
/**
* @param array $option
* @return bool
*/
public function driver_clean($option = array())
{
//Is not supported, only support command line operations
return false;
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
if ($this->connectServer()) {
$x = $this->instant->exists($keyword);
return !($x == null);
} else {
return $this->backup()->isExisting($keyword);
}
}
}

View File

@ -0,0 +1,133 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
/**
* Class wincache
* @package phpFastCache\Drivers
*/
class wincache extends DriverAbstract
{
/**
* phpFastCache_wincache constructor.
* @param array $config
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
$this->fallback = true;
}
}
/**
* @return bool
*/
public function checkdriver()
{
if (extension_loaded('wincache') && function_exists('wincache_ucache_set')) {
return true;
}
$this->fallback = true;
return false;
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool
*/
public function driver_set($keyword, $value = "", $time = 300, $option = array())
{
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
return wincache_ucache_add($keyword, $value, $time);
} else {
return wincache_ucache_set($keyword, $value, $time);
}
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
*/
public function driver_get($keyword, $option = array())
{
// return null if no caching
// return value if in caching
$x = wincache_ucache_get($keyword, $suc);
if ($suc == false) {
return null;
} else {
return $x;
}
}
/**
* @param $keyword
* @param array $option
* @return bool
*/
public function driver_delete($keyword, $option = array())
{
return wincache_ucache_delete($keyword);
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$res = array(
'info' => '',
'size' => '',
'data' => wincache_scache_info(),
);
return $res;
}
/**
* @param array $option
* @return bool
*/
public function driver_clean($option = array())
{
wincache_ucache_clear();
return true;
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
if (wincache_ucache_exists($keyword)) {
return true;
} else {
return false;
}
}
}

View File

@ -0,0 +1,141 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Drivers;
use phpFastCache\Core\DriverAbstract;
use Exception;
/**
* Class xcache
* @package phpFastCache\Drivers
*/
class xcache extends DriverAbstract
{
/**
* phpFastCache_xcache constructor.
* @param array $config
*/
public function __construct($config = array())
{
$this->setup($config);
if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
$this->fallback = true;
}
}
/**
* @return bool
*/
public function checkdriver()
{
// Check xcache
if (extension_loaded('xcache') && function_exists('xcache_get')) {
return true;
}
$this->fallback = true;
return false;
}
/**
* @param $keyword
* @param string $value
* @param int $time
* @param array $option
* @return bool
*/
public function driver_set($keyword, $value = "", $time = 300, $option = array())
{
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true) {
if (!$this->isExisting($keyword)) {
return xcache_set($keyword, serialize($value), $time);
}
} else {
return xcache_set($keyword, serialize($value), $time);
}
return false;
}
/**
* @param $keyword
* @param array $option
* @return mixed|null
*/
public function driver_get($keyword, $option = array())
{
// return null if no caching
// return value if in caching
$data = unserialize(xcache_get($keyword));
if ($data === false || $data == '') {
return null;
}
return $data;
}
/**
* @param $keyword
* @param array $option
* @return bool
*/
public function driver_delete($keyword, $option = array())
{
return xcache_unset($keyword);
}
/**
* @param array $option
* @return array
*/
public function driver_stats($option = array())
{
$res = array(
'info' => '',
'size' => '',
'data' => '',
);
try {
$res[ 'data' ] = xcache_list(XC_TYPE_VAR, 100);
} catch (Exception $e) {
$res[ 'data' ] = array();
}
return $res;
}
/**
* @param array $option
* @return bool
*/
public function driver_clean($option = array())
{
$cnt = xcache_count(XC_TYPE_VAR);
for ($i = 0; $i < $cnt; $i++) {
xcache_clear_cache(XC_TYPE_VAR, $i);
}
return true;
}
/**
* @param $keyword
* @return bool
*/
public function driver_isExisting($keyword)
{
return xcache_isset($keyword);
}
}

View File

@ -0,0 +1,26 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Exceptions;
use \Exception;
/**
* Class phpFastCacheCoreException
* @package phpFastCache\Exceptions
*/
class phpFastCacheCoreException extends Exception
{
}

View File

@ -0,0 +1,26 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Exceptions;
use \Exception;
/**
* Class phpFastCacheDriverException
* @package phpFastCache\Exceptions
*/
class phpFastCacheDriverException extends Exception
{
}

View File

@ -0,0 +1,33 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\plugins;
use phpFastCache\CacheManager;
// Setup your cronjob to run this file every
// 30 mins - 60 mins to help clean up
// the expired files faster
require_once (__DIR__ . "/../phpFastCache.php");
$setup = array(
"path" => "/your_path/to_clean/"
);
$cache = CacheManager::Files($setup);
// clean up expired files cache every hour
// For now only "files" drivers is supported
$cache->autoCleanExpired(3600*1);

View File

@ -0,0 +1,47 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
namespace phpFastCache\Util;
use phpFastCache\Exceptions\phpFastCacheCoreException;
/**
* Class Languages
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
class Languages
{
public static function setEncoding($encoding = 'UTF-8', $language = null)
{
if ($language === null || !in_array($language, array('uni', 'Japanese', 'ja', 'English', 'en'), true)) {
$language = 'uni';
}
switch(strtoupper($encoding))
{
case 'UTF-8':
if(extension_loaded("mbstring")) {
mb_internal_encoding($encoding);
mb_http_output($encoding);
mb_http_input($encoding);
mb_language($language);
mb_regex_encoding($encoding);
} else {
throw new phpFastCacheCoreException("MB String need to be installed for Unicode Encoding");
}
break;
}
}
}

View File

@ -0,0 +1,43 @@
<?php
/*
* If Any problem with Autoload on other project
* Try to put this line on your config project
* define("PHPFASTCACHE_LEGACY",true);
* and just keep include phpFastCache/phpFastCache.php or Composer Autoloader
*/
use phpFastCache\CacheManager;
require_once __DIR__.'/../Core/DriverInterface.php';
require_once __DIR__.'/../Core/DriverAbstract.php';
require_once __DIR__.'/../Core/phpFastCache.php';
require_once __DIR__.'/../Core/phpFastCacheExtensions.php';
require_once __DIR__.'/../Exceptions/phpFastCacheCoreException.php';
require_once __DIR__.'/../Exceptions/phpFastCacheDriverException.php';
require_once __DIR__.'/../Drivers/files.php';
require_once __DIR__.'/../Drivers/memcache.php';
require_once __DIR__.'/../Drivers/memcached.php';
require_once __DIR__.'/../Drivers/mongodb.php';
require_once __DIR__.'/../Drivers/predis.php';
require_once __DIR__.'/../Drivers/redis.php';
require_once __DIR__.'/../Drivers/sqlite.php';
require_once __DIR__.'/../CacheManager.php';
require_once __DIR__.'/../phpFastCache.php';
/**
* __c() Short alias
* @param string $storage
* @param array $config
* @return mixed
*/
if (!function_exists("__c")) {
function __c($storage = 'auto', $config = array())
{
return CacheManager::getInstance($storage, $config);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace phpFastCache\Util;
define('PHP_OPEN_BASEDIR', @ini_get("open_basedir"));
class OpenBaseDir {
public static $stores = array();
public static function checkBaseDir($path) {
if(!is_null(PHP_OPEN_BASEDIR) && PHP_OPEN_BASEDIR != "") {
/*
* ONLY check ONE time if System Have Open Base Dir
* Else, always return TRUE for system without OPenBaseDir
*/
$index = md5($path);
if (!isset(self::$stores[$index])) {
// never check before, then check it 1 one time for the src dir only
$list = explode(":", PHP_OPEN_BASEDIR);
foreach ($list as $allowed_path) {
$tmp = explode($allowed_path, $path, 2);
if ($tmp[0] != $path) {
// echo "<br>".$tmp[0]." = ".$path." BY {$allowed_path}";
self::$stores[$index] = true;
return true;
}
}
self::$stores[$index] = false;
} else {
return self::$stores[$index];
}
return false;
}
return true;
}
}

View File

@ -0,0 +1 @@
<a href="http://www.phpfastcache.com" title="Simple Php Caching Class">Visit www.phpfastcache.com</a>

View File

@ -0,0 +1,55 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
use phpFastCache\CacheManager;
define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
if(!defined("PHPFASTCACHE_LEGACY")) {
/**
* Register Autoload
*/
spl_autoload_register(function ($entity) {
// Explode is faster than substr & strstr also more control
$module = explode('\\',$entity,2);
if ($module[0] !== 'phpFastCache') {
/**
* Not a part of phpFastCache file
* then we return here.
*/
return;
}
$entity = str_replace('\\', '/', $module[1]);
$path = __DIR__ . '/' . $entity . '.' . PHP_EXT;
if (is_readable($path)) {
require_once $path;
}
});
} else {
require_once __DIR__.'/Util/Legacy.php';
}
/**
* phpFastCache() Full alias
* @param string $storage
* @param array $config
* @return mixed
*/
if (!function_exists("phpFastCache")) {
function phpFastCache($storage = 'auto', $config = array())
{
return CacheManager::getInstance($storage, $config);
}
}