php操作memcache共享缓存方法





采用memcache的前提下,是需要在服务器端被配置memcahche环境!

证实memcahce经过正常的连接可以在程序中使用!

<?php
/**
* Memcache缓存操作
* @update http://www.lai18.com
*/
class MCache extends Object implements CacheFace
{
private $mem = null; //Mem对象 private $sId = 1; //servier服务ID /**
* 初始化Memcache
*
* @return Object
*/
public function __construct()
{
if ( !class_exists('Memcache') )
{
throw new QException('PHP extension does not exist: Memcache');
}
$this->mem = new Memcache();
} /**
* 链接memcahce服务
*
* @access private
* @param string $key keyword
* @param string $value 缓存内容
* @return array
* @update http://www.lai18.com
*/
private function connect( $sid )
{
$file = $this->CacheFile();
require $file;
if(! isset($cache) )
{
throw new QException('缓存配置文件不存在'.$file);
}
$server = $cache[$this->cacheId];
$sid = isset($sid) == 0 ? $this->sId : $sid;//memcache服务选择
if ( ! $server[$sid])
{
throw new QException('当前操作的缓存服务器配置文件不存在');
}
$host = $server[$sid]['host'];
$port = $server[$sid]['port'];
try {
$this->mem->connect( $host , $port );
} catch (Exception $e) {
exit('memecache连接失败。错误信息:'. $e->getMessage());
}
} /**
* 写入缓存
*
* @access private
* @param string $key keyword
* @param string $value 缓存内容
* @return array
*/
public function set( $key , $value , $sid , $expire = 0)
{
$data = $this->get($key , $sid); //假设已经存在key值
if( $data )
{
return $this->mem->set( $key , $value ,MEMCACHE_COMPRESSED , $expire);
} else {
return $this->mem->add( $key , $value ,MEMCACHE_COMPRESSED , $expire);
}
} /**
* 读取缓存
*
* @access private
* @param string $key keyword
* @param int $sid 选择第几台memcache服务器
* @return array
*/
public function get( $key , $sid)
{
$this->connect( $sid );
return $this->mem->get($key);
} /**
* 清洗(删除)已经存储的全部的元素
*
* @access private
* @return array
*/
public function flush()
{
$this->connect();
return $this->mem->flush();
}
/**
* 删除缓存
*
* @access private
* @param string $key keyword
* @param int $sid 选择第几台memcache服务器
* @return array
*/
public function remove( $key , $sid)
{
$this->connect();
return $this->mem->delete($key);
} /**
* 析构函数
* 最后关闭memcache
*/
public function __destruct()
{
/*if(! $this->mem)
{
$this->mem->close();
}*/
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

整理php操作memcache缓存为基础的方法的更多相关文章

  1. 详解PHP操作Memcache缓存技术提高响应速度的方法

    本文转载http://blog.csdn.net/zhihua_w  不错的博客,仅供本人学习之用 一般来说,如果并发量不大的情况,使不使用缓存技术并没有什么影响,但如果高并发的情况,使用缓存技术就显 ...

  2. php操作memcache缓存基本方法

    memcache 是一个高效的分布式的内存对象缓存系统,他可以支持把php的各种数据(数组,对象,基本数据类型)放在它管理的内存中 1.代码使用 <?php //连接 $mem = new Me ...

  3. memcached基于socket访问memcache缓存服务器

    memcached基于socket访问memcache缓存服务器 操作memcache常用三种方法: .memcache基于php_memcache.dll扩展(php扩展) .memcached基于 ...

  4. 使用Memcache缓存mysql数据库操作的原理和缓存过程浅析

    转载自脚本之家 http://www.jb51.net/article/51831.htm  作者:忙碌的松鼠 对于大型网站如facebook,ebay等网站,如果没有Memcache做为中间缓存层, ...

  5. php操作memcache的用法、详解和方法介绍

    1.简介 memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问. memcache也提供用于通信对话(sess ...

  6. DedeCMS V5.7开启memcache缓存的方法配置说明

    一.安装Memcache服务:1.1.linux下的Memcache安装:-------------------------1. 下载 memcache的linux版本,注意 memcached 用 ...

  7. PHP之Memcache缓存详解

         Mem:memory缩写(内存):内存缓存 1.  断电或者重启服务器内存数据即消失,即临时数据: Memcache默认端口:11211 存入方式:key=>>value    ...

  8. 前端学PHP之PHP操作memcache

    × 目录 [1]安装 [2]连接 [3]增删改查[4]分布式[5]状态[6]安全[7]应用 前面的话 和访问mysql服务器类似,PHP也是作为客户端API访问memcached服务器的,所以同样需要 ...

  9. Memcache缓存系统构建一

    在如今这个高效率的社会中,怎样将这个高效率应用到自己的程序中,是一个值得追寻和值得探讨的问题.因为这个memcache能够很好的提高检索速度,提升用户体验,而且重要的是减少数据库的访问.这就大大的提高 ...

随机推荐

  1. java HashMap中出现反复的key, 求解释

    上代码: Person p1 = new Person("xiaoer",1); Person p2 = new Person("san",4); Map< ...

  2. 软件測试系统文章(文件夹&amp;链接在此)

    前言 我会在此账号上写一系列关于软件測试的文章,故在此置顶软件測试系列文章的文件夹和链接,以方便大家阅读! 文件夹 软件測试系列之入门篇(一) 软件測试系列之了解篇(二) 软件測试系列之黑白盒(三) ...

  3. sql dateDiff函数

    当月的数据select * from MOPICK where dateDiff(m,getdate(),START_DATE)=0

  4. 使用邮件发送执行时间久的SQL语句

    ALTER proc [dbo].[usp_EmailLongRunningSQL] as begin declare@LongRunningQueries AS TABLE ( lrqId int ...

  5. UML简单梳理类图

    依赖 Dependency Class Car{} Class Person{ int a; static int b public void buy(Car c){ int c; .... } } ...

  6. ps设计资料整理

    零基础学会建立一个简单化妆品网站—前台设计篇1[PS画草图] http://xiebiji.com/2008/09/huazhuang4/?wptheme=Plainscape&ie=1 PS ...

  7. 关于程序猿怎样降低程序Bug的若干建议

    毫无疑问,程序猿是善于思考问题的一族. 一个程序的编写都是通过:思考.设计.编写.调试.測试以及执行这些主要的阶段. 但大部分程序猿都有一个问题就是不太愿意測试自己的代码. 他们草草的调式完毕以后就觉 ...

  8. 如何区分MNO和MVNO

    MVNO(Mobile Virtaul Network Operator)虚拟网络运营商,没有自己的物理网络,租用MNO(Mobile Network Operator)网络提供的网络服务. 我们知道 ...

  9. cygwin,在win中开发linux程序

    cygwin,在win中开发linux程序 http://www.cygwin.cn/site/info/show.php?IID=1001  很多用windows的朋友不习惯于用linux的开发环境 ...

  10. 【BZOJ1014】【JSOI2008】火星人prefix Splay处理区间,hash+dichotomy(二分)check出解

    题意不赘述了,太清晰了. 说题解:首先依据原字符串建立SPT.首尾建议多加一个空白字符. 给一个树构图,依照平衡树的前后大小顺序性质能够使它们始终维持为一个序列,而且能够通过rank找到序列的第k个. ...