From 137640f244ed4d0b08f1dba139af7398616627e0 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 17 Feb 2012 02:44:46 +0000 Subject: [PATCH] * make caching more resilient against class-version upgrades (just fail silently, don't break things) --- .../web/AbstractEpisodeListProvider.java | 6 ++-- .../filebot/web/CachedResource.java | 30 ++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java b/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java index 1706e11b..531354de 100644 --- a/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java +++ b/source/net/sourceforge/filebot/web/AbstractEpisodeListProvider.java @@ -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()); } } diff --git a/source/net/sourceforge/filebot/web/CachedResource.java b/source/net/sourceforge/filebot/web/CachedResource.java index 909322c2..d8611ecd 100644 --- a/source/net/sourceforge/filebot/web/CachedResource.java +++ b/source/net/sourceforge/filebot/web/CachedResource.java @@ -40,17 +40,21 @@ public abstract class CachedResource { 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; - - // 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); + try { + element = cache.get(cacheKey); + if (element != null) { + lastUpdateTime = element.getLatestOfCreationAndUpdateTime(); } + + // fetch from cache + if (element != null && System.currentTimeMillis() - lastUpdateTime < expirationTime) { + return type.cast(element.getValue()); + } + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage()); } // fetch and process resource @@ -64,9 +68,13 @@ public abstract class CachedResource { } } + try { + cache.put(element); + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage()); + } + // update cached data and last-updated time - cache.put(element); return type.cast(element.getValue()); } - }