您的位置:首页 > 数据库 > Memcache

比较省资源的PHP简单的MEMCACHE助手类代码分享

2014-07-13 00:00 441 查看
摘要: 这个助手类主要特点,Memcache连接复用,一个http请求只使用一个Memcache连接,使用Key前缀,使多个项目共享Memcache实例,这个助手类可用于任意框架(codeigniter可放于helper文件夹下),很不错吧!去试试吧!

<?php if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
* 这个助手类主要特点,Memcache连接复用,一个http请求只使用一个Memcache连接
* 使用Key前缀,使多个项目共享Memcache
*/
class Mcache
{
static $conn = null;

static function connect()
{
//如果已经有连接,则不在创建新的连接
if (is_object(self::$conn)) {
return self::$conn;
}

$server = array(
array(
'host' => '127.0.0.1',
'port' => '11211'
)
);

self::$conn = new Memcache;
for ($i = 0; $i < count($server); $i++) {
self::$conn->addServer($server[$i]['host'], $server[$i]['port'], false);
}

return self::$conn;
}

/**
* 当要使用助手类没有封装的Memcache方法时,用这个方法获取key
* @param $key
* @return string
*/
static function key($key)
{
return md5('zzj.net_' . $key);
}

static function read($key)
{
$key = md5('zzj.net_' . $key);

$ret = null;
if ($conn = self::connect($key)) {
$ret = $conn->get($key);
}
return $ret;
}

static function write($key, $val, $expire = 0, $flag = 0)
{
$key = md5('zzj.net_' . $key);

$ret = null;
if ($conn = self::connect($key)) {
$ret = $conn->set($key, $val, $flag, $expire);
}
return $ret;
}

static function delete($key, $expire = 0)
{
$key = md5('zzj.net_' . $key);

$ret = null;
if ($conn = self::connect($key)) {
$ret = $conn->delete($key, $expire);
}
return $ret;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Memcache php