|
在项目中,涉及大访问量时,合理的使用缓存能减轻数据库的压力,同时提升用户体验。即在非实时性的需求的前提下,一小段时间内(若干秒),用于显示的数据从缓存中获取的,而不用直接读取数据库,能有效的减少数据库的读取压力。这里记录一下php语言使用memcache的情形:
首先,我们建立一个memcachepool,可以根据不同的配置读取,生成不同的memcache实例。用到$memcache->addServer($host,$port,$flag);向连接池中添加一个memcache服务器。代码示例如下
1 class memcachePool{
2 private static $instance;
3 private $memcacheList = array();
4 private function __construct(){
5
6 }
7 public static function getInstance(){
8 if(self::$instance != null)
9 return self::$instance;
10 self::$instance = new memcachePool();
11 return self::$instance;
12 }
13 /**
14 * get memcache object from pool
15 * @param [type] $host 服务器
16 * @param [type] $port 端口
17 * @param [type] $flag 控制是否使用持久化连接。默认TRUE
18 * @return [type]
19 */
20 public function getMemcache($host,$port,$flag){
21 if(isset($this->memcacheList[$host.$port]))
22 return $this->memcacheList[$host.$port];
23
24 $memcache = new Memcache();
25 // 向连接池中添加一个memcache服务器
26 $memcache->addServer($host,$port,$flag);
27 //开启大值自动压缩,第一个参数表示处理数据大小的临界点,第二个参数表示压缩的比例,默认为0.2
28 $memcache->setCompressThreshold(2000,0.2);
29 $this->memcacheList[$host.$port] = $memcache;
30 return $memcache;
31 }
32 }
View Code 接着实现一个包含memcache常用方法如add,set,get,flush,delete等的方法类,这里命名为dlufmemcache
1 class dlufMemcache{
2 private $memcache = null;
3 function __construct($host,$port){
4
5 $this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true);
6 }
7 /**
8 * memcache set value
9 * @param [type] $key 键
10 * @param [type] $value 值
11 * @param integer $expire 到期的时间,如果此值设置为0表明此数据永不过期
12 * @param integer $flag 标志位 使用MEMCACHE_COMPRESSED指定对值进行压缩(使用zlib)
13 * @param [type] $serializetype
14 */
15 public function set($key,$value,$expire=0,$flag=0,$serializetype=null){
16 if($serializetype == 'json' && is_array($value)){
17 $value = json_encode($value);
18 }
19 $this->memcache->set($key,$value,$flag,$expire);
20 }
21 /**
22 * 从服务端查找元素
23 * @param [type] $key
24 * @return [type]
25 */
26 public function get($key){
27 return $this->memcache->get($key);
28 }
29 /**
30 * 增加一个条目到缓存服务器
31 * @param [type] $key
32 * @param [type] $value
33 * @param integer $expire
34 * @param integer $flag
35 * @param [type] $serializetype
36 */
37 public function add($key,$value,$expire=0,$flag=0,$serializetype=null){
38 if($serializetype == 'json' && is_array($value)){
39 $value = json_encode($value);
40 }
41 $ret = $this->memcache->add($key,$value,$flag,$expire);
42 return $ret;
43 }
44 /**
45 * 清洗(删除)已经存储的所有的元素
46 * @return [type]
47 */
48 public function flush(){
49 return $this->memcache->flush();
50 }
51 /**
52 * 从服务端删除一个元素
53 * @param [type] delete 参数:key要删除的元素的key 删除该元素的执行时间 timeout如果值为0,则该元素立即删除。
54 * @return [type]
55 */
56 public function delete($key){
57 $ret = $this->memcache->delete($key,0);
58 return $ret;
59 }
60 }
View Code 然后调用dlufmemcache:
1 $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);
运行输出可见:
http://php.net/manual/zh/class.memcache.php |
|
|