缓存 memcache 小白笔记

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

     

猜你喜欢

转载自www.cnblogs.com/zzqqyy/p/9294349.html