缓存MEMCACHE php调用
在项目中,涉及大访问量时,合理的使用缓存能减轻数据库的压力,同时提升用户体验。即在非实时性的需求的前提下,一小段时间内(若干秒),用于显示的数据从缓存中获取的,而不用直接读取数据库,能有效的减少数据库的读取压力。这里记录一下php语言使用memcache的情形:
首先,我们建立一个memcachepool,可以根据不同的配置读取,生成不同的memcache实例。用到$memcache->addServer($host,$port,$flag);向连接池中添加一个memcache服务器。代码示例如下
class memcachePool{
private static $instance;
private $memcacheList = array();
private function __construct(){
}
public static function getInstance(){
if(self::$instance != null)
return self::$instance;
self::$instance = new memcachePool();
return self::$instance;
}
/**
* get memcache object from pool
* @param [type] $host 服务器
* @param [type] $port 端口
* @param [type] $flag 控制是否使用持久化连接。默认TRUE
* @return [type]
*/
public function getMemcache($host,$port,$flag){
if(isset($this->memcacheList[$host.$port]))
return $this->memcacheList[$host.$port];
$memcache = new Memcache();
// 向连接池中添加一个memcache服务器
$memcache->addServer($host,$port,$flag);
//开启大值自动压缩,第一个参数表示处理数据大小的临界点,第二个参数表示压缩的比例,默认为0.2
$memcache->setCompressThreshold(2000,0.2);
$this->memcacheList[$host.$port] = $memcache;
return $memcache;
}
}
接着实现一个包含memcache常用方法如add,set,get,flush,delete等的方法类,这里命名为dlufmemcache
class dlufMemcache{
private $memcache = null;
function __construct($host,$port){
$this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true);
}
/**
* memcache set value
* @param [type] $key 键
* @param [type] $value 值
* @param integer $expire 到期的时间,如果此值设置为0表明此数据永不过期
* @param integer $flag 标志位 使用MEMCACHE_COMPRESSED指定对值进行压缩(使用zlib)
* @param [type] $serializetype
*/
public function set($key,$value,$expire=0,$flag=0,$serializetype=null){
if($serializetype == 'json' && is_array($value)){
$value = json_encode($value);
}
$this->memcache->set($key,$value,$flag,$expire);
}
/**
* 从服务端查找元素
* @param [type] $key
* @return [type]
*/
public function get($key){
return $this->memcache->get($key);
}
/**
* 增加一个条目到缓存服务器
* @param [type] $key
* @param [type] $value
* @param integer $expire
* @param integer $flag
* @param [type] $serializetype
*/
public function add($key,$value,$expire=0,$flag=0,$serializetype=null){
if($serializetype == 'json' && is_array($value)){
$value = json_encode($value);
}
$ret = $this->memcache->add($key,$value,$flag,$expire);
return $ret;
}
/**
* 清洗(删除)已经存储的所有的元素
* @return [type]
*/
public function flush(){
return $this->memcache->flush();
}
/**
* 从服务端删除一个元素
* @param [type] delete 参数:key要删除的元素的key 删除该元素的执行时间 timeout如果值为0,则该元素立即删除。
* @return [type]
*/
public function delete($key){
$ret = $this->memcache->delete($key,0);
return $ret;
}
}
然后调用dlufmemcache:
$memcache = new dlufMemcache('127.0.0.1',11211);
$memcache->set('memcache','come on dluf&baidu !!!!!!');
$ret = $memcache->get('memcache');
echo print_r($ret,true);
运行输出可见:

http://php.net/manual/zh/class.memcache.php
缓存MEMCACHE php调用的更多相关文章
- 缓存MEMCACHE php调用(一)
在项目中,涉及大访问量时,合理的使用缓存能减轻数据库的压力,同时提升用户体验.即在非实时性的需求的前提下,一小段时间内(若干秒),用于显示的数据从缓存中获取的,而不用直接读取数据库,能有效的减少数据库 ...
- 缓存 memcache 小白笔记
W: Memcached是神魔? Q:Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. W:原理图 Q:如下 1浏览器 2 服务器 3 数据库 4 memcac ...
- 使用缓存Memcache存储access_token
接上篇文本,千辛万苦终于拿到了access_token. 正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效.目前,获取access_token ...
- 使用缓存Memcache存储更新微信access token
关键字:Memcache access_token 更新 存储 7200 本文介绍如何使用缓存Memcache存储及更新 access token的方法. 一.Access Token access_ ...
- python运维开发(十一)----python操作缓存memcache、redis
内容目录: 缓存 memcache redis memcache Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数 ...
- 分布式缓存Memcache和Redis
引言 针对于如今计算机的CPU和网络设施,相应用程序来说,运行效率的瓶颈.已经不是代码的长度(实现同一个功能)和带宽了,而是,代码訪问资源的过程.即:让我们的程序慢下来的罪魁祸首就是IO操作. 程序从 ...
- php5.4之分布式缓存memcache(windows7下安装配置)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq1355541448/article/details/36663203 使用理由:就是为了频繁查询 ...
- 分布式缓存 — memcache
MemCache是一个自由.源码开放.高性能.分布式的分布式内存对象缓存系统,用于动态Web应用以减轻数据库的负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高了网站访问的速度.Mem ...
- 【系统架构】缓存Memcache 使用原子性操作add,实现并发锁
原文地址 memcache中Memcache::add()方法在缓存服务器之前不存在key时, 以key作为key存储一个变量var到缓存服务器.我们使用add来向服务器添加一个键值对应,如果成功则添 ...
随机推荐
- HTTPS 使用成本
HTTPS 目前唯一的问题就是它还没有得到大规模应用,受到的关注和研究都比较少.至于使用成本和额外开销,完全不用太过担心. 一般来讲,使用 HTTPS 前大家可能会非常关注如下问题: 证书费用以及更新 ...
- IOS项目之弹出动画终结篇
在之前写过IOS项目之弹出动画一.IOS项目之弹出动画二.IOS项目之弹出动画三,今天来一个终极封装已经上传到Github上弹出动画总结篇UIPopoverTableView. UIPopoverTa ...
- Angular2-编写一个简易的组件
Angular2组件可以这么理解:编写一个类,然后在类的上面用组件装饰器装饰一下,这个类就成组件了. 所以编写组件分两步:1)编写类:2)编写装饰器 1)编写类: export class Simpl ...
- Java - 用静态工厂方法代替构造器
Effective Item - 考虑用静态工厂方法代替构造器我们有两种常见的方法获得一个类的实例: 公有的构造器 提供静态工厂方法(static factory method) 相对公有的构造器,静 ...
- Vue2.0学习笔记:Vue事件修饰符的使用
事件处理 如果需要在内联语句处理器中访问原生DOM事件.可以使用特殊变量$event,把它传入到methods中的方法中. 在Vue中,事件修饰符处理了许多DOM事件的细节,让我们不再需要花大量的时间 ...
- 简单的winform编辑器
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- [javaSE] 集合框架(Map概述)
Map集合,将key对象映射到value对象 三个主要的子类:Hashtable,HashMap,TreeMap Hashtable:底层是哈希表数据结构,不允许使用null值,线程同步 HashMa ...
- MongoTemplate基本操作
本文基于spring boot项目,快速构建项目请参考:https://www.cnblogs.com/lay2017/p/8836273.html 添加mongo依赖 <dependency& ...
- Spring MVC No converter found for return value of type 解决方法
1.在pom中添加 jackson <properties> <jackson.version>2.8.5</jackson.version> </prope ...
- spring AOP为什么配置了没有效果?
spring Aop的配置一定要配置在springmvc配置文件中 springMVC.xml 1 <!-- AOP 注解方式 :定义Aspect --> <!-- ...