ElixirVault/www/system-brs-core/library/cApc/cApcModule.php

169 lines
4.2 KiB
PHP

<?php
namespace BRSystems\Core\Library\cApc;
use BRSystems\Core\Library\cDb\cDb;
/**
* APC Module
*
* @category LIB APC / APCu
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @since Version 2.1
*/
class cApcModule
{
public $db;
private $prefix = 'apc_';
/**
* Class constructor
*/
public function __construct($pfix) {
$this->db = cDb::getInstance(DB_HOST, DB_USR, DB_PWD, DB_PORT, DB_NAME);
$this->prefix = $pfix;
}
/**
* Function to get all cache expire times
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category APC Module
* @return array with data with times
* @version 1.1
*/
public function getExpirationTimes(){
$time = time();
$sSql = "
SELECT
DISTINCT ex.key
FROM
temp_cache_exp ex
WHERE
ex.expires > {$time}
";
$this->db->query($sSql);
return $this->db->getFieldBasedArray('key');
}
/**
* Function to add apc expire key to db
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category APC Module
* @param string $key - cache key
* @param int $lTime - cache live time
* @return bool true / false
* @version 1.1
*/
public function addKey($key, $lTime){
$time = time();
$this->dropKey(array($key));
$this->db->_queryInsert('temp_cache_exp', array(
'key' => $this->prefix . $key,
'inserted' => $time,
'expires' => $time + $lTime,
));
}
/**
* Function to drop apc cache key
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category APC Module
* @param array $key - cache keys to drop
* @return bool true / false
* @version 1.1
*/
public function dropKey($key){
if(is_array($key) && count($key)){
$sSql = "
DELETE
FROM
temp_cache_exp
WHERE
`key` IN ('{$this->prefix}" . implode("','{$this->prefix}", $key) . "')
";
return $this->db->execute($sSql);
}
}
/**
* Function to drop apc cache key starting like passed values
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category APC Module
* @param string $key - cache key to drop
* @return bool true / false
* @version 1.1
*/
public function dropKeyLike($key){
if(is_array($key) && count($key)){
$sSql = "
DELETE
FROM
temp_cache_exp
WHERE
`key` LIKE '{$this->prefix}" . implode("%' OR `key` LIKE '{$this->prefix}", $key) . "%'
";
return $this->db->execute($sSql);
}
}
/**
* Function to drop apc cache key starting like passed values
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category APC Module
* @param array $keys - cache keys to drop
* @return bool true / false
* @version 1.1
*/
public function dropKeyContaining($keys){
if(is_array($keys) && count($keys)){
$sSql = "
DELETE
FROM
temp_cache_exp
WHERE
`key` LIKE '{$this->prefix}%' AND (
`key` LIKE '%" . implode("%' OR `key` LIKE '%", $keys) . "%'
)
";
return $this->db->execute($sSql);
}
}
/**
* Function to clear all old apc cache keys
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category APC Module
* @return bool true / false
* @version 1.1
*/
public function cleanup(){
$time = time();
$sSql = "
DELETE
FROM
temp_cache_exp
WHERE
expires < {$time}
";
$this->db->execute($sSql);
}
}