首页 > 代码库 > redis 简单应用

redis 简单应用

Incr

$views = Redis::incr(‘views‘);
$article_views = Redis::incr(‘article:‘ . $article_id . ‘:views‘);

zIncrBy  有序集合 增加

    public function set($id)
    {
        Redis::zIncrBy(‘articleViews‘, 1, ‘article:‘.$id);
    }

    public function get()
    {
        $hots = Redis::zRevRange(‘articleViews‘, 0, -1);
        foreach($hots as $index => $hot){
            $id    = str_replace(‘article:‘, ‘‘, $hot);
            $views = Redis::zScore(‘articleViews‘, $hot);
            echo $index+1 . ‘)‘. ‘the article id is ‘.$id.‘, it has ‘.$views.‘ views<br>‘;
        }
    }

Cache 缓存

    public function show()
    {
        return Cache::remember(‘post_cache‘, 1, function () {
            return Post::first();
        });
    }

 

redis 简单应用