婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Redis 緩存實現存儲和讀取歷史搜索關鍵字的操作方法

Redis 緩存實現存儲和讀取歷史搜索關鍵字的操作方法

熱門標簽:鄭州人工智能電銷機器人系統 十堰營銷電銷機器人哪家便宜 宿遷便宜外呼系統平臺 北京400電話辦理收費標準 超呼電話機器人 魔獸2青云地圖標注 山東外呼銷售系統招商 日本中國地圖標注 貴州電銷卡外呼系統

一、本案例涉及知識

  1.  Layui
  2. Redis
  3. Vue.js
  4. jQuery
  5. 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 中 datakeyword 字段。

點擊搜索按鈕時,觸發 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 中 datahistory 字段。

history.php 中添加讀取操作:

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

將讀取到的數據成功賦值給 Vue 中 datahistory 字段后,頁面中即可將數據循環輸出展示:

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實現緩存步驟詳解

標簽:楊凌 臺州 朝陽 北京 大慶 江蘇 果洛 吉安

巨人網絡通訊聲明:本文標題《Redis 緩存實現存儲和讀取歷史搜索關鍵字的操作方法》,本文關鍵詞  Redis,緩存,實現,存儲,和,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Redis 緩存實現存儲和讀取歷史搜索關鍵字的操作方法》相關的同類信息!
  • 本頁收集關于Redis 緩存實現存儲和讀取歷史搜索關鍵字的操作方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 攀枝花市| 孙吴县| 平罗县| 永丰县| 太白县| 宁武县| 会东县| 曲松县| 古交市| 凤庆县| 宁波市| 吉木乃县| 杭锦后旗| 郓城县| 广丰县| 耿马| 林芝县| 嘉禾县| 天镇县| 永泰县| 德庆县| 饶河县| 应城市| 固原市| 滦平县| 宾川县| 文成县| 乐安县| 汝阳县| 阿合奇县| 榆中县| 平安县| 义马市| 泰来县| 台北县| 崇阳县| 毕节市| 六枝特区| 九江县| 南涧| 巴彦县|