javaWeb中使用Redis緩存實例解析
直接進入主題:
一: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ù)據(jù),下次取的時候,因為緩存中有數(shù)據(jù)便在緩存中取,這是數(shù)據(jù)庫的數(shù)據(jù)與緩存中的數(shù)據(jù)不一致,便出現(xiàn)差異,這就要緩存同步了。
其實很簡單,就是在修改,刪除(如果添加也需要的話),執(zhí)行下面操作:
1:刪除緩存,處理數(shù)據(jù),把數(shù)據(jù)放如緩存
2:刪除緩存,處理數(shù)據(jù)(等查詢數(shù)據(jù)的時候會把數(shù)據(jù)放入緩存,兩種情況只是寫緩存時間的區(qū)別)
try {
jedisClientSingleService.hdel(ALL_RESOURCES_NO_CONDITION, hashId);
} catch (Exception e) {
e.printStackTrace();
}
總結(jié)
以上就是本文關(guān)于javaWeb中使用Redis緩存實例解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Idea如何關(guān)閉或開啟引用提示Usages和Annotations
這篇文章主要介紹了Idea如何關(guān)閉或開啟引用提示Usages和Annotations問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
springboot如何配置嵌套map和list參數(shù)
這篇文章主要介紹了springboot如何配置嵌套map和list參數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
nacos中的配置使用@Value注解獲取不到值的原因及解決方案
這篇文章主要介紹了nacos中的配置使用@Value注解獲取不到值的原因分析,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03

