From: David ‘Bombe’ Roden Date: Tue, 12 Jul 2011 04:57:49 +0000 (+0200) Subject: Add short-term cache for search results. X-Git-Tag: 0.6.6^2~18 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;ds=inline;h=4a079c5610c1e8eac406fdfb7077525ef5b5fe2d;p=Sone.git Add short-term cache for search results. This resolves #211. --- diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index c1b7c3c..fab7a57 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -32,9 +32,16 @@ import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; +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.Mapper; import net.pterodactylus.util.collection.Mappers; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.collection.TimedMap; import net.pterodactylus.util.filter.Filter; import net.pterodactylus.util.filter.Filters; import net.pterodactylus.util.logging.Logging; @@ -55,6 +62,20 @@ public class SearchPage extends SoneTemplatePage { /** The logger. */ private static final Logger logger = Logging.getLogger(SearchPage.class); + /** Short-term cache. */ + private final Cache, Set>> hitCache = new MemoryCache, Set>>(new ValueRetriever, Set>>() { + + @SuppressWarnings("synthetic-access") + public CacheItem>> retrieve(List phrases) throws CacheException { + Set posts = new HashSet(); + for (Sone sone : webInterface.getCore().getSones()) { + posts.addAll(sone.getPosts()); + } + return new DefaultCacheItem>>(getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator())); + } + + }, new TimedMap, CacheItem>>>(300000)); + /** * Creates a new search page. * @@ -90,12 +111,14 @@ public class SearchPage extends SoneTemplatePage { Set sones = webInterface.getCore().getSones(); Set> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR); - Set posts = new HashSet(); - for (Sone sone : sones) { - posts.addAll(sone.getPosts()); + Set> 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(); } - @SuppressWarnings("synthetic-access") - Set> postHits = getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()); /* now filter. */ soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER);