缓存MEMCACHE php调用

在项目中,涉及大访问量时,合理的使用缓存能减轻数据库的压力,同时提升用户体验。即在非实时性的需求的前提下,一小段时间内(若干秒),用于显示的数据从缓存中获取的,而不用直接读取数据库,能有效的减少数据库的读取压力。这里记录一下php语言使用memcache的情形:

首先,我们建立一个memcachepool,可以根据不同的配置读取,生成不同的memcache实例。用到$memcache->addServer($host,$port,$flag);向连接池中添加一个memcache服务器。代码示例如下

 1 class memcachePool{
 2      private static $instance;
 3      private $memcacheList = array();
 4     private function __construct(){
 5
 6     }
 7      public static function getInstance(){
 8          if(self::$instance != null)
 9              return self::$instance;
10          self::$instance = new memcachePool();
11          return self::$instance;
12      }
13     /**
14      * get memcache object from pool
15      * @param  [type] $host 服务器
16      * @param  [type] $port 端口
17      * @param  [type] $flag 控制是否使用持久化连接。默认TRUE
18      * @return [type]
19      */
20      public function getMemcache($host,$port,$flag){
21          if(isset($this->memcacheList[$host.$port]))
22              return $this->memcacheList[$host.$port];
23
24         $memcache = new Memcache();
25         // 向连接池中添加一个memcache服务器
26         $memcache->addServer($host,$port,$flag);
27         //开启大值自动压缩,第一个参数表示处理数据大小的临界点,第二个参数表示压缩的比例,默认为0.2
28         $memcache->setCompressThreshold(2000,0.2);
29         $this->memcacheList[$host.$port] = $memcache;
30         return $memcache;
31      }
32  }

接着实现一个包含memcache常用方法如add,set,get,flush,delete等的方法类,这里命名为dlufmemcache

 1 class dlufMemcache{
 2      private $memcache = null;
 3      function __construct($host,$port){
 4
 5        $this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true);
 6      }
 7     /**
 8      * memcache set value
 9      * @param [type]  $key 键
10      * @param [type]  $value 值
11      * @param integer $expire  到期的时间,如果此值设置为0表明此数据永不过期
12      * @param integer $flag 标志位 使用MEMCACHE_COMPRESSED指定对值进行压缩(使用zlib)
13      * @param [type]  $serializetype
14      */
15      public function set($key,$value,$expire=0,$flag=0,$serializetype=null){
16         if($serializetype == ‘json‘ && is_array($value)){
17             $value = json_encode($value);
18         }
19          $this->memcache->set($key,$value,$flag,$expire);
20      }
21     /**
22      * 从服务端查找元素
23      * @param  [type] $key
24      * @return [type]
25      */
26      public function get($key){
27          return $this->memcache->get($key);
28      }
29     /**
30      * 增加一个条目到缓存服务器
31      * @param [type]  $key
32      * @param [type]  $value
33      * @param integer $expire
34      * @param integer $flag
35      * @param [type]  $serializetype
36      */
37     public function add($key,$value,$expire=0,$flag=0,$serializetype=null){
38         if($serializetype == ‘json‘ && is_array($value)){
39             $value = json_encode($value);
40         }
41         $ret = $this->memcache->add($key,$value,$flag,$expire);
42         return $ret;
43     }
44     /**
45      * 清洗(删除)已经存储的所有的元素
46      * @return [type]
47      */
48     public function flush(){
49         return $this->memcache->flush();
50     }
51     /**
52      *  从服务端删除一个元素
53      * @param  [type] delete 参数:key要删除的元素的key 删除该元素的执行时间 timeout如果值为0,则该元素立即删除。
54      * @return [type]
55      */
56     public function delete($key){
57         $ret = $this->memcache->delete($key,0);
58         return $ret;
59     }
60  }

然后调用dlufmemcache:

