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

歡迎訪問 生活随笔!

生活随笔

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

数据库

实战SSM_O2O商铺_47【Redis缓存】清除缓存接口的开发

發布時間:2025/3/21 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实战SSM_O2O商铺_47【Redis缓存】清除缓存接口的开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • 接口層改造
  • CacheService接口
  • CacheService接口實現類
  • 工具類中的方法
  • 單元測試
  • Github地址

概述

設計如下: 在接口層傳入緩存key的前綴,通過匹配的方式將能匹配到該前綴的所有key均刪除。

舉個例子

如上3個key,當我們傳入shopcategory這個前綴時,會將如上3個前綴全部清除掉。


接口層改造

為了方便使用前綴,我們在將之前定義在方法體中的前綴抽取到接口層,如下所示

public interface AreaService {// redis key的前綴,抽取到接口層,方便使用public static final String AREALISTKEY = "arealist";.......... } public interface HeadLineService {// redis key的前綴,抽取到接口層,方便使用public static final String HEADLINEKEY = "headline";.......... } public interface ShopCategoryService {// redis key的前綴,抽取到接口層,方便使用public static final String SCLISTKEY = "shopcategory";.......... }

CacheService接口

package com.artisan.o2o.service;public interface CacheService {/*** * * @Title: removeFromCache* * @Description: 根據緩存的前綴清理匹配的全部緩存* * @param keyPrefix* * @return: void*/void removeFromCache(String keyPrefix); }

CacheService接口實現類

package com.artisan.o2o.service.impl;import java.util.Set;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.artisan.o2o.cache.JedisUtil; import com.artisan.o2o.service.CacheService;@Service public class CacheServiceImpl implements CacheService {@AutowiredJedisUtil.Keys jedisKeys;@Overridepublic void removeFromCache(String keyPrefix) {Set<String> keySet = jedisKeys.keys(keyPrefix + "*");for (String key : keySet) {jedisKeys.del(key);}}}

工具類中的方法

/*** 查找所有匹配給定的模式的鍵* * @param String* key的表達式,*表示多個,?表示一個* */public Set<String> keys(String pattern) {Jedis jedis = getJedis();Set<String> set = jedis.keys(pattern);jedis.close();return set;}/*** 刪除keys對應的記錄,可以是多個key* * @param String* ... keys* @return 刪除的記錄數* */public long del(String... keys) {Jedis jedis = getJedis();long count = jedis.del(keys);jedis.close();return count;}

單元測試

package com.artisan.o2o.service;import java.io.IOException; 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.Area; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException;public class AreaServiceTest extends BaseTest {@AutowiredAreaService areaService;@AutowiredCacheService cacheService;@Testpublic void testGetAreaList() throws JsonParseException, JsonMappingException, IOException {// 首次從db中加載List<Area> areaList = areaService.getAreaList();for (Area area : areaList) {System.out.println("||---->" + area.toString());}// 再次查詢從redis中獲取areaList = areaService.getAreaList();for (Area area : areaList) {System.out.println("**---->" + area.toString());}// 清除緩存cacheService.removeFromCache(AreaService.AREALISTKEY);// 再次查詢 從db中獲取areaList = areaService.getAreaList();for (Area area : areaList) {System.out.println("**---->" + area.toString());}// 再次查詢從redis中獲取areaList = areaService.getAreaList();for (Area area : areaList) {System.out.println("**---->" + area.toString());}}}

觀察數據,確保測試結果符合預期。 比較簡單就不貼數據了。


Github地址

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

總結

以上是生活随笔為你收集整理的实战SSM_O2O商铺_47【Redis缓存】清除缓存接口的开发的全部內容,希望文章能夠幫你解決所遇到的問題。

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