Blogger Information
Blog 64
fans 6
comment 2
visits 82958
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel--phpstudy--redis 基本用法
王娇
Original
1842 people have browsed it

学习总结

如果想在larave中使用redis,需要引入use Illuminate\Support\Facades\Redis;类,然后进行redis的操作

设置redis数据存储的过期时间使用Redis::setex(key,ttl,value)其中ttl是整数,按秒计算

使用json_decode('json字符串',true)解码json字符串时,第二个参数设置为true解码的数据转换为数组,否则转换为对象

1.在phpstudy中下载redis服务

环境>工具>redis>更多

redis3.0.504是redis服务,下载

redisClient2.0.0是redis可视化存储工具,下载

下载后在服务中启动redis服务

2.php中安装redis扩展

在“环境”中选择所用的php版本,点“设置”

在“扩展组件”中打开redis扩展的开关

3.Shop控制器中的redis_test是redis的测试代码

  1. <?php
  2. namespace App\Http\Controllers\homes;
  3. use App\Http\Controllers\Controller;
  4. //引入数据库查询构造器,链式调用
  5. use Illuminate\Support\Facades\DB;
  6. //引入redis
  7. use Illuminate\Support\Facades\Redis;
  8. use Illuminate\Http\Request;
  9. //商城相关
  10. class Shop extends Controller
  11. {
  12. public function index()
  13. {
  14. $res = [];
  15. $res['products'] = DB::table('product')->limit(12)->lists();
  16. //存放显示的商品类别的编号
  17. $cates = [];
  18. if($res['products']){
  19. $cates = array_column($res['products'],'cid');
  20. }
  21. //有选择的加载商品分类
  22. $res['cates'] = DB::table('product_cate')->whereIn('id',$cates)->cate('id');
  23. // echo '<pre>';
  24. // print_r($res);
  25. // exit;
  26. return view('/homes/shop/index',$res);
  27. }
  28. public function list()
  29. {
  30. return view('/homes/shop/list');
  31. }
  32. public function detail(Request $req)
  33. {
  34. $pid =(int)$req->pid;
  35. $res['product'] = DB::table('product')->where('id',$pid)->item();
  36. $res['detail'] = DB::table('product_detail')->where('proid',$pid)->item();
  37. // echo '<pre>';
  38. // print_r($res);
  39. // exit;
  40. //如果数据库中有这条数据则渲染,否则跳转到商品首页
  41. if($res['product'])
  42. {
  43. return view('/homes/shop/detail',$res);
  44. }
  45. else
  46. {
  47. return redirect('/homes/shop/index');
  48. }
  49. }
  50. public function create_order(Request $req)
  51. {
  52. $pid = $req->pid;
  53. }
  54. //接入微信支付,形成微信支付二维码
  55. public function wxpay()
  56. {
  57. }
  58. //redis测试
  59. public function redis_test(Request $req)
  60. {
  61. $pid =(int)$req->pid;
  62. $redisPro = 'product_'.$pid;
  63. //从redis取数据
  64. $data = Redis::get($redisPro);
  65. // var_dump($data);
  66. if($data)
  67. {
  68. //从redis中取出的数据是json,转换为数组 ,第二个参数是true转换为数组,否则转换为对象
  69. $res = json_decode($data,true);//
  70. }
  71. else
  72. {
  73. $res['product'] = DB::table('product')->where('id',$pid)->item();
  74. $res['detail'] = DB::table('product_detail')->where('proid',$pid)->item();
  75. //如果
  76. if($res['product'])
  77. {
  78. // Redis::setex('key','过期时间(秒)','value')数据存入redis后,多少秒后删除
  79. Redis::setex($redisPro,86400,json_encode($res));//存一天
  80. //Redis::set('key','value'); 存入redis
  81. // Redis::set($redisPro,json_encode($res));
  82. }
  83. else
  84. {
  85. return redirect('/homes/shop/index');
  86. }
  87. }
  88. // echo '<pre>';
  89. // print_r($res);
  90. // exit;
  91. return view('/homes/shop/detail',$res);
  92. }
  93. }

redis保存后,打开redisClient查看数据是否保存成功

页面刷新效果图

Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:redis 是网站加速神器,一定要吃透!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
P粉835050979 2022-08-27 21:57:21
有用,很不错,一次就过
1 floor
Author's latest blog post