<?php
if (!defined('LIB_CACHE_ENABLE'))
{
define('LIB_CACHE_ENABLE', 0);
Logger::getLogger('cache')->error('setting LIB_CACHE_ENABLE is not defined, ' .
'turning cache off. define LIB_CACHE_ENABLE to 1 to enable cache');
}
interface Cache
{
public function setKey($key);
public function get($key = null);
public function set($value, $expire = 3600, $key = null);
public function delete($key = null);
public function getInstance();
}
/**
* todo: docme
* @author Cory
* @version 1.0
*/
abstract class AbstractCache
{
protected $_log;
protected $_key;
protected $_origKey;
const CACHE_MISS = 'CACHEMISS';
/**
* todo: docme
*/
protected function __construct()
{
$this->_log = Logger::getLogger('cache');
$this->_key = null;
}
/**
* todo: docme
* @param string $key the new cache key to use for the next calls to get, set, delete
* if $key is not a string, it will be serialized()
*/
public function setKey($key)
{
$this->_origKey = $key;
if (!is_string($key))
$key = serialize($key);
$this->_key = $key;
// hash it if we need to
if (strspn($this->_key, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-') != strlen($this->_key))
$this->_key = md5($this->_key);
$this->_log->trace("new key: [{$this->_origKey}] => {$this->_key}");
return $this;
}
/**
* @return mixed something that was cached
*/
abstract public function get($key = null);
/**
* @return true on success
*/
abstract public function set($value, $expire = 3600, $key = null);
/**
* @return true on success
*/
abstract public function delete($key = null);
}