using ehcache to cache the plugin results

This commit is contained in:
Sebastian Sdorra
2011-05-27 10:13:31 +02:00
parent 85ecf7ceed
commit db58c82ac2
3 changed files with 213 additions and 6 deletions

View File

@@ -41,6 +41,12 @@
<artifactId>logback-classic</artifactId>
<version>0.9.28</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcache.version}</version>
</dependency>
</dependencies>

View File

@@ -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<PluginInformation> 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<PluginInformation> 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<PluginInformation> 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;
}

View File

@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2010, Sebastian Sdorra
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of SCM-Manager; nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
http://bitbucket.org/sdorra/scm-manager
-->
<!--
Document : ehcache.xml
Created on : October 14, 2010, 6:54 AM
Author : sdorra
Description:
Purpose of the document follows.
-->
<ehcache xmlns="http://ehcache.org/ehcache.xsd">
<!--
Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by
its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path
-->
<diskStore path="java.io.tmpdir"/>
<!--
Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required:
maxElementsInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the
element is never expired.
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
The following attributes are optional:
timeToIdleSeconds - Sets the time to idle for an element before it expires.
i.e. The maximum amount of time between accesses before an element expires
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that an Element can idle for infinity.
The default value is 0.
timeToLiveSeconds - Sets the time to live for an element before it expires.
i.e. The maximum time between creation time and when an element expires.
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that and Element can live for infinity.
The default value is 0.
diskPersistent - Whether the disk store persists between restarts of the Virtual Machine.
The default value is false.
diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value
is 120 seconds.
-->
<defaultCache
maxElementsInMemory="20000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="1200"
timeToLiveSeconds="2400"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="2400"
/>
<cache
name="sonia.cache.plugin-backend"
maxElementsInMemory="20000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600"
diskPersistent="false"
/>
</ehcache>