亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 64
粉絲 6
評論 2
訪問量 100610
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
laravel--phpstudy--redis 基本用法
王嬌
原創(chuàng)
2272人瀏覽過

學習總結(jié)

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

設(shè)置redis數(shù)據(jù)存儲的過期時間使用Redis::setex(key,ttl,value)其中ttl是整數(shù),按秒計算

使用json_decode('json字符串',true)解碼json字符串時,第二個參數(shù)設(shè)置為true解碼的數(shù)據(jù)轉(zhuǎn)換為數(shù)組,否則轉(zhuǎn)換為對象

1.在phpstudy中下載redis服務(wù)

環(huán)境>工具>redis>更多

redis3.0.504是redis服務(wù),下載

redisClient2.0.0是redis可視化存儲工具,下載

下載后在服務(wù)中啟動redis服務(wù)

2.php中安裝redis擴展

在“環(huán)境”中選擇所用的php版本,點“設(shè)置”

在“擴展組件”中打開redis擴展的開關(guān)

3.Shop控制器中的redis_test是redis的測試代碼

  1. <?php
  2. namespace App\Http\Controllers\homes;
  3. use App\Http\Controllers\Controller;
  4. //引入數(shù)據(jù)庫查詢構(gòu)造器,鏈式調(diào)用
  5. use Illuminate\Support\Facades\DB;
  6. //引入redis
  7. use Illuminate\Support\Facades\Redis;
  8. use Illuminate\Http\Request;
  9. //商城相關(guān)
  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. //如果數(shù)據(jù)庫中有這條數(shù)據(jù)則渲染,否則跳轉(zhuǎn)到商品首頁
  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取數(shù)據(jù)
  64. $data = Redis::get($redisPro);
  65. // var_dump($data);
  66. if($data)
  67. {
  68. //從redis中取出的數(shù)據(jù)是json,轉(zhuǎn)換為數(shù)組 ,第二個參數(shù)是true轉(zhuǎn)換為數(shù)組,否則轉(zhuǎn)換為對象
  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')數(shù)據(jù)存入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查看數(shù)據(jù)是否保存成功

頁面刷新效果圖

批改老師:GuanhuiGuanhui

批改狀態(tài):合格

老師批語:redis 是網(wǎng)站加速神器,一定要吃透!
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
1條評論
P粉835050979 2022-08-27 21:57:21
有用,很不錯,一次就過
1樓
作者最新博文
關(guān)于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓,幫助PHP學習者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學習
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學習!
    全站2000+教程免費學