<?php

/**
* memcache 操作实现
* @author timeless
*/
class Memcache_manage { //CI原始的信息
private $_ci;
private $_memcache_prefix;
private $host;
private $port;
private $expire;
private $weight; /**
* 读取配置文件信息然后更新
* @access public
*/
public function memcache($flag = 'default') {
//要你自定义的类库中访问CodeIgniter的原始资源,你必须使用 get_instance() 函数.这个函数返回一个CodeIgniter super object.
$this->_ci = &get_instance();
//记载memcache 缓存配置     //memcached 中的数据     /* memcahed.php 文件中的配置信息 CI框架中
$config = array(
//现在是单独的memcache 服务器 以后可以添加多个 只需要 mem 对象添加 addserver
'default' => array(
'hostname' => '127.0.0.1',
'port' => '11211',
'weight' => '1',
//100分钟
'expire' => '6000',
'memcache_prefix'=>'',
),
);
     */
$this->_ci->config->load('memcached', FALSE, TRUE);
//获取配置文件
$default_conf = $this->_ci->config->item('default');
$this->host = $default_conf['hostname'];
$this->port = $default_conf['port'];
$this->expire = $default_conf['expire'];
$this->weight = $default_conf['weight'];
$this->_memcache_prefix = $default_conf['memcache_prefix'];
$this->connected_server = array();
$this->_connect();
} /**
* 连接memcache 数据库
* @access private
*/
private function _connect() {
if (function_exists('memcache_connect')) {
$this->cache = new Memcache;
$this->_connect_memcached();
}
} /**
* 添加memcache 服务器
* @access private
*/
private function _connect_memcached() {
$error_display = ini_get('display_errors');
$error_reporting = ini_get('error_reporting');
if ($this->cache->addServer($this->host, $this->port, TRUE, $this->weight)) {
$this->connected_server[] = $this->host;
}
ini_set('error_reporting', $error_reporting);
} public function get($key) {
if (empty($this->connected_server)) {
return false;
}
return $this->cache->get($this->key_name($key));
} public function set($key, $data) {
if (empty($this->connected_server)) {
return false;
}
return $this->cache->set($this->key_name($key), $data, 0, $this->expire);
} public function set_expire($key, $data, $expire) {
if (empty($this->connected_server)) {
return false;
}
return $this->cache->set($this->key_name($key), $data, 0, $expire);
} public function replace($key, $data) {
if (empty($this->connected_server)) {
return false;
}
return $this->cache->replace($this->key_name($key), $data, 0, $this->expire);
} public function delete($key, $when = 0) {
if (empty($this->connected_server)) {
return false;
}
return $this->cache->delete($this->key_name($key), $when);
} public function flush() {
return $this->cache->flush();
} /**
* @Name: 生成md5加密后的唯一键值
* @param:$key key
* @return : md5 string
* */
private function key_name($key) {
return md5(strtolower($this->_memcache_prefix . $key));
} }

memcache 操作类的更多相关文章

  1. PHP 对 memcache操作类

    <span style="font-size:18px;">class myMemcache { private $memcache; /** * 一般建议这2个值做成 ...

  2. Memcache操作类

    using Memcached.ClientLibrary; using System; using System.Collections.Generic; using System.Linq; us ...

  3. 设计模式之PHP项目应用——单例模式设计Memcache和Redis操作类

    1 单例模式简单介绍 单例模式是一种经常使用的软件设计模式. 在它的核心结构中仅仅包括一个被称为单例类的特殊类. 通过单例模式能够保证系统中一个类仅仅有一个实例并且该实例易于外界訪问.从而方便对实例个 ...

  4. ecshop的Mysql操作类

    摘要,这是直接摘抄的ecshop的mysql操作类:不过他这里的缓存是用的文件缓存,我们如果想直接使用,可以替换成memcache的或者redis的! <?php /** * ECSHOP MY ...

  5. 3.NetDh框架之缓存操作类和二次开发模式简单设计(附源码和示例代码)

    前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...

  6. 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~

    最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...

  7. JQuery操作类数组的工具方法

    JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...

  8. Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)

    今天在开发一个简单查询时,发现我的Lambda操作类的GetValue方法无法正确获取枚举类型值,以至查询结果错误. 我增加了几个单元测试来捕获错误,代码如下. /// <summary> ...

  9. Util应用程序框架公共操作类(九):Lambda表达式扩展

    上一篇对Lambda表达式公共操作类进行了一些增强,本篇使用扩展方法对Lambda表达式进行扩展. 修改Util项目的Extensions.Expression.cs文件,代码如下. using Sy ...

随机推荐

  1. lightoj 1037 状态压缩

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1037 #include<cstdio> #include<cstri ...

  2. 长沙Uber优步司机奖励政策(1月25日~1月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 平时Error记录

    The Windows Firewall on this machine is currently 1.This row already belongs to another table. DataT ...

  4. 【Android - 框架】之Retrofit的使用

    Retrofit是Square公司发布的一个可以应用在Android和Java中的Http客户端访问框架,其底层应用的是OkHttp. 在这个帖子中,我们以下面这个Http请求为例: https:// ...

  5. IOS 多个ImageView图片层叠透明区域点击事件穿透

    经常用到多个透明图片层叠,但又需要获取不同图片的点击事件,本文实现图片透明区域穿透点击事件 实现人体各个部位点击 - (BOOL) pointInside:(CGPoint)point withEve ...

  6. [AngularJS + Webpack] Requiring CSS & Preprocessors

    Making your CSS modular is a difficult thing to do, but using Webpack makes this so much easier. By ...

  7. java Map实现的cache manager

    一个模仿memcached的JAVA虚拟缓存工具,可以缓存java对象 import java.io.ByteArrayInputStream; import java.io.ByteArrayOut ...

  8. JSP-注释,脚本元素,指令

    jsp中的注释 jsp隐藏注释 完全对客户机隐藏,不会将注释内容发送给客户机. 其语法格式为:<%-- comment --%> comment为要添加的文本注释内容. HTML注释 不完 ...

  9. #IOS-navigation中左滑pop的三种方法

    IOS-navigation中左滑pop的三种方法 系统自带pop方法 如果我们没有对navigation中的back按钮进行自定义,我们可以直接使用系统自带的左滑pop方法.但是如果我们对back按 ...

  10. C# WinForm获取程序所在路径方法

    多个获取WinForm程序所在文件夹路径的方法,收藏备忘. 1)获取当前进程的完整路径,包含文件名(进程名). 代码:string str =this.GetType().Assembly.Locat ...