Skip to content

Instantly share code, notes, and snippets.

@kostapc
Created October 28, 2016 13:00
Show Gist options
  • Save kostapc/727489139e876b7ca391483e3a18a5f3 to your computer and use it in GitHub Desktop.
Save kostapc/727489139e876b7ca391483e3a18a5f3 to your computer and use it in GitHub Desktop.
CacheEntity<K,V> execute(CacheEntity<K,V> cacheEntity, Action action) {
long now = System.currentTimeMillis();
if(cacheEntity==null) {
return null;
}
if(cacheEntity.isExpired()) {
storage.remove(cacheEntity);
return null;
}
Duration duration;
switch (action) {
case CHECK:
return cacheEntity;
/*
* This method is called by a caching implementation after a Cache.Entry is
* created, but before a Cache.Entry is added to a cache
*/
case CREATE:
duration = policy.getExpiryForCreation();
break;
/*
* This method is called by a caching implementation after a Cache.Entry is
* accessed to determine the {@link Duration} before an entry expires.
*/
case ACCESS:
duration = policy.getExpiryForAccess();
break;
/*
* This method is called by the caching implementation after a Cache.Entry is
* updated to determine the {@link Duration} before the updated entry expires.
*/
case UPDATE:
duration = policy.getExpiryForUpdate();
break;
default:
return cacheEntity;
}
if(duration==null) {
return cacheEntity;
}
if(duration.isZero()) {
storage.remove(cacheEntity);
return null;
}
long expireTime = duration.getAdjustedTime(now);
cacheEntity.setExpireTimestamp(expireTime);
if(cacheEntity.isExpired()) {
storage.remove(cacheEntity);
return null;
}
return cacheEntity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment