比如现在才发布了iphone13,这人非要搜iphone100,这缓存没有,数据库也不可能有
public Map<String, List<Catelog2Vo>> getCatalogJson2() {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String catalogJson = ops.get("catalogJson");
if (StringUtils.isEmpty(catalogJson)) {
System.out.println("缓存不命中...查询数据库...");
Map<String, List<Catelog2Vo>> catalogJsonFromDb = getCatalogJsonFromDbWithRedissonLock();
if(catalogJsonFromDb.isEmpty() || catalogJsonFromDb == null){
ops.set("catalogJson","0",300,TimeUnit.SECONDS);
}
return catalogJsonFromDb;
}
System.out.println("缓存命中...直接返回...");
Map<String, List<Catelog2Vo>> result = JSON.parseObject(catalogJson,new TypeReference<Map<String, List<Catelog2Vo>>>(){});
return result;
}
具体业务代码
public Map<String, List<Catelog2Vo>> getCatalogJsonFromDbWithRedissonLock() {
RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("catalogJson-lock");
RLock rLock = readWriteLock.readLock();
Map<String, List<Catelog2Vo>> dataFromDb = null;
try {
rLock.lock();
dataFromDb = getDataFromDb();
} finally {
rLock.unlock();
}
return dataFromDb;
}
|