redis 杂记

1、存取对象方法

@Override
public QueryResult<CmthpbankDto> queryCacheBankList(CmthpbankQuery query) {
	QueryResult<CmthpbankDto> result = new QueryResult<CmthpbankDto>(false, ErrorCodeConstant.ERROR_UNKNOWN,
			"error", null);
	try {
		List<CmthpbankDto> listDto = JSON.parseArray(jedisCluster.get(BANK_NAME), CmthpbankDto.class);
		if (listDto == null || listDto.size() == 0) {
			listDto = cmthpbankDao.queryCmthpbankList(query);
			if (!StringUtils.isEmpty(listDto)) {
				jedisCluster.set(BANK_NAME, JSON.toJSONString((listDto))); // 去数据库取
				jedisCluster.expire(BANK_NAME, RedisConstant.EXPIRE_TIME_MONTH);
			}
		}
		result.setSuccess(true);
		result.setResults(listDto);
	} catch (Exception e) {
		e.printStackTrace();
		result.setSuccess(false);
	}
	return result;
}

 注释:

    BANK_NAME:存入取出的key

    RedisConstant.EXPIRE_TIME_MONTH:缓存有效时间

相关推荐