使用 Redis 作为缓存驱动时,Redis 和 Cache 的区别?

设置 Redis 为缓存驱动后,Cache::get('xx')Redis::get('xx') 有何区别?
Cache::put('test', '1', 10);Redis::get('test') 值不是 1,Cache 和 Redis 是相互独立的吗?

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

@wanghan 贴一段代码就知道了,以后这种问题可以自己翻一下源码的,这样会让你印象更加深刻。:smile:
1、redis作为缓存驱动,Cache::get('xx')Redis::get('xx')没任何区别,你取不到是因为你少了前缀。
2、为什么这么做的原因,可以看下设计模式,继承了同一接口Illuminate\Contracts\Cache\Store的缓存驱动,可以让你通过环境变量快速无缝切换缓存(比如我切换到文件缓存驱动或者memcache缓存驱动),无需更改任何业务代码.
vendor/laravel/framework/src/Illuminate/Cache/RedisStore.php

/**
     * Store an item in the cache for a given number of minutes.
     *
     * @param  string  $key
     * @param  mixed   $value
     * @param  float|int  $minutes
     * @return void
     */
    public function put($key, $value, $minutes)
    {
        $this->connection()->setex(
            $this->prefix.$key, (int) max(1, $minutes * 60), $this->serialize($value)
        );
    }

vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php

/**
     * Dynamically call the default driver instance.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->store()->$method(...$parameters);
    }
5年前 评论
讨论数量: 8

@wanghan 贴一段代码就知道了,以后这种问题可以自己翻一下源码的,这样会让你印象更加深刻。:smile:
1、redis作为缓存驱动,Cache::get('xx')Redis::get('xx')没任何区别,你取不到是因为你少了前缀。
2、为什么这么做的原因,可以看下设计模式,继承了同一接口Illuminate\Contracts\Cache\Store的缓存驱动,可以让你通过环境变量快速无缝切换缓存(比如我切换到文件缓存驱动或者memcache缓存驱动),无需更改任何业务代码.
vendor/laravel/framework/src/Illuminate/Cache/RedisStore.php

/**
     * Store an item in the cache for a given number of minutes.
     *
     * @param  string  $key
     * @param  mixed   $value
     * @param  float|int  $minutes
     * @return void
     */
    public function put($key, $value, $minutes)
    {
        $this->connection()->setex(
            $this->prefix.$key, (int) max(1, $minutes * 60), $this->serialize($value)
        );
    }

vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php

/**
     * Dynamically call the default driver instance.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->store()->$method(...$parameters);
    }
5年前 评论
wanghan

坐等大佬

5年前 评论

@wanghan 贴一段代码就知道了,以后这种问题可以自己翻一下源码的,这样会让你印象更加深刻。:smile:
1、redis作为缓存驱动,Cache::get('xx')Redis::get('xx')没任何区别,你取不到是因为你少了前缀。
2、为什么这么做的原因,可以看下设计模式,继承了同一接口Illuminate\Contracts\Cache\Store的缓存驱动,可以让你通过环境变量快速无缝切换缓存(比如我切换到文件缓存驱动或者memcache缓存驱动),无需更改任何业务代码.
vendor/laravel/framework/src/Illuminate/Cache/RedisStore.php

/**
     * Store an item in the cache for a given number of minutes.
     *
     * @param  string  $key
     * @param  mixed   $value
     * @param  float|int  $minutes
     * @return void
     */
    public function put($key, $value, $minutes)
    {
        $this->connection()->setex(
            $this->prefix.$key, (int) max(1, $minutes * 60), $this->serialize($value)
        );
    }

vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php

/**
     * Dynamically call the default driver instance.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->store()->$method(...$parameters);
    }
5年前 评论
wanghan

@jiyis 大佬。。。接我一个拥抱

5年前 评论
Shuyi

Cache属于Facade,所以用的key不一样,而且Cache可能跑去Call Memcached也不一定; Redis则是往Redis放data

5年前 评论

@danielwng 加前缀就是规定从哪个存储里取,不加就是从默认的file里取,即

Cache::get('xx') 等价于 Cache::store('file')->get('xx');

redis 作为缓存驱动,即

Redis::get('xx') 等价于 Cache::store('redis')->get('xx');
3年前 评论
aa24615

这是缓存驱动,不是缓存前缀,你的理解错了

3年前 评论

redis默认用的库是0 redis的缓存默认用的库是1

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!