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