佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Redis 緩存實現(xiàn)存儲和讀取歷史搜索關(guān)鍵字的操作方法

Redis 緩存實現(xiàn)存儲和讀取歷史搜索關(guān)鍵字的操作方法

熱門標(biāo)簽:網(wǎng)站排名優(yōu)化 百度競價排名 Linux服務(wù)器 呼叫中心市場需求 鐵路電話系統(tǒng) 服務(wù)外包 地方門戶網(wǎng)站 AI電銷

一、本案例涉及知識

  1.  Layui
  2. Redis
  3. Vue.js
  4. jQuery
  5. Ajax

二、效果圖

三、功能實現(xiàn)

(一)使用 Layui 的樣式構(gòu)建頁面

!DOCTYPE html>
html>
head>
 meta charset="utf-8">
 title>Redis應(yīng)用 - 搜索歷史/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>

(二)點擊搜索時儲存本次搜索的關(guān)鍵字

給文本框添加 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>

當(dāng)文本框被輸入內(nèi)容后,輸入的內(nèi)容將綁定給 Vue 中 datakeyword 字段。

點擊搜索按鈕時,觸發(fā) addHistory() 函數(shù),此函數(shù)將輸入的內(nèi)容發(fā)送給 PHP ,PHP 操作 Redis 將內(nèi)容進行緩存。

addHistory() 函數(shù)中:

addHistory: function () {
 $.ajax({
  url: "history.php",
  type: "GET",
  data: {type: 'add', keyword: this.keyword},
  success: function () {
  	// 請求成功后刷新本頁面
   window.location.reload();
  }
 });
}

data 中傳值兩個字段,type 表示本次請求的類型,其中 add 代表往緩存中添加關(guān)鍵字,read 代表從緩存中讀取關(guān)鍵字。

history.php 中:

?php
$redis = new Redis();
$con = $redis->connect('localhost', 6379);
if (!$con) {
 echo 'Redis連接失敗';
}
// 接收請求類型參數(shù)的值
$type = $_GET['type'];
// 模擬用戶的id,因為每個用戶搜索的內(nèi)容不同,需要進行區(qū)分
$user_id = 'user-1';
// 如果請求類型為添加
if ($type == 'add') {
	// 接收輸入的關(guān)鍵字
 $keyword = $_GET['keyword'];
 // 讀取當(dāng)前用戶隊列中存儲的關(guān)鍵字個數(shù),即隊列的長度
 $len = $redis->llen($user_id);
 // 如果個數(shù)大于等于 5 個,則刪除最開始搜索的關(guān)鍵字,加入最新搜索的關(guān)鍵字
 if ($len >= 5) {
 	// 移除隊列左側(cè)的第一個關(guān)鍵字
  $redis->lPop($user_id);
  // 在隊列右側(cè)加入新的關(guān)鍵字
  $redis->rPush($user_id, $keyword);
 } else {
 	// 不多于 5 個直接在隊列右側(cè)加入新的關(guān)鍵字
  $redis->rPush($user_id, $keyword);
 }
}

(三)讀取并展示歷史搜索的關(guān)鍵字

第二步中加入了當(dāng)請求添加緩存成功后會刷新頁面的代碼,

window.location.reload();

在這個基礎(chǔ)上,我們希望刷新的同時執(zhí)行另一個 Ajax 請求從 PHP 中操作 Redis 將所有的歷史搜索關(guān)鍵字讀取出來并在頁面中展示。

所以在 Vue 中加入頁面加載完成自動調(diào)用getHistory()函數(shù):

methods: {
 getHistory: function () {},
 addHistory: function () {
  $.ajax({
   url: "history.php",
   type: "GET",
   data: {type: 'add', keyword: this.keyword},
   success: function () {
    window.location.reload();
   }
  });
 }
},
// 頁面加載完成自動調(diào)用 getHistory()
created () {
 this.getHistory();
}

getHistory()函數(shù)中:

getHistory: function () {
 $.ajax({
  url: "history.php",
  type: "GET",
  data: {type: 'read'},
  success: function (r) {
  	// JSON.parse(r) 將讀取到的 json 字符串轉(zhuǎn)為 json 對象
   vm.history = JSON.parse(r);
  }
 });
}

data 中傳值一個字段,read 代表從緩存中讀取關(guān)鍵字,請求成功后將返回的結(jié)果賦值給 Vue 中 datahistory 字段。

history.php 中添加讀取操作:

// 如果請求類型為讀取
if ($type == 'read') {
	// 從隊列左側(cè)依次取出 5 個關(guān)鍵字
 $history = $redis->lrange($user_id, 0, 4);
 // 轉(zhuǎn)為 json 格式的數(shù)據(jù)并輸出到頁面中供 Ajax 使用
 echo json_encode($history, JSON_UNESCAPED_UNICODE);
}

將讀取到的數(shù)據(jù)成功賦值給 Vue 中 datahistory 字段后,頁面中即可將數(shù)據(jù)循環(huán)輸出展示:

span class="layui-badge layui-bg-gray" v-for="item in history" style="margin-left: 5px;">{{item}}/span>

連貫過程為:用戶輸入關(guān)鍵字并點擊搜索按鈕,Ajax 請求 PHP 操作 Redis 進行數(shù)據(jù)緩存且緩存成功后刷新頁面,頁面刷新后自動調(diào)用函數(shù)執(zhí)行 Ajax 請求 PHP 操作 Redis 進行緩存數(shù)據(jù)的讀取并返回于頁面中同時進行渲染展示。

到此這篇關(guān)于Redis 緩存實現(xiàn)存儲和讀取歷史搜索關(guān)鍵字的文章就介紹到這了,更多相關(guān)Redis 緩存實現(xiàn)存儲和讀取關(guān)鍵字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時間解決方案
  • SpringCache 分布式緩存的實現(xiàn)方法(規(guī)避redis解鎖的問題)
  • NestJS+Redis實現(xiàn)緩存步驟詳解

標(biāo)簽:黃山 蘭州 湘潭 衡水 仙桃 銅川 崇左 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Redis 緩存實現(xiàn)存儲和讀取歷史搜索關(guān)鍵字的操作方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    德钦县| 广河县| 古浪县| 井研县| 呼图壁县| 隆昌县| 临海市| 东乡| 黎平县| 荃湾区| 九江市| 漾濞| 雅江县| 新巴尔虎右旗| 丰台区| 竹山县| 高邑县| 兴城市| 上饶市| 光山县| 益阳市| 荔波县| 资源县| 夏邑县| 平乐县| 波密县| 余江县| 诏安县| 泰兴市| 娱乐| 福泉市| 波密县| 安图县| 师宗县| 莱州市| 科尔| 开平市| 论坛| 新疆| 平定县| 汝阳县|