<?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. Space Ant - POJ 1696 (凸包)

    题目大意:给一些散列点然后初始点是坐标最下面最左面的点,然后只能往左走,求出来最多可以经过多少个点,把序号输出出来.   分析:先求出来初始的点,然后不断排序找出来最近的凸点....复杂度是 n^2* ...

  2. test、exec、match区别

    test.exec.match的简单区别 1.test test 返回 Boolean,查找对应的字符串中是否存在模式. var str = "1a1b1c"; var reg = ...

  3. jsp的include两种使用方法区别

    指令include是将被包含页面中的代码复制粘贴到主页面中,最后编译形成主页面的类文件(一个). 指令include中file属性的值必须是项目中已存在的文件,否则主页面报异常. 指令include如 ...

  4. UIView与CALayer的区别,很详细

    研 究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不多,看得人云山雾罩,感觉,写那篇东西的人, ...

  5. 23web app实现上下左右滑动

    转载请说明出处:http://blog.csdn.net/wowkk/article/category/1619287 (创意系列) /*最近项目须要苹果电脑,假设您支持学生创业并愿意赞助我们一台,请 ...

  6. lua字符匹配

    匹配下列格式的数据中的 source和MAC地址: Chain WiFiDog_br-lan_Outgoing (1 references) pkts bytes target prot opt in ...

  7. 动态设置布局LayoutInflater

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象.LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是 ...

  8. Qt 学习之路:文件

    文件操作是应用程序必不可少的部分.Qt 作为一个通用开发库,提供了跨平台的文件操作能力.从本章开始,我们来了解下 Qt 的文件以及输入输出的功能,也就是 I/O 系统. Qt 通过QIODevice提 ...

  9. Apple-Watch开发

    Apple Watch界面设计规范(4) - 通知 Apple Watch界面设计规范(3) - Glance Apple Watch界面设计规范(2) - 应用解析 Apple Watch界面设计规 ...

  10. Junit简介和常用API

    测试几个的概念 白盒测试——把测试对象看作一个打开的盒子,程序内部的逻辑结构和其他信息对测试人员是公开的. 回归测试——软件或环境的修复或更正后的“再测试”,自动测试工具对这类测试尤其有用. 单元测试 ...