thinkphp5 中的cache 缓存和redis 缓存哪个比较快?

tp5中的cache 储存方式,我本地设置的是读取文本的形式。

以这段代码为例:

    public function getAllManegerId(){
        $cache =checkCache('kf_getallManeger');
        if($cache)return$cache;
        $role = Db::table('customer_role')->where(['type'=>2, 'role_status'=>0,])->select();
        $array = [];
        if(!empty($role)){
            foreach ($role as $key=>$value){
                $customer = Db::table('customer')->where(['role_id'=>$value['role_id'],'user_status'=>0])->select();
                foreach ($customer as $keys=>$values){
                    array_push($array,$values['customer_id']);
                }
            }
        }
        \cache('kf_getallManeger',$array,300);
        return $array;
    }

这里有两个循环,如果不用缓存基本上要花费 400–600ms 的时间处理完信息。

这里写图片描述

加入tp自带的cache 之后 所花费的时间明显的缩短了,在40-60ms 之间,比较理想。

写到这里之后, 我试想,redis 会不会在10ms 左右,会比自带的cache强大。

于是加入了redis 缓存

        $redis = $this->redis = new \Redis;
        $redis->connect('127.0.0.1', 6379);
        $caches = $redis->get('kf_getallManeger');
        if($caches)return $caches;

测试一下
这里写图片描述

基本上和tp 的cache 方法不分秋色

得到的结论是:

如果储存的量不大,tp 的file 缓存,和redis 差不多。如果存储的值多,redis 强大的i/o能力会强于 普通的文件读写。

猜你喜欢

转载自blog.csdn.net/qq_22823581/article/details/80251374