是否想过php使用redis作为缓存时,如何能:

  1. 前后台模块共用model层;
  2. 但是,不能每个model类都进行缓存,这样太浪费redis资源;
  3. 前后台模块可以自由决定从数据库还是从缓存读数据;
  4. 没有冗余代码;
  5. 使用方便。
    这里我们先展示实现的最终效果。

最终的代码和使用说明请移步github:https://github.com/yeszao/php-redis-cache

马上安装使用命令:

$ composer install yeszao/cache

 

经过简单配置就可以使用,请参看github的readme说明。

1 最终效果

假设在mvc框架中,model层有一个book类和一个getbyid方法,如下:

class book
{
    public function getbyid($id)
    {
        return $id;
    }
}

 

加入缓存技术之后,原来方法的调用方式和返回的数据结构都不应该改变。

所以,我们希望,最后的效果应该是这样的:

1 (new book)->getbyid(100);           // 原始的、不用缓存的调用方式,还是原来的方式,一般是读取数据库的数据。
2 (new book)->getbyidcache(100);      // 使用缓存的调用方式,缓存键名为:app_models_book:getbyid: + md5(参数列表)
3 (new book)->getbyidclear(100);      // 删除这个缓存
4 (new book)->getbyidflush();         // 删除 getbyid() 方法对应的所有缓存,即删除 app_models_book:getbyid:*。这个方法不需要参数。

 

这样我们可以很清楚的明白自己在做什么,同时又知道数据的来源函数,并且被引用方式完全统一,可谓一箭三雕。

其实实现起来也比较简单,就是使用php的魔术方法__call()方法。

2 __call()方法

这里简单说明一下__call方法的作用。

在php中,当我们访问一个不存在的类方法时,就会调用这个类的__call()方法。

(如果类方法不存在,又没有写__call()方法,php会直接报错)

假设我们有一个book类:

 1 class book
 2 {
 3     public function __call($name, $arguments)
 4     {
 5         echo '类book不存在方法', $name, php_eol;
 6     }
 7 
 8     public function getbyid($id)
 9     {
10         echo '我的id是', $id, php_eol;
11     }
12 }

 

当调用存在的getbyid(50)方法时,程序打印:我的id是50。

而如果调用不存在的getage()方法时,程序就会执行到a类的__call()方法里面,这里会打印:类book不存在方法getage。

这就是__call的原理。

3 实现细节

接下来我们就利用__call()方法的这种特性,来实现缓存策略。

从上面的例子,我们看到,__call()方法被调用时,会传入两个参数。

name:想要调用的方法名arguments:参数列表
我们就可以在参数上面做文章。

还是以book类为例,我们假设其原本结构如下:

 1 class book
 2 {
 3     public function __call($name, $arguments)
 4     {
 5         // 待填充内容
 6     }
 7 
 8     public function getbyid($id)
 9     {
10         return ['id' => $id, 'title' => 'php缓存技术' . $id];
11     }
12 }

 

开始之前,我们还确认redis的连接,这是缓存必须用到的,这里我们写个简单的单例类:

 1 class common
 2 {
 3     private static $redis = null;
 4 
 5     public static function redis()
 6     {
 7         if (self::$redis === null) {
 8             self::$redis = new \redis('127.0.0.1');
 9             self::$redis->connect('redis');
10         }
11         return self::$redis;
12 }

 

然后,我们开始填充__call()方法代码,具体说明请看注释:

 1 class book
 2 {
 3     public function __call($name, $arguments)
 4     {
 5         // 因为我们主要是根据方法名的后缀决定具体操作,
 6         // 所以如果传入的 $name 长度小于5,可以直接报错
 7         if (strlen($name) < 5) {
 8             exit('method does not exist.');
 9         }
10 
11         // 接着,我们截取 $name,获取原方法和要执行的动作,
12         // 是cache、clear还是flush,这里我们取了个巧,动作
13         // 的名称都是5个字符,这样截取就非常高效。
14         $method = substr($name, 0, -5);
15         $action = substr($name, -5);
16 
17         // 当前调用的类名称,包括命名空间的名称
18         $class = get_class();
19 
20         // 生成缓存键名,$arguments稍后再加上
21         $key = sprintf('%s:%s:', str_replace('\\', '_', $class), $method);
22         // 都用小写好看点
23         $key = strtolower($key);
24 
25         switch ($action) {
26             case 'cache':
27                 // 缓存键名加上$arguments
28                 $key = $key . md5(json_encode($arguments));
29 
30                 // 从redis中读取数据
31                 $data = common::redis()->get($key);
32 
33                 // 如果redis中有数据
34                 if ($data !== false) {
35                     $decodedata = json_decode($data, json_unescaped_unicode);
36                     // 如果不是json格式的数据,直接返回,否则返回json解析后的数据
37                     return $decodedata === null ? $data : $decodedata;
38                 }
39 
40                 // 如果redis中没有数据则继续往下执行
41 
42                 // 如果原方法不存在
43                 if (method_exists($this, $method) === false) {
44                     exit('method does not exist.');
45                 }
46 
47                 // 调用原方法获取数据
48                 $data = call_user_func_array([$this, $method], $arguments);
49 
50                 // 保存数据到redis中以便下次使用
51                 common::redis()->set($key, json_encode($data), 3600);
52 
53                 // 结束执行并返回数据
54                 return $data;
55                 break;
56 
57             case 'clear':
58                 // 缓存键名加上$arguments
59                 $key = $key . md5(json_encode($arguments));
60                 return common::redis()->del($key);
61                 break;
62 
63             case 'flush':
64                 $key = $key . '*';
65 
66                 // 获取所有符合 $class:$method:* 规则的缓存键名 
67                 $keys = common::redis()->keys($key);
68                 return common::redis()->del($keys);
69                 break;
70 
71             default:
72                 exit('method does not exist.');
73         }
74     }
75 
76     // 其他方法
77 }

 

这样就实现了我们开始时的效果。

4 实际使用时

在实际使用中,我们需要做一些改变,把这一段代码归入一个类中,

然后在model层的基类中引用这个类,再传入redis句柄、类对象、方法名和参数,

这样可以降低代码的耦合,使用起来也更灵活。

完整的代码已经放在github上,请参考文章开头的参考地址。

 

推荐阅读:

php操作redis数据库常用方法

redis的面试问题总结,面试跳槽必备

用php+redis实现延迟任务,实现自动取消订单

php基于redis实现轻量级延迟队列

php 面试官问:你说说redis的几个过期策略?