private CacheResult recacheVersionKey(CacheResult cacheResult, Object[] args, CacheDefinition cacheDefinition) {
long currentVersion = cacheResult.getCurrentVersion();
String versionKey = cacheResult.getVersionKey();
boolean isReturnCollection = cacheDefinition.isReturnCollection();
Cache cache = cacheResult.getCache();
Object result = cacheResult.getRetVal();
if (currentVersion == 0) { // 原来的version已经因为时间等原因过期,需要重新缓存
Long cachedCurrentVersion = null;
// 第一次查询缓存,需要生产版本的key;
if (versionKey == null) {
if (isReturnCollection) {
versionKey = cacheDefinition.generateVersionKey(args);
} else if (result != null && StringUtils.isNotEmpty(cacheDefinition.getVkey())) {
versionKey = cacheDefinition.generateVersionKey(new Object[] { result });
} else {
LOGGER.info("不需要缓存版本信息");
return cacheResult;
}
if (versionKey == null) {
LOGGER.info("无法生成更新版本信息的缓存KEY,缓存失败");
return cacheResult;
}
LOGGER.debug("第一次查询缓存,需要生成版本信息的缓存KEY:" + versionKey);
cachedCurrentVersion = (Long) cache.get(versionKey); // 由于第一次查询无法得知版本信息,所以在知道版本key后才需要再查一次版本信息
}
// 为空或者无意义才表明版本信息也需要缓存,否则版本信息就为缓存的版本信息
if (versionKey != null && (cachedCurrentVersion == null || cachedCurrentVersion <= 0)) {
currentVersion = System.currentTimeMillis(); // 改用系统时间作为版本号,防止重构后的版本号与对象的版本正好一致,而是对象无法失效
long expire = cacheDefinition.getExpire();
cache.add(versionKey, currentVersion, expire);
LOGGER.debug("更新版本信息的缓存KEY:" + versionKey + "到版本" + currentVersion + ", 缓存时间:" + expire + "毫秒");
cacheResult.setCurrentVersion(currentVersion);
} else {
if (cachedCurrentVersion == null || cachedCurrentVersion <= 0) {
cachedCurrentVersion = System.currentTimeMillis(); // 改用系统时间作为版本号,防止重构后的版本号与对象的版本正好一致,而是对象无法失效