GQ猿旅程我爱编程

redis缓存隔离三部曲_redis基础知识(一)

2017-01-19  本文已影响168人  GQ1994

本篇为基础篇

前言

redis的特点

  1. Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储。

  2. Redis支持数据的备份,即master-slave模式的数据备份。

  3. Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。

Laravel中 使用的Redis

Redis 是一款开源且先进的键值对数据库。由于它可用的键包含了字符串、哈希、列表、集合 和 有序集合,因此常被称作数据结构服务器。在使用 Redis 之前,你必须通过 Composer 安装 predis/predis 扩展包(~1.0)。

1.安装predis组件

composer require "predis/predis:~1.0"

2.配置

应用程序的 Redis 设置都在config/database.php配置文件中。在这个文件里,你可以看到 redis 数组里面包含了应用程序使用的 Redis 服务器:

'redis' => [

    'cluster' => false,

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

],

默认的服务器配置对于开发来说应该足够了。然而,你也可以根据使用的环境来随意更改数组。只需给每个 Redis 指定名称以及在服务器中使用的 host 和 port 即可。

4. 集合

    - 求两个集合的交集

            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');
                }
            }
        页面响应 ![](https://img.haomeiwen.com/i2853374/01b65a52115f83f8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

    - 求两个集合的并集

            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');
                }
            }

        页面响应 ![](https://img.haomeiwen.com/i2853374/174fbadfcbb1af7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

    - 求两个集合的差集

            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的值为基准。。

        页面响应 ![](https://img.haomeiwen.com/i2853374/7489c7c98031d665.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

5. 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');
                }
            }

        页面响应 ![](https://img.haomeiwen.com/i2853374/c85c985cdca52b22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


6. 判断这个redis key是否存在
    
        \Redis::exists($key);

当然了,这里只是一些最基本的Redis缓存demo,其实他的强大远远不止这些,操作也不止这些,比如队列里的弹出,比如集合与集合之间的复杂关系运用...如何将非关系型的Redis运用成关系型数据库那样??Redis的一些实用场景又是那一些??敬请查看下一篇--"Redis 三部曲之第二部 laravel中Redis 基本的数据隔离"。

再次特别感谢倡哥的博客和倡哥的指导
本文为作者参考倡哥博格加实战所总结而来,允许转载,转载后请以链接形式说明文章出处.
倡哥,博客地址:http://blog8090.com

上一篇 下一篇

猜你喜欢

热点阅读