From db58c82ac2aceb7e5431daed18b64d25e37413e9 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 27 May 2011 10:13:31 +0200 Subject: [PATCH] using ehcache to cache the plugin results --- scm-plugin-backend/pom.xml | 6 + .../sonia/scm/plugin/rest/PluginResource.java | 103 +++++++++++++++- .../src/main/resources/config/ehcache.xml | 110 ++++++++++++++++++ 3 files changed, 213 insertions(+), 6 deletions(-) create mode 100644 scm-plugin-backend/src/main/resources/config/ehcache.xml diff --git a/scm-plugin-backend/pom.xml b/scm-plugin-backend/pom.xml index 81ebafd4da..2653b35373 100644 --- a/scm-plugin-backend/pom.xml +++ b/scm-plugin-backend/pom.xml @@ -41,6 +41,12 @@ logback-classic 0.9.28 + + + net.sf.ehcache + ehcache-core + ${ehcache.version} + diff --git a/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/PluginResource.java b/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/PluginResource.java index 5aa9ce3e05..7bb29f293c 100644 --- a/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/PluginResource.java +++ b/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/PluginResource.java @@ -38,17 +38,23 @@ package sonia.scm.plugin.rest; import com.google.inject.Inject; import com.google.inject.Singleton; +import net.sf.ehcache.Cache; +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Element; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.plugin.BackendConfiguration; import sonia.scm.plugin.PluginBackend; +import sonia.scm.plugin.PluginBackendListener; import sonia.scm.plugin.PluginCenter; import sonia.scm.plugin.PluginInformation; import sonia.scm.plugin.PluginVersion; //~--- JDK imports ------------------------------------------------------------ +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -69,9 +75,15 @@ import javax.ws.rs.core.Response; */ @Singleton @Path("{version}/plugins") -public class PluginResource +public class PluginResource implements PluginBackendListener { + /** Field description */ + public static final String CACHE = "sonia.cache.plugin-backend"; + + /** Field description */ + public static final String CONFIG = "/config/ehcache.xml"; + /** the logger for PluginResource */ private static final Logger logger = LoggerFactory.getLogger(PluginResource.class); @@ -91,6 +103,33 @@ public class PluginResource { this.backend = backend; this.configuration = configuration; + + // added listener to clear the cache on a event + this.backend.addListener(this); + + CacheManager cacheManager = + new CacheManager(PluginResource.class.getResource(CONFIG)); + + cache = cacheManager.getCache(CACHE); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param plugins + */ + @Override + public void addedNewPlugins(Collection plugins) + { + if (logger.isDebugEnabled()) + { + logger.debug("clear cache {}", CACHE); + } + + cache.removeAll(); } //~--- get methods ---------------------------------------------------------- @@ -120,16 +159,65 @@ public class PluginResource version, Boolean.toString(snapshot)); } - List plugins = - backend.getPlugins(new DefaultPluginFilter(version, os, arch, snapshot)); - PluginCenter pc = new PluginCenter(); + PluginCenter pc = null; + String key = createCacheKey(version, os, arch, snapshot); + Element el = cache.get(key); - pc.setPlugins(getNewestPlugins(plugins)); - pc.setRepositories(configuration.getRepositories()); + if (el != null) + { + if (logger.isDebugEnabled()) + { + logger.debug("load plugin center from cache"); + } + + pc = (PluginCenter) el.getObjectValue(); + } + else + { + if (logger.isDebugEnabled()) + { + logger.debug("load plugin center from backend"); + } + + List plugins = + backend.getPlugins(new DefaultPluginFilter(version, os, arch, + snapshot)); + + pc = new PluginCenter(); + pc.setPlugins(getNewestPlugins(plugins)); + pc.setRepositories(configuration.getRepositories()); + cache.put(new Element(key, pc)); + } return Response.ok(pc).build(); } + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param version + * @param os + * @param arch + * @param snapshot + * + * @return + */ + private String createCacheKey(String version, String os, String arch, + boolean snapshot) + { + StringBuilder key = new StringBuilder(version); + + key.append(":").append(os).append(":").append(arch).append(":"); + key.append(Boolean.toString(snapshot)); + + return key.toString(); + } + + //~--- get methods ---------------------------------------------------------- + /** * Method description * @@ -206,6 +294,9 @@ public class PluginResource /** Field description */ private PluginBackend backend; + /** Field description */ + private Cache cache; + /** Field description */ private BackendConfiguration configuration; } diff --git a/scm-plugin-backend/src/main/resources/config/ehcache.xml b/scm-plugin-backend/src/main/resources/config/ehcache.xml new file mode 100644 index 0000000000..ecf0f5323f --- /dev/null +++ b/scm-plugin-backend/src/main/resources/config/ehcache.xml @@ -0,0 +1,110 @@ + + + + + + + + + + + + + + + + + + + +