javaWeb中使用Redis緩存實(shí)例解析
直接進(jìn)入主題:
一:serviceImpl定義:
@Service
public class JedisClientSingleService implements JedisClient {
@Autowired
private JedisPool jedisPool;
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return string;
}
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return string;
}
@Override
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return string;
}
@Override
public long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}
@Override
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public long expire(String key, int second) {
Jedis jedis = jedisPool.getResource();
long result = jedis.expire(key, second);
jedis.close();
return result;
}
@Override
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
long result = jedis.ttl(key);
jedis.close();
return result;
}
@Override
public long del(String key) {
Jedis jedis = jedisPool.getResource();
long result = jedis.del(key);
jedis.close();
return result;
}
@Override
public long hdel(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
long result = jedis.hdel(hkey, key);
jedis.close();
return result;
}
二:添加緩存出(一般寫在service是層中):
public List<RoleResource> getTreeGrid() {
//從緩存中獲取內(nèi)容
try {
String cachString = jedisClientSingleService.hget(ALL_RESOURCES_NO_CONDITION, hashId);
if(!StringUtils.isBlank(cachString)){
List<RoleResource> list = JsonUtils.jsonStrToList(cachString, RoleResource.class) ;
return list ;
}
} catch (Exception e) {
e.printStackTrace();
}
List<RoleResource> list =sessionFactory.openSession().selectList("cn.sys.auth.entity.ResourcesMapper.getTreeGrid");
//將緩存中添加緩存
try {
//redsi只存字符串,把list轉(zhuǎn)換換成字符串
String cachString =JsonUtils.toJson(list) ;
jedisClientSingleService.hset(ALL_RESOURCES_NO_CONDITION, hashId, cachString) ;
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
邏輯:先從緩存中取數(shù)據(jù),如果緩存中沒有,就去數(shù)據(jù)庫中取,然后把數(shù)據(jù)存入緩存,下次查詢時(shí)就會(huì)從緩存中取。
三:緩存的同步
問題來了,入過你修改或者刪除了數(shù)據(jù),下次取的時(shí)候,因?yàn)榫彺嬷杏袛?shù)據(jù)便在緩存中取,這是數(shù)據(jù)庫的數(shù)據(jù)與緩存中的數(shù)據(jù)不一致,便出現(xiàn)差異,這就要緩存同步了。
其實(shí)很簡單,就是在修改,刪除(如果添加也需要的話),執(zhí)行下面操作:
1:刪除緩存,處理數(shù)據(jù),把數(shù)據(jù)放如緩存
2:刪除緩存,處理數(shù)據(jù)(等查詢數(shù)據(jù)的時(shí)候會(huì)把數(shù)據(jù)放入緩存,兩種情況只是寫緩存時(shí)間的區(qū)別)
try {
jedisClientSingleService.hdel(ALL_RESOURCES_NO_CONDITION, hashId);
} catch (Exception e) {
e.printStackTrace();
}
總結(jié)
以上就是本文關(guān)于javaWeb中使用Redis緩存實(shí)例解析的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
SpringIOC refresh()初始化代碼實(shí)例
這篇文章主要介紹了SpringIOC refresh()初始化代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java多線程局域網(wǎng)聊天室的實(shí)現(xiàn)
在學(xué)習(xí)了一個(gè)學(xué)期的java以后,搞了一個(gè)多線程的聊天室,熟悉了一下服務(wù)器和客戶機(jī)的操作。感興趣的小伙伴們可以參考一下2021-06-06
Idea如何關(guān)閉或開啟引用提示Usages和Annotations
這篇文章主要介紹了Idea如何關(guān)閉或開啟引用提示Usages和Annotations問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
springboot如何配置嵌套map和list參數(shù)
這篇文章主要介紹了springboot如何配置嵌套map和list參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
SpringBoot如何獲取Get請(qǐng)求參數(shù)詳解
SpringBoot為我們封裝了許多簡便的獲取請(qǐng)求參數(shù)的方法,下面這篇文章主要給大家介紹了關(guān)于SpringBoot如何獲取Get請(qǐng)求參數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
nacos中的配置使用@Value注解獲取不到值的原因及解決方案
這篇文章主要介紹了nacos中的配置使用@Value注解獲取不到值的原因分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03

