* make caching more resilient against class-version upgrades (just fail silently, don't break things)

This commit is contained in:
Reinhard Pointner 2012-02-17 02:44:46 +00:00
parent 09621b6b31
commit 137640f244
2 changed files with 22 additions and 14 deletions

View File

@ -104,7 +104,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
try {
cache.put(new Element(new Key(id, normalize(query), locale), value.toArray(new SearchResult[0])));
} catch (Exception e) {
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e);
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage());
}
return value;
@ -129,7 +129,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
try {
cache.put(new Element(new Key(id, key, sortOrder, locale), episodes.toArray(new Episode[0])));
} catch (Exception e) {
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e);
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage());
}
return episodes;
@ -154,7 +154,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
try {
cache.put(new Element(new Key(id, category, locale, key), object));
} catch (Exception e) {
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e);
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage());
}
}

View File

@ -40,17 +40,21 @@ public abstract class CachedResource<T extends Serializable> {
public synchronized T get() throws IOException {
String cacheKey = type.getName() + ":" + resource.toString();
Element element = null;
long lastUpdateTime = 0;
Element element = cache.get(cacheKey);
long lastUpdateTime = (element != null) ? element.getLatestOfCreationAndUpdateTime() : 0;
try {
element = cache.get(cacheKey);
if (element != null) {
lastUpdateTime = element.getLatestOfCreationAndUpdateTime();
}
// fetch from cache
if (element != null && System.currentTimeMillis() - lastUpdateTime < expirationTime) {
try {
return type.cast(element.getValue());
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage(), e);
}
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage());
}
// fetch and process resource
@ -64,9 +68,13 @@ public abstract class CachedResource<T extends Serializable> {
}
}
// update cached data and last-updated time
try {
cache.put(element);
return type.cast(element.getValue());
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage());
}
// update cached data and last-updated time
return type.cast(element.getValue());
}
}