<?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. A*寻路算法的探寻与改良(一)

    A*寻路算法的探寻与改良(一) by:田宇轩                                                                    第一部分:这里我们主 ...

  2. The equation - SGU 106(扩展欧几里得)

    题目大意:有一个二元一次方程,给出系数值和x与y的取值范围,求出来总共有多少对整数解. 分析:有以下几点情况. 1,系数a=0, b=0, 当c != 0的时候结果很明显是无解,当c=0的时候x,y可 ...

  3. C# Excel操作类

    /// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...

  4. Java中的一些常见错误

    1.空指针错误 在java数组的使用中,有时候需要对字符串数组中的元素进行对比.那么当元素不为null时,程序会正常运行:然而,一旦对比的元素为null,那么程序就会出现空指针错误. 解决方法:加入保 ...

  5. 【设计模式 - 9】之装饰者模式(Decorator)

    1      模式简介 装饰者模式允许向一个现有的对象添加新的功能,同时又不改变其结构. 装饰者模式的思路是用"调料"对象将原始对象进行层层包裹,同时其属性.动作层层传递,达到最终 ...

  6. 3高并发server:多路IO之epoll

     1 epoll epoll是Linux下多路复用IO接口select/poll的增强版本号,它能显著提高程序在大量并.发连接中仅仅有少量活跃的情况下的系统CPU利用率,由于它会复用文件描写叙述符 ...

  7. CMAKE 学习

    http://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_01.html

  8. web.xml常用标签整理(不定期更新)

    <?xml version="1.0" encoding="UTF-8"?><!-- 标明使用的XML版本和文档编码,此项必须位于第一行,之前 ...

  9. 自定义控件(视图)2期笔记10:自定义视图之View事件分发机制("瀑布流"的案例)

    1. Touch事件的传递:   图解Touch事件的传递,如下: 当我们点击子View 02内部的Button控件时候,我们就触发了Touch事件. • 这个Touch事件首先传递给了顶级父View ...

  10. 禁止鼠标多次点击选中div中的文字

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>Fire ...