当前位置:   article > 正文

Laravel - Redis 缓存三部曲 (一) 初识Predis

laravel redis predis

本篇为基础篇

Redis的使用场景想必大家多多少少都了解一些了。比如新浪的首页那么多模块,那么多文章,如果读数据库是不是压力特别大,反应是不是特别慢?但是为什么新浪为什么能很快的响应页面?其中一部分功劳就是靠的Reids的缓存技术。相比较Memcached笔者还是更喜欢Redis一点。

下面简单的分析一下,欢迎拍砖!

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

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

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

Laravel中 使用的Redis

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

安装predis组件

composer require "predis/predis:~1.0"  

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

  1. 'redis' => [
  2. 'cluster' => false,
  3. 'default' => [
  4. 'host' => '127.0.0.1',
  5. 'port' => 6379,
  6. 'database' => 0,
  7. ],
  8. ],

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

基本使用方法

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

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'STRING:TEST';
  12. $value = 'Hello-World';
  13. // 写入一个字符串类型的redis
  14. $info = \Redis::Set($key,$value);
  15. dd($info);
  16. return view('test');
  17. }
  18. }
  • 页面响应 ![](/content/images/2017/01/1.png)

  • 读取相应的字符串

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'STRING:TEST';
  12. // 读取一个字符串类型的redis
  13. $info = \Redis::get($key);
  14. dd($info);
  15. return view('test');
  16. }
  17. }
  • 页面响应 ![123](/content/images/2017/01/2.png)

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


LIST类型

  • 写入队列
  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'LIST:TEST:R';
  12. $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
  13. // 从右往左压入队列
  14. $info = \Redis::rpush($key,$names);
  15. dd($info);
  16. return view('test');
  17. }
  18. }
  • 页面响应 (写入的数量) ![](/content/images/2017/01/3.png)

  • 写入队列

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'LIST:TEST:R';
  12. $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
  13. // 获取队列内容(0到-1 所有 00是一位 01是两位)
  14. $info = \Redis::lrange($key,0,-1);
  15. dd($info);
  16. return view('test');
  17. }
  18. }
  • 页面响应 (数组) ![](/content/images/2017/01/4.png)

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

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'LIST:TEST:L';
  12. $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
  13. // 从左往右存数据
  14. \Redis::lpush($key,$names);
  15. // 取出数据
  16. $info = \Redis::lrange($key,0,-1);
  17. dd($info);
  18. return view('test');
  19. }
  20. }
  • 页面响应 (数组 是不是正好和上面的相反?) ![](/content/images/2017/01/5.png)

HASH类型

  • 存数据
  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'HASH:TEST';
  12. $names = ['id'=>'99',
  13. 'name'=>'AXiBa',
  14. 'age'=>'23',
  15. 'tel'=>'13995578699',
  16. 'addtime'=>'1231231233'];
  17. // 将数据写入hash
  18. $info = \Redis::hMset($key,$names);
  19. dd($info);
  20. return view('test');
  21. }
  22. }
  • 页面响应 ![](/content/images/2017/01/6.png)

  • 取数据(取所有)

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'HASH:TEST';
  12. $names = ['id'=>'99',
  13. 'name'=>'AXiBa',
  14. 'age'=>'23',
  15. 'tel'=>'13995578699',
  16. 'addtime'=>'1231231233'];
  17. // 取出hash里的数据
  18. $info = \Redis::hGetall($key);
  19. dd($info);
  20. return view('test');
  21. }
  22. }
  • 页面响应 ![](/content/images/2017/01/7.png)

  • 取数据(取个别字段)

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'HASH:TEST';
  12. $names = ['id'=>'99',
  13. 'name'=>'AXiBa',
  14. 'age'=>'23',
  15. 'tel'=>'13995578699',
  16. 'addtime'=>'1231231233'];
  17. // 取出hash里的 某一个字段的数据
  18. $info = \Redis::hGet($key,'name');
  19. dd($info);
  20. return view('test');
  21. }
  22. }
  • 页面响应 ![](/content/images/2017/01/8.png)
  1. // 判断这个redis key是否存在
  2. \Redis::exists($key);

SET类型

  • 写入一个无序集合(数据插入无顺序)
  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'SET:TEST';
  12. $value = ['a','b','c','d','e'];
  13. $info = \Redis::sadd($key,$value);
  14. $info = \Redis::smembers($key);
  15. dd($info);
  16. return view('test');
  17. }
  18. }
  • 页面响应 ![](/content/images/2017/01/9.png)

  • 求两个集合的交集

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'SET:TEST';
  12. $key1 = 'SET:TEST:1';
  13. $value = ['a','b','c','d','e'];
  14. $value1 = ['a','b','c','1','2'];
  15. // 写入另一个集合
  16. \Redis::sadd($key1,$value1);
  17. // 交集
  18. $info = \Redis::sinter($key,$key1);
  19. dd($info);
  20. return view('test');
  21. }
  22. }
  • 页面响应 ![](/content/images/2017/01/10.png)

  • 求两个集合的并集

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'SET:TEST';
  12. $key1 = 'SET:TEST:1';
  13. $value = ['a','b','c','d','e'];
  14. $value1 = ['a','b','c','1','2'];
  15. // 并集
  16. $info = \Redis::sunion($key,$key1);
  17. dd($info);
  18. return view('test');
  19. }
  20. }
  • 页面响应 ![](/content/images/2017/01/11.png)

  • 求两个集合的差集

  1. class PhotoController extends Controller
  2. {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return \Illuminate\Http\Response
  7. */
  8. public function index()
  9. {
  10. //
  11. $key = 'SET:TEST';
  12. $key1 = 'SET:TEST:1';
  13. $value = ['a','b','c','d','e'];
  14. $value1 = ['a','b','c','1','2'];
  15. // 差集
  16. $info = \Redis::sdiff($key,$key1);
  17. dd($info);
  18. return view('test');
  19. }
  20. }

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

  • 页面响应 ![](/content/images/2017/01/12.png)

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

[原文地址](http://www.blog8090.com/)

转载于:https://www.cnblogs.com/jhcyzxx/p/10479870.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/876918
推荐阅读
相关标签
  

闽ICP备14008679号