欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年12月10日 10:51:34   作者:相逢不晚何必匆匆  
這篇文章主要介紹了Redis 緩存實(shí)現(xiàn)存儲(chǔ)和讀取歷史搜索關(guān)鍵字,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、本案例涉及知識(shí)

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

二、效果圖

搜索并記錄關(guān)鍵字

三、功能實(shí)現(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>

(二)點(diǎn)擊搜索時(shí)儲(chǔ)存本次搜索的關(guān)鍵字

給文本框添加 Vue 雙向綁定

<input type="text" class="layui-input" v-model="keyword">

給搜索按鈕添加點(diǎn)擊事件

<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 字段。

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

addHistory() 函數(shù)中:

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

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

history.php 中:

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

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

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

window.location.reload();

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

所以在 Vue 中加入頁面加載完成自動(dòng)調(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();
   }
  });
 }
},
// 頁面加載完成自動(dòng)調(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 對(duì)象
   vm.history = JSON.parse(r);
  }
 });
}

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

history.php 中添加讀取操作:

// 如果請(qǐng)求類型為讀取
if ($type == 'read') {
	// 從隊(duì)列左側(cè)依次取出 5 個(gè)關(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)鍵字并點(diǎn)擊搜索按鈕,Ajax 請(qǐng)求 PHP 操作 Redis 進(jìn)行數(shù)據(jù)緩存且緩存成功后刷新頁面,頁面刷新后自動(dòng)調(diào)用函數(shù)執(zhí)行 Ajax 請(qǐng)求 PHP 操作 Redis 進(jìn)行緩存數(shù)據(jù)的讀取并返回于頁面中同時(shí)進(jìn)行渲染展示。

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

相關(guān)文章

  • Redis集群Lettuce主從切換問題解決方案

    Redis集群Lettuce主從切換問題解決方案

    這篇文章主要為大家介紹了Redis集群Lettuce主從切換問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • NoSQL和Redis簡介及Redis在Windows下的安裝和使用教程

    NoSQL和Redis簡介及Redis在Windows下的安裝和使用教程

    這篇文章主要介紹了NoSQL和Redis簡介及Redis在Windows下的安裝和使用教程,本文同時(shí)講解了python操作redis,并給出了操作實(shí)例,需要的朋友可以參考下
    2015-01-01
  • Redis序列化設(shè)置以及jetcache連接Redis序列化的設(shè)置過程

    Redis序列化設(shè)置以及jetcache連接Redis序列化的設(shè)置過程

    這篇文章主要介紹了Redis序列化設(shè)置以及jetcache連接Redis序列化的設(shè)置過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • redis專屬鏈表ziplist的使用

    redis專屬鏈表ziplist的使用

    本文主要介紹了redis專屬鏈表ziplist的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • redis 交集、并集、差集的具體使用

    redis 交集、并集、差集的具體使用

    這篇文章主要介紹了redis 交集、并集、差集的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 如何自定義redis工具jar包供其他SpringBoot項(xiàng)目直接使用

    如何自定義redis工具jar包供其他SpringBoot項(xiàng)目直接使用

    這篇文章主要介紹了如何自定義redis工具jar包供其他SpringBoot項(xiàng)目直接使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • redis如何取hash的值

    redis如何取hash的值

    這篇文章主要介紹了redis如何取hash的值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • redis?key鍵過期刪除策略及淘汰機(jī)制探究

    redis?key鍵過期刪除策略及淘汰機(jī)制探究

    這篇文章主要為大家介紹了redis?key鍵過期刪除策略及淘汰機(jī)制探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Redis入門教程_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Redis入門教程_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Redis是一款開源的、高性能的鍵-值存儲(chǔ)(key-value store)。下面通過本文大家分享Redis入門教程,感興趣的朋友參考下吧
    2017-08-08
  • Redis實(shí)現(xiàn)事物以及鎖的方法

    Redis實(shí)現(xiàn)事物以及鎖的方法

    本文主要介紹了Redis實(shí)現(xiàn)事物以及鎖的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評(píng)論