
批改狀態(tài):合格
老師批語:redis 是網(wǎng)站加速神器,一定要吃透!
use Illuminate\Support\Facades\Redis;
類,然后進(jìn)行redis的操作Redis::setex(key,ttl,value)
其中ttl是整數(shù),按秒計(jì)算json_decode('json字符串',true)
解碼json字符串時(shí),第二個(gè)參數(shù)設(shè)置為true解碼的數(shù)據(jù)轉(zhuǎn)換為數(shù)組,否則轉(zhuǎn)換為對象
<?php
namespace App\Http\Controllers\homes;
use App\Http\Controllers\Controller;
//引入數(shù)據(jù)庫查詢構(gòu)造器,鏈?zhǔn)秸{(diào)用
use Illuminate\Support\Facades\DB;
//引入redis
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;
//商城相關(guān)
class Shop extends Controller
{
public function index()
{
$res = [];
$res['products'] = DB::table('product')->limit(12)->lists();
//存放顯示的商品類別的編號
$cates = [];
if($res['products']){
$cates = array_column($res['products'],'cid');
}
//有選擇的加載商品分類
$res['cates'] = DB::table('product_cate')->whereIn('id',$cates)->cate('id');
// echo '<pre>';
// print_r($res);
// exit;
return view('/homes/shop/index',$res);
}
public function list()
{
return view('/homes/shop/list');
}
public function detail(Request $req)
{
$pid =(int)$req->pid;
$res['product'] = DB::table('product')->where('id',$pid)->item();
$res['detail'] = DB::table('product_detail')->where('proid',$pid)->item();
// echo '<pre>';
// print_r($res);
// exit;
//如果數(shù)據(jù)庫中有這條數(shù)據(jù)則渲染,否則跳轉(zhuǎn)到商品首頁
if($res['product'])
{
return view('/homes/shop/detail',$res);
}
else
{
return redirect('/homes/shop/index');
}
}
public function create_order(Request $req)
{
$pid = $req->pid;
}
//接入微信支付,形成微信支付二維碼
public function wxpay()
{
}
//redis測試
public function redis_test(Request $req)
{
$pid =(int)$req->pid;
$redisPro = 'product_'.$pid;
//從redis取數(shù)據(jù)
$data = Redis::get($redisPro);
// var_dump($data);
if($data)
{
//從redis中取出的數(shù)據(jù)是json,轉(zhuǎn)換為數(shù)組 ,第二個(gè)參數(shù)是true轉(zhuǎn)換為數(shù)組,否則轉(zhuǎn)換為對象
$res = json_decode($data,true);//
}
else
{
$res['product'] = DB::table('product')->where('id',$pid)->item();
$res['detail'] = DB::table('product_detail')->where('proid',$pid)->item();
//如果
if($res['product'])
{
// Redis::setex('key','過期時(shí)間(秒)','value')數(shù)據(jù)存入redis后,多少秒后刪除
Redis::setex($redisPro,86400,json_encode($res));//存一天
//Redis::set('key','value'); 存入redis
// Redis::set($redisPro,json_encode($res));
}
else
{
return redirect('/homes/shop/index');
}
}
// echo '<pre>';
// print_r($res);
// exit;
return view('/homes/shop/detail',$res);
}
}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號