一、本案例涉及知識
- Layui
- Redis
- Vue.js
- jQuery
- Ajax
二、效果圖

三、功能實現
(一)使用 Layui 的樣式構建頁面
!DOCTYPE html>
html>
head>
meta charset="utf-8">
title>Redis應用 - 搜索歷史/title>
!-- 引入 Layui CSS -->
link rel="stylesheet" href="css/layui.css" rel="external nofollow" >
/head>
body>
div class="layui-form" style="width: 50%;margin-top: 20px;" id="app">
div class="layui-form-item">
label class="layui-form-label">/label>
div class="layui-input-block">
input type="text" class="layui-input">
/div>
/div>
div class="layui-form-item">
label class="layui-form-label">/label>
div class="layui-input-block">
button class="layui-btn">搜索/button>
/div>
/div>
div class="layui-form-item">
label class="layui-form-label">/label>
div class="layui-input-block">
搜索歷史
/div>
/div>
div class="layui-form-item">
label class="layui-form-label">/label>
div class="layui-input-block">
span class="layui-badge layui-bg-gray" style="margin-left: 5px;">PHP/span>
span class="layui-badge layui-bg-gray" style="margin-left: 5px;">JavaScript/span>
/div>
/div>
/div>
!-- 引入 jQuery -->
script src="js/jquery-3.5.1.min.js">/script>
!-- 引入 Layui JS -->
script src="js/layui.js">/script>
!-- 引入 Vue.js -->
script src="js/vue.min.js">/script>
/body>
/html>
(二)點擊搜索時儲存本次搜索的關鍵字
給文本框添加 Vue 雙向綁定
input type="text" class="layui-input" v-model="keyword">
給搜索按鈕添加點擊事件
button class="layui-btn" @click="addHistory()">搜索/button>
script type="text/javascript">
var vm = new Vue({
el: "#app",
data: {
keyword: ""
},
methods: {
addHistory: function () {}
}
});
/script>
當文本框被輸入內容后,輸入的內容將綁定給 Vue 中 data
的 keyword
字段。
點擊搜索按鈕時,觸發 addHistory()
函數,此函數將輸入的內容發送給 PHP ,PHP 操作 Redis 將內容進行緩存。
addHistory()
函數中:
addHistory: function () {
$.ajax({
url: "history.php",
type: "GET",
data: {type: 'add', keyword: this.keyword},
success: function () {
// 請求成功后刷新本頁面
window.location.reload();
}
});
}
data
中傳值兩個字段,type
表示本次請求的類型,其中 add
代表往緩存中添加關鍵字,read
代表從緩存中讀取關鍵字。
history.php
中:
?php
$redis = new Redis();
$con = $redis->connect('localhost', 6379);
if (!$con) {
echo 'Redis連接失敗';
}
// 接收請求類型參數的值
$type = $_GET['type'];
// 模擬用戶的id,因為每個用戶搜索的內容不同,需要進行區分
$user_id = 'user-1';
// 如果請求類型為添加
if ($type == 'add') {
// 接收輸入的關鍵字
$keyword = $_GET['keyword'];
// 讀取當前用戶隊列中存儲的關鍵字個數,即隊列的長度
$len = $redis->llen($user_id);
// 如果個數大于等于 5 個,則刪除最開始搜索的關鍵字,加入最新搜索的關鍵字
if ($len >= 5) {
// 移除隊列左側的第一個關鍵字
$redis->lPop($user_id);
// 在隊列右側加入新的關鍵字
$redis->rPush($user_id, $keyword);
} else {
// 不多于 5 個直接在隊列右側加入新的關鍵字
$redis->rPush($user_id, $keyword);
}
}
(三)讀取并展示歷史搜索的關鍵字
第二步中加入了當請求添加緩存成功后會刷新頁面的代碼,
window.location.reload();
在這個基礎上,我們希望刷新的同時執行另一個 Ajax 請求從 PHP 中操作 Redis 將所有的歷史搜索關鍵字讀取出來并在頁面中展示。
所以在 Vue 中加入頁面加載完成自動調用getHistory()
函數:
methods: {
getHistory: function () {},
addHistory: function () {
$.ajax({
url: "history.php",
type: "GET",
data: {type: 'add', keyword: this.keyword},
success: function () {
window.location.reload();
}
});
}
},
// 頁面加載完成自動調用 getHistory()
created () {
this.getHistory();
}
getHistory()
函數中:
getHistory: function () {
$.ajax({
url: "history.php",
type: "GET",
data: {type: 'read'},
success: function (r) {
// JSON.parse(r) 將讀取到的 json 字符串轉為 json 對象
vm.history = JSON.parse(r);
}
});
}
data
中傳值一個字段,read
代表從緩存中讀取關鍵字,請求成功后將返回的結果賦值給 Vue 中 data
的 history
字段。
history.php
中添加讀取操作:
// 如果請求類型為讀取
if ($type == 'read') {
// 從隊列左側依次取出 5 個關鍵字
$history = $redis->lrange($user_id, 0, 4);
// 轉為 json 格式的數據并輸出到頁面中供 Ajax 使用
echo json_encode($history, JSON_UNESCAPED_UNICODE);
}
將讀取到的數據成功賦值給 Vue 中 data
的 history
字段后,頁面中即可將數據循環輸出展示:
span class="layui-badge layui-bg-gray" v-for="item in history" style="margin-left: 5px;">{{item}}/span>
連貫過程為:用戶輸入關鍵字并點擊搜索按鈕,Ajax 請求 PHP 操作 Redis 進行數據緩存且緩存成功后刷新頁面,頁面刷新后自動調用函數執行 Ajax 請求 PHP 操作 Redis 進行緩存數據的讀取并返回于頁面中同時進行渲染展示。
到此這篇關于Redis 緩存實現存儲和讀取歷史搜索關鍵字的文章就介紹到這了,更多相關Redis 緩存實現存儲和讀取關鍵字內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時間解決方案
- SpringCache 分布式緩存的實現方法(規避redis解鎖的問題)
- NestJS+Redis實現緩存步驟詳解