【php】キャッシュクラスの作成:ファイルキャッシュとmemcacheキャッシュをサポート
4966 ワード
<?php
// : mysqli
abstract class Abstract_Cache {
protected $cache_time;
public function set_cache_time($cache_time) {
$this->cache_time = $cache_time;// ,
}
abstract function &get($key);
abstract function set($key, $data);
abstract function add($key, $data);
abstract function del($key);
abstract function get_key($key);
abstract function flush();
}
class cache_file extends Abstract_Cache {
private $cache_dir;
public function set_cache_dir($cache_dir = './cache/') {
$this->cache_dir = $cache_dir;
}
public function mkpath($dir) {
//
return is_dir($dir) or ($this->mkpath(dirname($dir)) and (mkdir($dir, 0777) and chmod($dir,0777)));
}
public function &get($key, $cache_time = '') {
if(empty ($cache_time)) {
$cache_time = $this->cache_time;
} else {
$cache_time = intval($cache_time);
}
$cache_encode_key = $this->get_key($key);
//
$filename = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/'.$cache_encode_key.".cache.php";
//
if(!is_file($filename) || time() - filemtime($filename) > $cache_time) {
return false;
} else {
return unserialize(file_get_contents($filename));// ,
}
}
public function set($key,$data) {
$cache_encode_key = $this->get_key($key);
$cache_dir = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/';
$filename = $cache_dir.$cache_encode_key.".cache.php";
if( $this->mkpath($cache_dir) ) {
$out = serialize($data);
file_put_contents($filename, $out);
}
}
public function add($key, $data) {
$cache_encode_key = $this->get_key($key);
$cache_dir = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/';
$filename = $cache_dir.$cache_encode_key.".cache.php";
if(is_file($filename)) {
return false;
} else {
$this->set($key, $data);
}
}
public function del($key) {
$cache_encode_key = $this->get_key($key);
$cache_dir = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/';
$filename = $cache_dir.$cache_encode_key.".cache.php";
if(is_file($filename)) {
return false;
} else {
@unlink($filename);
}
}
public function flush() {
$this->remove_dir($this->cache_dir,false);
}
public function get_key($key){
Return md5(CACHE_KEY.$key);
}
private function remove_file($dir) {
if (is_dir($dir)) {
$dh = opendir($dir);
while (false !== ($file = readdir($dh))) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
@unlink($fullpath);
} else {
$this->remove_dir($fullpath);
}
}
}
closedir($dh);
}
}
private function remove_dir($dir, $self = true) {
$this->remove_file($dir);
if ($self == true && is_dir($dir)) {
rmdir($dir);
}
}
}
class cache_memcache extends Abstract_Cache {
var $_memcache = null;
/**
*
*
* @author
* @param array $options
* @return bool
*/
function __construct($memcache_conf)
{
$this->_memcache = new Memcache;
return $this->_memcache->connect($memcache_conf['host'], $memcache_conf['port']);
}
/**
*
*
* @author
* @param none
* @return void
*/
function set($key, $value, $ttl = null)
{
$cache_encode_key = $this->get_key($key);
return $this->_memcache->set($cache_encode_key, $value, $ttl);
}
/**
*
*
* @author
* @param string $key
* @return mixed
*/
function &get($key)
{
$cache_encode_key = $this->get_key($key);
return $this->_memcache->get($cache_encode_key);
}
/**
*
*
* @author
* @param none
* @return void
*/
function add($key, $value, $ttl = null)
{
$cache_encode_key = $this->get_key($key);
return $this->_memcache->add($cache_encode_key, $value, $ttl);
}
/**
*
*
* @author
* @return bool
*/
function flush()
{
return $this->_memcache->flush();
}
function del($key)
{
$cache_encode_key = $this->get_key($key);
return $this->_memcache->delete($key);
}
public function get_key($key)
{
$cache_encode_key = $this->get_key($key);
Return md5(CACHE_KEY.$key);
}
}