Created
September 5, 2012 07:36
-
-
Save nekman/3632694 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package se.labs.cache; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| /** | |
| * | |
| * @author ne | |
| * | |
| */ | |
| public class ItemCacheImpl<T> implements ItemCache<T> { | |
| /** | |
| * Logger | |
| */ | |
| private static final Logger logger = LoggerFactory.getLogger(ItemCacheImpl.class); | |
| /** | |
| * When the cache was last updated | |
| */ | |
| private Long lastUpdated; | |
| /** | |
| * Time in ms between each reload | |
| */ | |
| private Long timeBetweenReload; | |
| private T item; | |
| private ItemCacheLoader<T> cacheLoader; | |
| /** | |
| * | |
| * @param cacheLoader - the cache loader | |
| * @param secondsBetweenReload - the seconds between each reload of the cache | |
| * | |
| */ | |
| public ItemCacheImpl(final ItemCacheLoader<T> cacheLoader, final int secondsBetweenReload) { | |
| if (cacheLoader == null) { | |
| throw new IllegalArgumentException("cacheloader"); | |
| } | |
| this.cacheLoader = cacheLoader; | |
| this.timeBetweenReload = secondsBetweenReload * 1000L; | |
| } | |
| /** | |
| * | |
| * @param cacheLoader - the cache loader | |
| */ | |
| public ItemCacheImpl(final ItemCacheLoader<T> cacheLoader) { | |
| this(cacheLoader, 600); | |
| } | |
| /** | |
| * @see ItemCache#getItem() | |
| */ | |
| @Override | |
| public T getItem() { | |
| // Check if a reload of the cache is needed, if so, | |
| // get the item from the cacheloader and update the time stamp. | |
| if (isReloadNeeded()) { | |
| synchronized(this) { | |
| if (isReloadNeeded()) { | |
| item = cacheLoader.reload(); | |
| lastUpdated = System.currentTimeMillis(); | |
| } | |
| } | |
| logger.info("Cache was reloaded."); | |
| } | |
| return item; | |
| } | |
| /** | |
| * @see ItemCache#isReloadNeeded() | |
| */ | |
| @Override | |
| public boolean isReloadNeeded() { | |
| if (item == null) { | |
| return true; | |
| } | |
| return (System.currentTimeMillis() - lastUpdated) > timeBetweenReload; | |
| } | |
| @Override | |
| public void invalidate() { | |
| item = null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment