日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

实战SSM_O2O商铺_46【Redis缓存】头条信息+商铺目录Service层加入缓存

發布時間:2025/3/21 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实战SSM_O2O商铺_46【Redis缓存】头条信息+商铺目录Service层加入缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • HeadLineServiceImpl的改造
    • 代碼
    • 單元測試
  • ShopCategoryServiceImpl的改造
    • 代碼
    • 單元測試
  • Github地址

概述

根據數據的特點,不經常變動的數據 即時性要求沒有那么高的讀數據 為了減輕DB壓力,我們可以將數據放到緩存中。

按照規劃,目前我們需要將區域信息、商鋪分類信息和頭條信息放入到redis中。

實戰SSM_O2O商鋪_45【Redis緩存】配置Redis在Service層加入緩存中我們集成了Redis的配置,并使用AreaService來測試了配置的正確性。 接下來我們繼續將商鋪分類信息和頭條信息放入到redis中。


HeadLineServiceImpl的改造

代碼

思路:根據mapper中不同的查詢條件,在service層緩存不同的KEY,方便使用。


package com.artisan.o2o.service.impl;import java.util.ArrayList; import java.util.List;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.artisan.o2o.cache.JedisUtil; import com.artisan.o2o.dao.HeadLineDao; import com.artisan.o2o.entity.HeadLine; import com.artisan.o2o.service.HeadLineService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper;@Service public class HeadLineServiceImpl implements HeadLineService {private static final Logger logger = LoggerFactory.getLogger(HeadLineServiceImpl.class);@AutowiredHeadLineDao headLineDao;@Autowiredprivate JedisUtil.Strings jedisStrings;@Autowiredprivate JedisUtil.Keys jedisKeys;@Overridepublic List<HeadLine> queryHeadLineList(HeadLine headLineConditon) {List<HeadLine> headLineList = new ArrayList<HeadLine>();// 定義KeyString key = "headline";// 定義jackson數據轉換操作類ObjectMapper mapper = new ObjectMapper();// 根據mapper中的查詢條件 拼裝key// 根據不同的條件緩存不同的key值 這里有3種緩存 headline_0 headline_1 和 headline 方便管理員權限操作if (headLineConditon != null && headLineConditon.getEnableStatus() != null) {key = key + "_" + headLineConditon.getEnableStatus();}// 如果不存在,從數據庫中獲取數據,然后寫入redisif(!jedisKeys.exists(key)){try {// 從DB中獲取數據headLineList = headLineDao.selectHeadLineList(headLineConditon);// 將相關的實體類集合轉換成string,存入redis里面對應的key中String jsonString = mapper.writeValueAsString(headLineList);jedisStrings.set(key, jsonString);} catch (JsonProcessingException e) {e.printStackTrace();logger.error("實體類集合轉換string存入redis異常{}", e.getMessage());} catch (Exception e) {e.printStackTrace();logger.error("其他異常{}", e.getMessage());}} else {// 否則直接從redis中獲取try {// 若存在,則直接從redis里面取出相應數據String jsonString = jedisStrings.get(key);// 指定要將string轉換成的集合類型JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, HeadLine.class);// 將相關key對應的value里的的string轉換成java對象的實體類集合headLineList = mapper.readValue(jsonString, javaType);} catch (Exception e) {e.printStackTrace();logger.error("異常{}", e.getMessage());}}return headLineList;}}

單元測試

tb_head_line數據

package com.artisan.o2o.service;import java.util.List;import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest; import com.artisan.o2o.entity.HeadLine;public class HeadLineServiceTest extends BaseTest {@Autowiredprivate HeadLineService headLineService;@Testpublic void testQueryHeadLineList() {HeadLine headLineConditon = new HeadLine();// 狀態 0 不可用 1 可用headLineConditon.setEnableStatus(0);// 查詢不可用的頭條信息List<HeadLine> headLineList = headLineService.queryHeadLineList(headLineConditon);for (HeadLine headLine : headLineList) {System.out.println("0<<<<" + headLine);}// 查詢可用的頭條信息headLineConditon.setEnableStatus(1);headLineList = headLineService.queryHeadLineList(headLineConditon);for (HeadLine headLine : headLineList) {System.out.println("**------>" + headLine);}// 再次查詢 狀態為0 和 1的頭條信息 ,確保從緩存中取數據// 查詢不可用的頭條信息headLineConditon.setEnableStatus(0);headLineList = headLineService.queryHeadLineList(headLineConditon);for (HeadLine headLine : headLineList) {System.out.println("0>>>>" + headLine);}// 查詢可用的頭條信息headLineConditon.setEnableStatus(1);headLineList = headLineService.queryHeadLineList(headLineConditon);for (HeadLine headLine : headLineList) {System.out.println("||------>" + headLine);}} }

啟動redis服務端,確保redis中無對應的數據,加入斷點逐步調測觀察。 單元測試后查看Redis中的數據

127.0.0.1:6379> get headline_0 "[]" 127.0.0.1:6379> get headline_1 "[{\"lineId\":6,\"lineName\":\"\xe8\xb4\xad\xe7\x89\xa9\",\"lineLink\":\"xxx\",\"lineImg\":\"\\\\upload\\\\item\\\\headtitle\\\\2018072520315746624.jpg\",\"priority\":99,\"enableStatus\":1,\"createTime\":null,\"lastEditTime\":null},{\"lineId\":2,\"lineName\":\"\xe5\xae\xb6\xe5\x85\xb7\",\"lineLink\":\"x\",\"lineImg\":\"\\\\upload\\\\item\\\\headtitle\\\\2018072520371786788.jpg\",\"priority\":98,\"enableStatus\":1,\"createTime\":null,\"lastEditTime\":null},{\"lineId\":3,\"lineName\":\"\xe5\x81\xa5\xe8\xba\xab\",\"lineLink\":\"xx\",\"lineImg\":\"\\\\upload\\\\item\\\\headtitle\\\\2018072520393452772.jpg\",\"priority\":97,\"enableStatus\":1,\"createTime\":null,\"lastEditTime\":null},{\"lineId\":4,\"lineName\":\"\xe7\xbe\x8e\xe5\xae\xb9\",\"lineLink\":\"aa\",\"lineImg\":\"\\\\upload\\\\item\\\\headtitle\\\\2018072520400198256.jpg\",\"priority\":96,\"enableStatus\":1,\"createTime\":null,\"lastEditTime\":null}]" 127.0.0.1:6379>

符合預期,測試通過。


ShopCategoryServiceImpl的改造

思路:根據mapper中不同的查詢條件,在service層緩存不同的KEY,方便使用。


代碼

package com.artisan.o2o.service.impl;import java.util.ArrayList; import java.util.List;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.artisan.o2o.cache.JedisUtil; import com.artisan.o2o.dao.ShopCategoryDao; import com.artisan.o2o.entity.ShopCategory; import com.artisan.o2o.service.ShopCategoryService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper;@Service public class ShopCategoryServiceImpl implements ShopCategoryService {private static final Logger logger = LoggerFactory.getLogger(ShopCategoryServiceImpl.class);@Autowiredprivate ShopCategoryDao shopCategoryDao;@Autowiredprivate JedisUtil.Keys jedisKeys;@Autowiredprivate JedisUtil.Strings jedisStrings;@Overridepublic List<ShopCategory> getShopCategoryList(ShopCategory shopCategory) {List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();// 定義redis中keyString key = "shopcategory";// 定義jackson數據轉換操作類ObjectMapper mapper = new ObjectMapper();// 根據mapper中的查詢條件,拼裝shopcategory的keyif (shopCategory == null) {// 查詢條件為空,列出所有的首頁大類,即parentId為空的店鋪類別key = key + "_allfirstlevelshopcategory";} else if (shopCategory != null && shopCategory.getParent() != null && shopCategory.getParent().getShopCategoryId() != null) {// 列出某個parentId下面的所有子類key = key + "_parent" + shopCategory.getParent().getShopCategoryId();} else if (shopCategory != null) {// 列出所有的子類,不管屬于哪個類key = key + "_allsecondlevelshopcategory";}// 如果緩存中不出在則從DB中查詢并緩存到redis中if (!jedisKeys.exists(key)) {try {// 從DB中加載shopCategoryList = shopCategoryDao.queryShopCategoryList(shopCategory);// 將相關的實體類集合轉換成string,存入redis里面對應的key中String jsonString = mapper.writeValueAsString(shopCategoryList);jedisStrings.set(key, jsonString);} catch (JsonProcessingException e) {e.printStackTrace();logger.error("實體類集合轉換string存入redis異常{}", e.getMessage());} catch (Exception e) {e.printStackTrace();logger.error("其他異常{}", e.getMessage());}} else {// 否則直接從redis中獲取try {String jsonString = jedisStrings.get(key);// 指定要將string轉換成的集合類型JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, ShopCategory.class);// 將相關key對應的value里的的string轉換成java對象的實體類集合shopCategoryList = mapper.readValue(jsonString, javaType);} catch (Exception e) {e.printStackTrace();logger.error("異常{}", e.getMessage());}}return shopCategoryList;}}

單元測試

tb_shop_category 數據

package com.artisan.o2o.service;import java.util.List;import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest; import com.artisan.o2o.entity.ShopCategory;public class ShopServiceCategoryTest extends BaseTest {@AutowiredShopCategoryService shopCategoryService;@Testpublic void testQueryShopCategory() {List<ShopCategory> shopCategories;// 查詢 ShopCategory為null的情況,即查詢parent_id is nullshopCategories = shopCategoryService.getShopCategoryList(null);for (ShopCategory shopCategory2 : shopCategories) {System.out.println("-----||" + shopCategory2);}// 查詢 ShopCategory不為空的情況ShopCategory shopCategory = new ShopCategory();shopCategories = shopCategoryService.getShopCategoryList(shopCategory);for (ShopCategory shopCategory2 : shopCategories) {System.out.println("----->>" + shopCategory2);}// 查詢對應父類下的目錄ShopCategory parent = new ShopCategory();ShopCategory child = new ShopCategory();parent.setShopCategoryId(1L);child.setParent(parent);shopCategories = shopCategoryService.getShopCategoryList(child);for (ShopCategory shopCategory2 : shopCategories) {System.out.println("-----**" + shopCategory2);}} }

運行兩次,查看數據,符合預期


Github地址

代碼地址: https://github.com/yangshangwei/o2o

總結

以上是生活随笔為你收集整理的实战SSM_O2O商铺_46【Redis缓存】头条信息+商铺目录Service层加入缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。