Memcache类(From ThinkPHP/Lib/Think/Util/Cache/CacheMemcache.class.php)

<?php
/**
+------------------------------------------------------------------------------
* Memcache缓存类
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Util
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/

class CacheMemcache extends Cache
{//类定义开始

/**
* 架构函数
*/

function __construct($options='')
{
if(empty($options)) {
$options = array
(
'host' => '127.0.0.1',
'port' => 11211,
'timeout' => false,
'persistent' => false
);
}
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
$this->handler = new Memcache;
$this->connected = $options['timeout'] === false ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
$this->type = strtoupper(substr(__CLASS__,6));
}

/**
* 是否连接
*/

private function isConnected()
{
return $this->connected;
}

/**
* 读取缓存
*/

public function get($name)
{
return $this->handler->get($name);
}

/**
* 写入缓存
*/

public function set($name, $value, $ttl = null)
{
if(isset($ttl) && is_int($ttl))
$expire = $ttl;
else
$expire = $this->expire;
return $this->handler->set($name, $value, 0, $expire);
}

/**
* 删除缓存
*/

public function rm($name, $ttl = false)
{
return $ttl === false ?
$this->handler->delete($name) :
$this->handler->delete($name, $ttl);
}

/**
* 清除缓存
*/

public function clear()
{
return $this->handler->flush();
}
} //类定义结束


其中Cache类定义:

<?php
/**
+------------------------------------------------------------------------------
* 缓存管理类
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Util
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/

class Cache extends Think
{//类定义开始

/**
* 是否连接
*/

protected $connected;

/**
* 操作句柄
*/

protected $handler;

/**
* 缓存存储前缀
*/

protected $prefix='~@';

/**
* 缓存连接参数
*/

protected $options = array();

/**
* 缓存类型
*/

protected $type;

/**
* 缓存过期时间
*/

protected $expire;

/**
* 连接缓存
*/

public function connect($type='memcache',$options=array())
{
$cachePath = dirname(__FILE__).'/Cache/';
$cacheClass = 'Cache'.ucwords(strtolower(trim($type)));
require_cache($cachePath.$cacheClass.'.class.php');
if(class_exists($cacheClass))
$cache = new $cacheClass($options);
else
throw_exception(L('_CACHE_TYPE_INVALID_').':'.$type);
return $cache;
}

public function __get($name) {
return $this->get($name);
}

public function __set($name,$value) {
return $this->set($name,$value);
}

public function __unset($name) {
$this->rm($name);
}


public function setOptions($name,$value) {
$this->options[$name] = $value;
}

public function getOptions($name) {
return $this->options[$name];
}


/**
* 取得缓存类实例
*/

static function getInstance()
{
$param = func_get_args();
return get_instance_of(__CLASS__,'connect',$param);
}
}//类定义结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值