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. CodeForces - 607B (记忆化搜索)

    传送门: http://codeforces.com/problemset/problem/607/B Genos recently installed the game Zuma on his ph ...

  2. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  3. 关于SQL优化这些你了解吗?

    目录树 背景 优化点 前提必备知识 优化之一 - 从数据库设计方面考虑 优化之二 - 从SQL语句优化方面考虑 优化之三 - 读写分离与分库分表 背景 在当今这个互联网的时代无非要解决两大难题,其一是 ...

  4. 一点一点看JDK源码(五)java.util.ArrayList 后篇之forEach

    一点一点看JDK源码(五)java.util.ArrayList 后篇之forEach liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点看JDK源码(〇) 代 ...

  5. Oracle语句(一)之简单查询

    1.查询数据表的所有列: select * from 表名; 程序员正常用法:select 列名,列名... form 表名; 2.起别名: select 列名 [AS 别名],列名 别名...fro ...

  6. chromium之message_pump_win之三

    上一篇分析MessagePumpForUI,参考chromium之message_pump_win之二 MessagePumpForIO,同MessagePumpForUI,也是要实现三个函数 // ...

  7. springboot-redis缓存

    Redis缓存使用 1.  引入依赖(可能已经引入了):spring-boot-starter-cache 2.  在application.yml配置文件中配置spring:redis:host/p ...

  8. Redis支持的五种数据类型

    redis支持的五种数据类型: 1.string(字符串) 2.hash(哈希) Redis hash 是一个键值(key=>value)对集合. Redis hash是一个string类型的f ...

  9. 【Linux】Linux 的慢动作基础

    了解一下刀片服务器: 刀片服务器是指在高标准度的机架式机箱内插装多个卡式的服务器单元,是一种实现HAHD的低成本服务器平台,其中每一片刀片实际上就是一块系统主板. Linux: Linux操作系统构成 ...

  10. node的安装和配置

    一 . 直接安装node 1. http://nodejs.cn/download/ 根据自己的电脑选择适合的安装包 2.安装 , 无脑下一步 , 可以选择安装路径 , 但是一定要记住 . 3.命令行 ...