实战SSM_O2O商铺_46【Redis缓存】头条信息+商铺目录Service层加入缓存
文章目錄
- 概述
- HeadLineServiceImpl的改造
- 代碼
- 單元測試
- ShopCategoryServiceImpl的改造
- 代碼
- 單元測試
- Github地址
概述
根據(jù)數(shù)據(jù)的特點(diǎn),不經(jīng)常變動的數(shù)據(jù) 即時性要求沒有那么高的讀數(shù)據(jù) 為了減輕DB壓力,我們可以將數(shù)據(jù)放到緩存中。
按照規(guī)劃,目前我們需要將區(qū)域信息、商鋪分類信息和頭條信息放入到redis中。
實(shí)戰(zhàn)SSM_O2O商鋪_45【Redis緩存】配置Redis在Service層加入緩存中我們集成了Redis的配置,并使用AreaService來測試了配置的正確性。 接下來我們繼續(xù)將商鋪分類信息和頭條信息放入到redis中。
HeadLineServiceImpl的改造
代碼
思路:根據(jù)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數(shù)據(jù)轉(zhuǎn)換操作類ObjectMapper mapper = new ObjectMapper();// 根據(jù)mapper中的查詢條件 拼裝key// 根據(jù)不同的條件緩存不同的key值 這里有3種緩存 headline_0 headline_1 和 headline 方便管理員權(quán)限操作if (headLineConditon != null && headLineConditon.getEnableStatus() != null) {key = key + "_" + headLineConditon.getEnableStatus();}// 如果不存在,從數(shù)據(jù)庫中獲取數(shù)據(jù),然后寫入redisif(!jedisKeys.exists(key)){try {// 從DB中獲取數(shù)據(jù)headLineList = headLineDao.selectHeadLineList(headLineConditon);// 將相關(guān)的實(shí)體類集合轉(zhuǎn)換成string,存入redis里面對應(yīng)的key中String jsonString = mapper.writeValueAsString(headLineList);jedisStrings.set(key, jsonString);} catch (JsonProcessingException e) {e.printStackTrace();logger.error("實(shí)體類集合轉(zhuǎn)換string存入redis異常{}", e.getMessage());} catch (Exception e) {e.printStackTrace();logger.error("其他異常{}", e.getMessage());}} else {// 否則直接從redis中獲取try {// 若存在,則直接從redis里面取出相應(yīng)數(shù)據(jù)String jsonString = jedisStrings.get(key);// 指定要將string轉(zhuǎn)換成的集合類型JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, HeadLine.class);// 將相關(guān)key對應(yīng)的value里的的string轉(zhuǎn)換成java對象的實(shí)體類集合headLineList = mapper.readValue(jsonString, javaType);} catch (Exception e) {e.printStackTrace();logger.error("異常{}", e.getMessage());}}return headLineList;}}
單元測試
tb_head_line數(shù)據(jù)
啟動redis服務(wù)端,確保redis中無對應(yīng)的數(shù)據(jù),加入斷點(diǎn)逐步調(diào)測觀察。 單元測試后查看Redis中的數(shù)據(jù)
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>符合預(yù)期,測試通過。
ShopCategoryServiceImpl的改造
思路:根據(jù)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數(shù)據(jù)轉(zhuǎn)換操作類ObjectMapper mapper = new ObjectMapper();// 根據(jù)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);// 將相關(guān)的實(shí)體類集合轉(zhuǎn)換成string,存入redis里面對應(yīng)的key中String jsonString = mapper.writeValueAsString(shopCategoryList);jedisStrings.set(key, jsonString);} catch (JsonProcessingException e) {e.printStackTrace();logger.error("實(shí)體類集合轉(zhuǎn)換string存入redis異常{}", e.getMessage());} catch (Exception e) {e.printStackTrace();logger.error("其他異常{}", e.getMessage());}} else {// 否則直接從redis中獲取try {String jsonString = jedisStrings.get(key);// 指定要將string轉(zhuǎn)換成的集合類型JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, ShopCategory.class);// 將相關(guān)key對應(yīng)的value里的的string轉(zhuǎn)換成java對象的實(shí)體類集合shopCategoryList = mapper.readValue(jsonString, javaType);} catch (Exception e) {e.printStackTrace();logger.error("異常{}", e.getMessage());}}return shopCategoryList;}}單元測試
tb_shop_category 數(shù)據(jù)
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);}// 查詢對應(yīng)父類下的目錄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);}} }運(yùn)行兩次,查看數(shù)據(jù),符合預(yù)期
Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結(jié)
以上是生活随笔為你收集整理的实战SSM_O2O商铺_46【Redis缓存】头条信息+商铺目录Service层加入缓存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_45【Redis
- 下一篇: 实战SSM_O2O商铺_47【Redis