<?php if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
* 这个助手类主要特点,Memcache连接复用,一个http请求只使用一个Memcache连接,用域名(当然你也可以用其他的)作为key前缀,可以让多个项目共享memcache
* 使用Key前缀,使多个项目共享Memcache
* User: 线筝 http://h5b.net/
*/
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('h5b.net_' . $key);
}
static function read($key)
{
//用域名作为key前缀,可以让多个项目共享memcache
$key = md5('h5b.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('h5b.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('h5b.net_' . $key);
$ret = null;
if ($conn = self::connect($key)) {
$ret = $conn->delete($key, $expire);
}
return $ret;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
适用于任何php框架的高效的Memcache助手类
共1个文件
php:1个
5星 · 超过95%的资源 需积分: 9 16 下载量 137 浏览量
2013-07-16
19:41:33
上传
评论
收藏 947B ZIP 举报
温馨提示
这个助手类主要特点,Memcache连接复用,一个http请求只使用一个Memcache连接,使用Key前缀,使多个项目共享Memcache实例,这个助手类可用于任意框架(codeigniter可放于helper文件夹下).
资源推荐
资源详情
资源评论
收起资源包目录
Mcache_helper.zip (1个子文件)
Mcache_helper.php 2KB
共 1 条
- 1
资源评论
- baobeiwjw2013-09-07感觉还不错,很基础。。。
安子檬
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功