对Laravel - Redis 缓存的理解

2018-12-02  本文已影响0人  wandx

最近在看redis的一些用法,下面就来说说我的使用历程!redis在很多项目中使用频率很高!而且技术的提升redis也是必会使用的!多做总结,慢慢的吃成技术的‘胖纸’!

首先Redis的使用场景想必大家多多少少都了解一些了。比如新浪的首页那么多模块,那么多文章,如果读数据库是不是压力特别大,反应是不是特别慢?但是为什么新浪为什么能很快的响应页面?其中一部分功劳就是靠的Reids的缓存技术。
1.Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储。
2.Redis支持数据的备份,即master-slave模式的数据备份。
3.Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。
Laravel中 使用的Redis
在使用 Redis 之前,你必须通过 Composer 安装 predis/predis 扩展包
composer require "predis/predis" (直接安装最新的即可)
然后配置 应用程序的 Redis 设置都在 config/database.php 配置文件中。在这个文件里,你可以看到 redis 数组里面包含了应用程序使用的 Redis 服务器:

'redis' => [
    'cluster' => false,
    'default' => [
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ],
],

STRING类型 - 写入字符串类型的redis

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'STRING:TEST';
        $value = 'Hello-World';
        // 写入一个字符串类型的redis
        $info = \Redis::Set($key,$value);
        dd($info);
        return view('test');
    }
}

读取相应的字符串

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'STRING:TEST';
        // 读取一个字符串类型的redis
        $info = \Redis::get($key);
        dd($info);
        return view('test');
    }
}

和redis语法同样的 字串也有incr和decr等递增、递减...

HASH类型

存数据

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'HASH:TEST';
        $names = ['id'=>'99',
                  'name'=>'AXiBa',
                  'age'=>'23',
                  'tel'=>'13995578699',
                  'addtime'=>'1231231233'];
        // 将数据写入hash
        $info = \Redis::hMset($key,$names);
        dd($info);
        return view('test');
    }
}

取数据(取所有)

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'HASH:TEST';
        $names = ['id'=>'99',
                  'name'=>'AXiBa',
                  'age'=>'23',
                  'tel'=>'13995578699',
                  'addtime'=>'1231231233'];
        // 取出hash里的数据
        $info = \Redis::hGetall($key);
        dd($info);
        return view('test');
    }
}

取数据(取个别字段)

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'HASH:TEST';
        $names = ['id'=>'99',
                  'name'=>'AXiBa',
                  'age'=>'23',
                  'tel'=>'13995578699',
                  'addtime'=>'1231231233'];
        // 取出hash里的 某一个字段的数据
        $info = \Redis::hGet($key,'name');
        dd($info);
        return view('test');
    }
}
// 判断这个redis key是否存在
\Redis::exists($key);

SET类型

写入一个无序集合(数据插入无顺序)

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'SET:TEST';
        $value = ['a','b','c','d','e'];
        $info = \Redis::sadd($key,$value);
         $info = \Redis::smembers($key);
        dd($info);
        return view('test');
    }
}

求两个集合的交集

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'SET:TEST';
        $key1 = 'SET:TEST:1';
        $value = ['a','b','c','d','e'];
        $value1 = ['a','b','c','1','2'];
        // 写入另一个集合
        \Redis::sadd($key1,$value1);
        // 交集
        $info = \Redis::sinter($key,$key1);
        dd($info);
        return view('test');
    }
}

求两个集合的并集

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'SET:TEST';
        $key1 = 'SET:TEST:1';
        $value = ['a','b','c','d','e'];
        $value1 = ['a','b','c','1','2'];
        // 并集
        $info = \Redis::sunion($key,$key1);
        dd($info);
        return view('test');
    }
}

求两个集合的差集

class PhotoController extends Controller  
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $key = 'SET:TEST';
        $key1 = 'SET:TEST:1';
        $value = ['a','b','c','d','e'];
        $value1 = ['a','b','c','1','2'];
        // 差集
        $info = \Redis::sdiff($key,$key1);
        dd($info);
        return view('test');
    }
}

哪个key在前,就以哪个key的值为基准。。


参考 redis中文官网 http://www.redis.cn/

上一篇下一篇

猜你喜欢

热点阅读