<?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);
}
}
【php】缓存类的编写:支持文件缓存和memcache缓存
最新推荐文章于 2024-05-21 09:34:51 发布