From 1b078dab3e386026650fe795518db303d710cc00 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 16 Jan 2013 14:49:12 +0100 Subject: [PATCH] =?utf8?q?Replace=20utils=E2=80=99=20Cache=20with=20Guava?= =?utf8?q?=E2=80=99s=20LoadingCache.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/SearchPage.java | 29 +++++++--------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index 002e4f2..3024dfa 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -24,6 +24,7 @@ import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -34,14 +35,7 @@ import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.cache.Cache; -import net.pterodactylus.util.cache.CacheException; -import net.pterodactylus.util.cache.CacheItem; -import net.pterodactylus.util.cache.DefaultCacheItem; -import net.pterodactylus.util.cache.MemoryCache; -import net.pterodactylus.util.cache.ValueRetriever; import net.pterodactylus.util.collection.Pagination; -import net.pterodactylus.util.collection.TimedMap; import net.pterodactylus.util.collection.mapper.Mapper; import net.pterodactylus.util.collection.mapper.Mappers; import net.pterodactylus.util.logging.Logging; @@ -52,6 +46,9 @@ import net.pterodactylus.util.text.StringEscaper; import net.pterodactylus.util.text.TextException; import com.google.common.base.Predicate; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.google.common.collect.Collections2; /** @@ -66,19 +63,18 @@ public class SearchPage extends SoneTemplatePage { private static final Logger logger = Logging.getLogger(SearchPage.class); /** Short-term cache. */ - private final Cache, Set>> hitCache = new MemoryCache, Set>>(new ValueRetriever, Set>>() { + private final LoadingCache, Set>> hitCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build(new CacheLoader, Set>>() { @Override @SuppressWarnings("synthetic-access") - public CacheItem>> retrieve(List phrases) throws CacheException { + public Set> load(List phrases) { Set posts = new HashSet(); for (Sone sone : webInterface.getCore().getSones()) { posts.addAll(sone.getPosts()); } - return new DefaultCacheItem>>(getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator())); + return getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()); } - - }, new TimedMap, CacheItem>>>(300000)); + }); /** * Creates a new search page. @@ -135,14 +131,7 @@ public class SearchPage extends SoneTemplatePage { Set sones = webInterface.getCore().getSones(); Collection> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR); - Collection> postHits; - try { - postHits = hitCache.get(phrases); - } catch (CacheException ce1) { - /* should never happen. */ - logger.log(Level.SEVERE, "Could not get search results from cache!", ce1); - postHits = Collections.emptySet(); - } + Collection> postHits = hitCache.getUnchecked(phrases); /* now filter. */ soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER); -- 2.7.4