249 lines
6.8 KiB
PHP
249 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace BRSystems\Core\Library\cApc;
|
|
|
|
/**
|
|
* Apc cache request class
|
|
*
|
|
* @category LIB APC / APCu
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @since Version 1.1
|
|
*/
|
|
class cApc
|
|
{
|
|
// APC CURRENTLY USED KEYS - keys need to be picked for universal drop by phrase -
|
|
// for example: in case user is used always add _USER{ID} since that will be the key dropped on user update
|
|
// however if company is involved on all company changes a key containing COMPANIES_TO will be dropped
|
|
//------------------------------------------------------------------
|
|
// [COMPANIES_TO][_USER{ID}] - COMPANIES_TO_USER12
|
|
CONST STORE_LANG_KEY = '_LANG';
|
|
CONST STORE_USER_KEY = '_USER';
|
|
CONST STORE_SESSION = '_SESSION';
|
|
CONST STORE_COMP_KEY = '_COMPANY';
|
|
CONST STORE_COMP_TO_KEY = '_COMPANIES_TO';
|
|
CONST STORE_COMP_SETTINGS = '_COMPANIES_SETTINGS';
|
|
CONST STORE_SUBSCRIPTION_KEY = '_SUBSCRIPTION';
|
|
CONST STORE_SVG_KEY = '_SVG_ICONS';
|
|
|
|
|
|
|
|
private $module;
|
|
|
|
public $enabled = true;
|
|
public $installed = false;
|
|
public $prefix = 'APC';
|
|
|
|
private $bApcu = false;
|
|
private $expire = array();
|
|
private $expireTime = 2592000; // 60*60*24*30 ~ 1 month
|
|
|
|
CONST APC_KEY = 'APC_DISABLED';
|
|
CONST APC_MIN = 60;
|
|
CONST APC_HOUR = 3600;
|
|
|
|
/**
|
|
* Class constructor
|
|
*/
|
|
public function __construct() {
|
|
|
|
if(defined('APC_PREFIX'))
|
|
$this->prefix = APC_PREFIX;
|
|
$this->module = new cApcModule($this->prefix);
|
|
|
|
// Check if extension loaded & enabled - either of one
|
|
$this->installed = extension_loaded('apc') && ini_get('apc.enabled');
|
|
if(!$this->installed){
|
|
$this->bApcu = true;
|
|
$this->installed = extension_loaded('apcu') && ini_get('apc.enabled');
|
|
}
|
|
|
|
if($this->installed){
|
|
$this->expire = $this->module->getExpirationTimes();
|
|
// Check if APC manually not disabled
|
|
if(isset($this->expire[$this->prefix . self::APC_KEY]))
|
|
$this->enabled = false;
|
|
}else{
|
|
$this->enabled = false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function to check if data exits in cache storage and return data
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @param string $key - cache key
|
|
* @param bool $force - force cache check when installed
|
|
* @return mixed data
|
|
*/
|
|
public function check($key, $force = false){
|
|
if($key && $this->enabled){
|
|
$key = $this->prefix . $key;
|
|
if(isset($this->expire[$key]) || $this->installed && $force){
|
|
$data = ($this->bApcu ? apcu_fetch($key) : apc_fetch($key));
|
|
if(!!$data)
|
|
return $data;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to check if data exits in cache storage
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @param string $key - cache key
|
|
* @return bool true / false
|
|
*/
|
|
public function exists($key){
|
|
if($key && $this->enabled){
|
|
$key = $this->prefix . $key;
|
|
if(isset($this->expire[$key])){
|
|
return ($this->bApcu ? apcu_exists($key) : apc_exists($key));
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to replace & store apc cache data
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @param string $key - cache key
|
|
* @param mixed $data - cache data
|
|
* @param int|bool $ttl - time to live s
|
|
* @return bool true / false
|
|
*/
|
|
public function store($key, $data, $ttl = false){
|
|
if($this->enabled && $key){
|
|
$sFullKey = $this->prefix . $key;
|
|
$bStatus = ($this->bApcu ? apcu_store($sFullKey, $data) : apc_store($sFullKey, $data));
|
|
|
|
if($bStatus){
|
|
if(!isset($this->expire[$sFullKey]))
|
|
$this->expire[$sFullKey] = $sFullKey;
|
|
$this->module->addKey($key, ($ttl?$ttl:$this->expireTime));
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to mark data as expired
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @param string|array $key - cache key / key array
|
|
* @return bool true / false
|
|
*/
|
|
public function setExpired($key): bool
|
|
{
|
|
if (!is_array($key)) {
|
|
$key = [$key];
|
|
}
|
|
$this->module->dropKey($key);
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to mark data as expired for keys starting like sth
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @param string|array $key - cache key / key array
|
|
* @return bool true / false
|
|
*/
|
|
public function setExpiredStartingWith($key){
|
|
|
|
if(!is_array($key))
|
|
$key = array($key);
|
|
$this->module->dropKeyLike($key);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to mark data as expired for keys starting like sth
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @param string|array $keys - cache key / key array
|
|
* @return bool true / false
|
|
*/
|
|
public function setExpiredContaining($keys){
|
|
|
|
if(!is_array($keys))
|
|
$keys = array($keys);
|
|
$this->module->dropKeyContaining($keys);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to cleanup all data
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @return bool true / false
|
|
*/
|
|
public function cleanup(){
|
|
|
|
return $this->module->cleanup();
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to enable / disable apc cache from administration
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @param bool $status - true / false
|
|
* @return bool true / false
|
|
*/
|
|
public function enabled($status = true){
|
|
if($status)
|
|
$this->setExpired(self::APC_KEY);
|
|
else
|
|
if($this->installed)
|
|
$this->store(self::APC_KEY, true, 60*60*24*365);
|
|
return $this->installed;
|
|
}
|
|
|
|
/**
|
|
* Function to get apc / apcu cache user data
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @return mixed
|
|
*/
|
|
public function getUserCacheData(){
|
|
|
|
return ($this->bApcu ? apcu_cache_info() : apc_cache_info('user'));
|
|
}
|
|
|
|
/**
|
|
* Function to clear apc cache
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB APC
|
|
* @return void
|
|
*/
|
|
public function clearCache(){
|
|
if($this->installed){
|
|
if($this->bApcu){
|
|
apcu_clear_cache();
|
|
}else{
|
|
apc_clear_cache();
|
|
apc_clear_cache('user');
|
|
}
|
|
}
|
|
}
|
|
}
|