PHP缓存之内存缓存(Memcache)

jopen 9年前

  1. PHP内存缓存应该也是数据缓存的一种,是将数据库查询出来的数据缓存在内存中,共享内存块中。这种方式可以通过扩展软件来实现,现在主流的主要是Memcache。  

1、PHP开启Memcache模块(windows)

         PHP开启memcache, 只需要将PHP的配置文件php.ini中

extension=php_memcache.dll  

前的#号去掉即可。在php.ini中还有几个memcache的配置参数:

memcache.allow_failover  
类型:Boolean
说明:在错误时是否透明的故障转移到其他服务器上处理

memcache.max_failover_attempts 
类型:integer
说明:定义服务器的数量类设置和获取数据,只联合 memcache.allow_failover 一同使用。

memcache.chunk_size
类型:integer
说明:数据将会被分成指定大小(chunk_size)的块来传输,这个值(chunk_size)越小,写操作的请求就越多,如果发现其他的无法解释的减速,请试着将这个值增大到32768.

memcache.hash_strategy 
类型:string
说明:控制在映射 key 到服务器时使用哪种策略。设置这个值一致能使hash 算法始终如一的使用于服务器接受添加或者删除池中变量时将不会被重新映射。设置这个值以标准的结果在旧的策略被使用时。

memcache.hash_function 
类型:string
说明:控制哪种 hsah 函数被应用于 key映射 到服务器过程中,默认值“crc32”使用 CRC32 算法,而“fnv”则表示使用 FNV-1a 算法。

session.save_handler 
类型:string
说明:通过设置这个值为memcache来确定使用 memcache 用于通信对话的处理(session handler)。

session.save_path 
类型:string
说明:定义用于通话存储的各服务器链接的分隔符号,例如:“tcp://host1:11211, tcp://host2:11211”。

 

       案例:

    memcache.allow_failover = On                       memcache.max_failover_attempts = 20        memcache.chunk_size = 8192        memcache.default_port = 11211        ;memcache.hash_strategy = "standard"        ;memcache.hash_function = "crc32"        ;session.save_handler = "files"        ;session.save_path = ""  
</div> </div>
2、memcache和memcached之间的区别

         对于内存缓存,比较常用的有两种memcache和memcached扩展。而memcached和memcache的守护进程memcached同名,比较容易引起混淆,甚至提到memcached,有些人第一想到的是后台的守护进程,这里还是有必要分析一下两者之间的区别。
         memcache是完全在PHP框架内开发的,memecached是使用libmemcached的。从手册上看,memcached 会比 memcache 多几个方法,使用方式上都差不多。
具体可以参考PHP官网:

Memcached:http://cn2.php.net/manual/zh/book.memcached.php
Memcache:http://cn2.php.net/manual/zh/book.memcache.php

3、PHP操作memcache的简单案例

class MemcacheModel {             private $memcache;             //连接memcache        public function __construct($host = '127.0.0.1', $port = 11211) {            $this->memcache = new Memcache();            $this->memcache->connect($host, $port);            return $this->memcache;        }             /**        * add  添加一个新key,但是如果 key已经在服务端存在,此操作会失败。        * @param string $Key    KEY名称        * @param string $value  值----可以是数组,对象,单值        * @param int $timelift 生存时间   add生存时间默认为0表示数据用不过期        */        public function add($key, $value, $timeLife) {            if ($time > 0) {                $retMes = $this->memcache->add($key, $value, MEMCACHE_COMPRESSED, $timeLife);            } else {                $retMes = $this->memcache->add($key, $value);            }            return $retMes;        }             /**        * set設置一致key  修改键名的值        * @param string $key 键名        * @param string $value 键值        * @param int $timeLife 生命周期        */        public function set($key, $value, $timeLife) {            if ($timeLife > 0) {                $retMes = $this->memcache->set($key, $value, MEMCACHE_COMPRESSED, $timeLife);            } else {                $retMes = $this->memcache->set($key, $value);            }            return $retMes;        }             /**        * 获取key        * @param string  $key 键名        */        public function get($key) {            $retMes = $this->memcache->get($key);            return $retMes;        }             /**        * 删除单个key        * @param string $key 键名        */        public function deleteKey($key) {            $retMes = $this->memcache->delete($key);            return $retMes;        }             /**        * 删除所有key        */        public function deleteAll() {            return $this->memcache->flush();        }                 /**        * 释放        */        public function __destruct() {            $this->memcache->close();        }         }  
</div> </div>