<?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. first blog编程之美-----计算1的个数

    根据以下总结写出以下程序,总结来源于网上 感想:得硬着头皮找规律 #include   int count1(int n) {         int i=1;         int count=0 ...

  2. appium api

    AppiumDriver getAppStrings()      默认系统语言对应的Strings.xml文件内的数据.iOS driver.getAppStrings(Stringlanguage ...

  3. c#语言基础编程—string

    引言 在c#中经常会有相关的string的操作,string类型为引用类型,集成于Object,所以会有四个方法.详情可见 值类型和引用类型的区别 里面详细介绍了,值类型和引用类型的区别和应用场合,所 ...

  4. xml学习篇(二) ----JSON 和XML对比

    在比较JSON和XML之前,我们先来上一堂关于数据格式的简要历史(更准确的说,是关于XML的始祖): 早在1970年,IBM开发了一种叫Generalized Markup Language的标记语言 ...

  5. 用Octopress在Github pages上写博客

    安装Git环境 下载msysgit(git for windows),并安装. 可以选择安装TortoiseGit,这个在windows的资源管理器里装了很多git的右键菜单,对git命令行不熟悉的同 ...

  6. 《Linux设备驱动开发详解(第2版)》配套视频登录51cto教育频道

    http://edu.51cto.com/course/course_id-379-page-1.html http://edu.51cto.com/course/course_id-379-page ...

  7. SpringMVC DispatcherServlet 说明与web配置

    使用Spring MVC,配置DispatcherServlet是第一步. DispatcherServlet是一个Servlet,所以能够配置多个DispatcherServlet. Dispatc ...

  8. 教你搞定Android自定义View

    Android App开发过程中,很多时候会遇到系统框架中提供的控件无法满足我们产品的设计需求,那么这时候我们可以选择先Google下有没有比较成熟的开源项目可以让我们用,当然现在Github上面的项 ...

  9. Linux守护进程详解(init.d和xinetd)

    一 Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程来执行的. ...

  10. 第九章------HAL

    HAL是建立在Linux驱动之上的的程序库,他是一个应用层,它的作用保护内部私有.有一种是位于Android层次的系统运行库层,Linux中只保留与寄存器相互的代码,通过HAL进行数据的传递. 为An ...