redisTemplate实现分页缓存,简单demo用法



//类注入对象
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate redisTemplate;


String hashKey = "_PNO" + pno + "_PSIZE" + psize;
String result=(String)redisTemplate.opsForHash().get(AniuMemcachedKey.ELEVEN_GOLD_EGGS_LIST,hashKey);

if(result != null && StringUtil.isNotNull(result))
	return result;

Map map = new HashMap<>(3);
PageImpl list = service.getGoldEggsList(pno,psize,map);
resp.setData(list);
if(resp != null && resp.getData() != null){
	redisTemplate.opsForHash().put(AniuMemcachedKey.ELEVEN_GOLD_EGGS_LIST,hashKey,JsonUtil.toJSON(resp));
	redisTemplate.expire(AniuMemcachedKey.ELEVEN_GOLD_EGGS_LIST,cacheTime, TimeUnit.SECONDS);
}


//清除缓存的时候只需要清除AniuMemcachedKey.ELEVEN_GOLD_EGGS_LIST就行了

redisTemplate.delete(AniuMemcachedKey.ELEVEN_GOLD_EGGS_LIST);

 

你可能感兴趣的:(redis相关)