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

本篇为基础篇

前言

  • 相信很多人都玩过新浪,新浪的首页那么多模块,那么多文章,而且每天那么多的访问量, 如果每次有人去访问就去查数据库,而且一个人的操作可能需要很多次数据库操作,这样如果都是走数据库,可想而知页面会不会很卡,数据库压力会很大,而且很可能造成数据库崩溃。

  • 但是为什么新浪为什么能很快的响应页面?其中一部分功劳就是靠的Reids的缓存技术。我去查询内存里的东西是不是会比数据库快很多?这样我缓存中有我要的东西,我就去去缓存,否则我去找数据库,找到再放到缓存,方便以后查找。

  • 相比较Memcached笔者还是更喜欢Redis一点,因为redis可以将缓存中的内容写入文件,再服务器断电时,我也可以保存记录,最大的原因还是因为redis的数据类型比memcache多,方便讲无关系性的数据库转为有关系型数据库一样的使用。

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 即可。

  • 基本使用方法

    1. 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');
           }
       }
      
       页面响应 :![](http://upload-images.jianshu.io/upload_images/2853374-9eade34be464d20b.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 = 'STRING:TEST';
                  // 读取一个字符串类型的redis
                  $info = \Redis::get($key);
                  dd($info);
                  return view('test');
              }
          }
        

      页面响应 “hello word”

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

    1. LIST类型

      • 写入队列

          class PhotoController extends Controller  
          {
              /**
               * Display a listing of the resource.
               *
               * @return \Illuminate\Http\Response
               */
              public function index()
              {
                  //
                  $key = 'LIST:TEST:R';
                  $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
                  // 从右往左压入队列
                  $info = \Redis::rpush($key,$names);
                  dd($info);
                  return view('test');
              }
          }
        

        页面响应 (写入的数量) 8

      • 写入队列

          class PhotoController extends Controller  
          {
              /**
               * Display a listing of the resource.
               *
               * @return \Illuminate\Http\Response
               */
              public function index()
              {
                  //
                  $key = 'LIST:TEST:R';
                  $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
                  // 获取队列内容(0到-1 所有 0到0是一位 0到1是两位)
                  $info = \Redis::lrange($key,0,-1);
                  dd($info);
                  return view('test');
              }
          }
        

        页面响应 (数组) 《redis缓存隔离三部曲_redis基础知识(一)》

      • 从左往右塞入队列 连贯方法

          class PhotoController extends Controller  
          {
              /**
               * Display a listing of the resource.
               *
               * @return \Illuminate\Http\Response
               */
              public function index()
              {
                  //
                  $key = 'LIST:TEST:L';
                  $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
                  // 从左往右存数据
                  \Redis::lpush($key,$names);
                  // 取出数据
                  $info = \Redis::lrange($key,0,-1);
                  dd($info);
                  return view('test');
              }
          }
        

        页面响应 (数组 是不是正好和上面的相反?) 《redis缓存隔离三部曲_redis基础知识(一)》

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

        页面响应 《redis缓存隔离三部曲_redis基础知识(一)》

      • 取数据(取所有)

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

        页面响应 《redis缓存隔离三部曲_redis基础知识(一)》

      • 取数据(取个别字段)

          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缓存隔离三部曲_redis基础知识(一)》

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');
                }
            }
        页面响应 ![](http://upload-images.jianshu.io/upload_images/2853374-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');
                }
            }

        页面响应 ![](http://upload-images.jianshu.io/upload_images/2853374-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的值为基准。。

        页面响应 ![](http://upload-images.jianshu.io/upload_images/2853374-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');
                }
            }

        页面响应 ![](http://upload-images.jianshu.io/upload_images/2853374-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

    原文作者:GQ1994
    原文地址: https://www.jianshu.com/p/a0cddc934ee9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