redis缓存商品列表

        $redis = new Redis();
        $redis ->connect('127.0.0.1',6379);
        $data = $redis->get('goods_cache');
        if($data){
            echo 'get data from redis<br>';
            $data = json_decode($data,true);
        }else{
            echo 'get data from mysql<br>';
            $data = pdo_fetchall("select id, title from ".tablename('ewei_shop_goods')." where status=1 limit 6");
            $redis->set('goods_cache',json_encode($data));
            $redis->expire('goods_cache',5);//设置缓存时长5秒
        }

        var_dump($data);

 

posted @ 2020-12-09 10:42  大熊童鞋  阅读(1428)  评论(0编辑  收藏  举报