W: Memcached是神魔?

Q:Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。

W:原理图

Q:如下

1浏览器    2 服务器   3  数据库    4  memcache缓存

1---》2--》判断缓存是否有数据

---分支1--》无缓存数据----》3---》同时传给服务器(3)  和    memcache (4)一份---》浏览器(1)

--分支2--》有缓存数据 读取memcache(4)--.返回浏览器(1)

W:工作流程是怎样的?

Q:

1:第一次访问

浏览器访问---》后台获取数据  --》同时向浏览器  和  memcached   双方各发一份数据-----》  用户获取数据

2:用户不是第一次访问

浏览器访问---》memcached  获取数据(如果有memcached数据,直接调用。没有的话,去数据库拿) 》  用户获取数据

W:MEMCACHE  安装

Q:

1:使用管理员身份运行cmd 并切换到上一步解压后的目录下, 运行命令 memcached -d install 来把memcache安装为系统服务,

2:运行命令 memcached -d start 来启动服务

3:运行命令 memcached -h 来验证安装是否成功

W: 缓存MEMCACHE php调用

Q:第一步   新建一个文件memcache(配置现骨干信息,生成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;

}

}

2:新加一个常用方法类

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;

}

}

把增删改查方法写进如方便调用。

W:使用缓存   ?

Q:

····1实例化memcache类

·····$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);
 

缓存 memcache 小白笔记的更多相关文章

  1. MVC缓存OutPutCache学习笔记 (二) 缓存及时化VaryByCustom

    <MVC缓存OutPutCache学习笔记 (一) 参数配置> 本篇来介绍如何使用 VaryByCustom参数来实现缓存的及时化.. 根据数据改变来及时使客户端缓存过期并更新.. 首先更 ...

  2. MVC缓存OutPutCache学习笔记 (一) 参数配置

    OutPutCache 参数详解 Duration : 缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的. Location : 缓存放置的位置; 该 ...

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

    内容目录: 缓存 memcache redis memcache Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数 ...

  4. 使用缓存Memcache存储access_token

    接上篇文本,千辛万苦终于拿到了access_token. 正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效.目前,获取access_token ...

  5. 分布式缓存Memcache和Redis

    引言 针对于如今计算机的CPU和网络设施,相应用程序来说,运行效率的瓶颈.已经不是代码的长度(实现同一个功能)和带宽了,而是,代码訪问资源的过程.即:让我们的程序慢下来的罪魁祸首就是IO操作. 程序从 ...

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

    关键字:Memcache access_token 更新 存储 7200 本文介绍如何使用缓存Memcache存储及更新 access token的方法. 一.Access Token access_ ...

  7. 【系统架构】缓存Memcache 使用原子性操作add,实现并发锁

    原文地址 memcache中Memcache::add()方法在缓存服务器之前不存在key时, 以key作为key存储一个变量var到缓存服务器.我们使用add来向服务器添加一个键值对应,如果成功则添 ...

  8. [nosql之缓存memcache]安装篇LInux for Windows

    首先呢在PHP开发的过程中会用到很多缓存服务,从而提升访问质量或者临时存储一些数据. 优点 结构简单,读取速度快,易于维护.还有一些特性memcache redis mongodb都可以用来做为缓存用 ...

  9. Memcache学习笔记

    以下内容大部分来自网络,小部分是本人遇到的问题融合后的记录. 先贴一段涨姿势~ Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全 ...

随机推荐

  1. java字符串类型和时间类型的转换

    类型转换 //reqeust.getParameter获取字符串直接赋值 1 public static Date date(String date_str) { try { Calendar zca ...

  2. HDU 1248 寒冰王座 (完全背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1248 寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. iOS:动画(18-10-15更)

    目录 1.UIView Animation 1-1.UIView Animation(基本使用) 1-2.UIView Animation(转场动画) 2.CATransaction(Layer版的U ...

  4. iOS7下Status Bar字体颜色修改

    原文来自这里:iOS7下Status Bar字体颜色修改. 旧项目在iOS7上遇到status bar字体颜色需要修改的问题,症状如下:导航栏设置为黑色后,iphone上status bar的字体颜色 ...

  5. string::size_type类型

    string::size_type类型 对于string中的size函数,size函数返回的是string对象的字符个数(长度),我们知道,对size()来说,返回一个int或者是一个unsigned ...

  6. chromium之histogram.h

    histogram不知道是干啥的 // Histogram is an object that aggregates statistics, and can summarize them in // ...

  7. Wireshark工具抓包的数据包分析

    Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包资料. Wireshark使用WinPCAP作为接口,直接与网卡 ...

  8. ubuntu14.04安装qt-4.8.4

    题记:因为工作中用到qt的qmake工具生成x项目的Makefile文件,因为原有工程用的是4.8.4版本的,因此在此基础之上安装此版本. 用安装包工具进行安装qt不能直接安装到4.8.4版本的,因此 ...

  9. diff命令--比较两个文件的命令

    可以使用 --brief 来比较两个文件是否相同,使用 -c参数来比较这两个文件的详细不同之处,这绝对是判断文件是否被篡改的有力神器,

  10. wamp 的配置

    一 . 安装  二 . 配置 安装过后打开E:\wamp2\wamp\bin\apache\Apache2.2.21\conf\httpd.conf 寻找Directoy 为文件路径 里面的默认文件删 ...