Home >PHP Framework >ThinkPHP >Which one is faster, thinkphp cache or redis cache?

Which one is faster, thinkphp cache or redis cache?

(*-*)浩
(*-*)浩Original
2019-10-30 13:35:003910browse

Which one is faster, thinkphp cache or redis cache?

The cache storage method in tp5, my local setting is to read text.

Take this code as an example: (Recommended learning: thinkphp5)

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

There are two loops here, if no caching is used It basically takes 400–600ms to process the information.

After adding the cache that comes with tp, the time spent is significantly shortened, between 40-60ms, which is ideal.

After writing this, I wonder if redis will be around 10ms, which will be more powerful than the built-in cache.

So I added redis cache

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

On the surface it is basically the same as tp’s cache method, but in fact there is concurrency, and the traffic has not been tested

The conclusion is:

If the amount of storage is not large, TP's file cache is similar to redis. But memory reading must be faster. If there are many values ​​stored, redis's powerful I/O capabilities will be stronger than ordinary file reading and writing.

The above is the detailed content of Which one is faster, thinkphp cache or redis cache?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn