0
    public function latest($count)
        {
            $key = 'latest.' . $count;
            $cacheKey = $this->getCacheKey($key);

            $articles_latest = Redis::get($cacheKey);
            if($articles_latest)
                return $articles_latest;
            $articles_latest = Articles::orderBy('id', 'desc)->take($count)->get();           
            Redis::put($cacheKey, $articles_latest, 1);
            return $articles_latest;
        }

.env:

CACHE_DRIVER=redis

执行上述代码会报错:Predis\ClientException: Command 'PUT' is not a registered Redis command。

4

1 回答 1

0

您可以通过Cache 门面访问配置的 redis 缓存,也可以使用 remember 功能:

    public function latest($count)
        {
            $key = 'latest.' . $count;
            $cacheKey = $this->getCacheKey($key);

            return Cache::remember($cacheKey, 60, function() {
                return Articles::orderBy('id', 'desc)->take($count)->get();
            });
        }

我假设 1 是一分钟,请注意,在 Laravel 5.8 中,缓存参数列表已更改为使用秒而不是分钟。您还应该查看有关代码格式的PSR编码标准。

于 2019-04-29T14:29:01.573 回答