1 $memcache = new dlufMemcache(‘127.0.0.1‘,11211);
2  $memcache->set(‘memcache‘,‘come on dluf&baidu !!!!!!‘);
3  $ret = $memcache->get(‘memcache‘);
4  echo print_r($ret,true);

运行输出可见:

http://php.net/manual/zh/class.memcache.php

缓存MEMCACHE php调用

时间: 2024-08-04 00:28:35

缓存MEMCACHE php调用的相关文章

使用缓存Memcache存储更新微信access token

关键字:Memcache access_token 更新 存储 7200 本文介绍如何使用缓存Memcache存储及更新 access token的方法. 一.Access Token access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效. 公众号可以使用AppID和AppSecret调用本接口来获取access_token.AppID和App

php5.4之分布式缓存memcache(windows7下安装配置)

一.安装memcache memcached在windows7上的安装问题 现在安装包:http://www.jb51.net/softs/44843.html   memcache的安装包 错误: 通过cmd命令行进入到D:\webEve\memcached(下载后的解压目录) 运行 memcached.exe -d install 报错" failed to install service or service already installed" 解决方法: www.2cto.c

缓存MEMCACHE 使用原子性操作add,实现并发锁

memcache中Memcache::add()方法在缓存服务器之前不存在key时, 以key作为key存储一个变量var到缓存服务器.我们使用add来向服务器添加一个键值对应,如果成功则添加,否则说明存在另一个并发作业在进行操作.通过add的原子性来判断是否要执行热点代码.具体代码需结合上一篇的php使用memcache.使用该方法控制并发需要考虑到缓存的有效期.缓存基于内存的特点. 实现一个包含锁,解锁,锁状态检查的类cacheLock: 1 class cacheLock{ 2 const

192.168.62.124python运维开发(十一)----python操作缓存memcache、redis

内容目录: 缓存 memcache redis memcache Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached基于一个存储键/值对的hashmap.其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信. Memcached安装配置 #安装依赖包 yum install libeve

自定义缓存Memcache 以及Cache

*****缓存存储配置项(读缓存和appsetting配直节点的问题 )***** MemCache经常用于数据不用经常改动的数据 不用每次都去数据库中读取,可以防止在系统的缓存中,缓存在内存中读取速度快,但是memcache(是可以分布的缓存)没有保存机制,服务器挂掉,memcache丢失 系统的配置节放在了appsettings配置节下  <add key=”mingzi” value=”assas” /> ConfigurationManager.appsetting[“mingzi”]

分布式缓存Memcache和Redis

引言 针对于现在计算机的CPU和网络设施,对应用程序来说,执行效率的瓶颈,已经不是代码的长度(实现同一个功能)和带宽了,而是,代码访问资源的过程,即:让我们的程序慢下来的罪魁祸首就是IO操作. 程序从硬盘上读取数据是一个非常花费时间的操作,因为我们现在所使用的硬盘是机械式的,你想机械的运行速度和电的速度,那是一个级别上的选手吗? 为了解决程序的瓶颈,人们提出了一种想法:使用空间换取时间.程序访问硬盘用的时间长,那就让数据放到内存中,让程序访问内存,这样不就节省了时间.这样确实剩下了我们程序获取数

缓存-memcache redies

一 memcache 1)server端 1.安装 下载:wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x.x ./configure && make && make test && sudo make install PS:依赖libevent yum install libevent-devel apt-get install libeve

83-高性能,分布式缓存memcache简析

一. memcached简介 memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in      speeding up dynamic web applications by alleviating database load. (由 LiveJournal旗下的Danga Interactive研发) #高性能,分布

分布式缓存——memcache原理

内容:1.什么是Memcached 2.MemCache和MemCached的区别 3.memcache访问模型 4.Memcached作为高速运行的分布式缓存服务器具有以下特点 5.Memcached的内存算法 6.Memcached的缓存策略             7.分布式算法(Consistent Hashing)             8. MemCache的特性和限制总结 1.什么是Memcached        MemCache是一个自由.源码开放.高性能.分布式的分布式内存