From de97f9a91b5406c437deccc5da45236524b0cb9e Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Tue, 16 Aug 2011 18:37:28 +0200 Subject: [PATCH] cache result of changesetpreprocessors --- .../sonia/scm/repository/CacheClearHook.java | 15 ++++ .../scm/repository/ChangesetViewerUtil.java | 79 +++++++++++++++++- .../rest/resources/RepositoryResource.java | 80 ------------------- 3 files changed, 92 insertions(+), 82 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java b/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java index a870dbb4d0..7587502c00 100644 --- a/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java +++ b/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java @@ -59,6 +59,21 @@ public class CacheClearHook implements RepositoryHook //~--- methods -------------------------------------------------------------- + /** + * Method description + * + * @since 1.7 + */ + public void clearCache() + { + if (logger.isDebugEnabled()) + { + logger.debug("clear cache"); + } + + cache.clear(); + } + /** * Method description * diff --git a/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java b/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java index 8bc2587eae..9b93fe9d08 100644 --- a/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java +++ b/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java @@ -44,6 +44,11 @@ import sonia.scm.NotSupportedFeatuerException; import sonia.scm.cache.Cache; import sonia.scm.cache.CacheManager; import sonia.scm.util.AssertUtil; +import sonia.scm.util.Util; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Set; /** * @@ -68,12 +73,18 @@ public class ChangesetViewerUtil extends CacheClearHook * * @param repositoryManager * @param cacheManager + * @param changesetPreProcessorSet + * @param changesetPreProcessorFactorySet */ @Inject - public ChangesetViewerUtil(RepositoryManager repositoryManager, - CacheManager cacheManager) + public ChangesetViewerUtil( + RepositoryManager repositoryManager, CacheManager cacheManager, + Set changesetPreProcessorSet, + Set changesetPreProcessorFactorySet) { this.repositoryManager = repositoryManager; + this.changesetPreProcessorSet = changesetPreProcessorSet; + this.changesetPreProcessorFactorySet = changesetPreProcessorFactorySet; cache = cacheManager.getCache(ChangesetViewerCacheKey.class, ChangesetPagingResult.class, CACHE_NAME); init(repositoryManager, cache); @@ -146,6 +157,13 @@ public class ChangesetViewerUtil extends CacheClearHook if (result == null) { result = viewer.getChangesets(start, max); + + if (Util.isNotEmpty(result.getChangesets())) + { + callPreProcessors(result); + callPreProcessorFactories(repository, result); + } + cache.put(key, result); } else if (logger.isDebugEnabled()) @@ -156,6 +174,57 @@ public class ChangesetViewerUtil extends CacheClearHook return result; } + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * + * @param repository + * @param changesets + */ + private void callPreProcessorFactories(Repository repository, + ChangesetPagingResult changesets) + { + if (Util.isNotEmpty(changesetPreProcessorFactorySet)) + { + for (ChangesetPreProcessorFactory factory : + changesetPreProcessorFactorySet) + { + ChangesetPreProcessor cpp = factory.createPreProcessor(repository); + + if (cpp != null) + { + for (Changeset c : changesets.getChangesets()) + { + cpp.process(c); + } + } + } + } + } + + /** + * Method description + * + * + * @param changesets + */ + private void callPreProcessors(ChangesetPagingResult changesets) + { + if (Util.isNotEmpty(changesetPreProcessorSet)) + { + for (Changeset c : changesets.getChangesets()) + { + for (ChangesetPreProcessor cpp : changesetPreProcessorSet) + { + cpp.process(c); + } + } + } + } + //~--- inner classes -------------------------------------------------------- /** @@ -266,6 +335,12 @@ public class ChangesetViewerUtil extends CacheClearHook /** Field description */ private Cache cache; + /** Field description */ + private Set changesetPreProcessorFactorySet; + + /** Field description */ + private Set changesetPreProcessorSet; + /** Field description */ private RepositoryManager repositoryManager; } diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java index 95873dbd2d..2ca8c45e09 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java @@ -45,10 +45,7 @@ import org.slf4j.LoggerFactory; import sonia.scm.NotSupportedFeatuerException; import sonia.scm.config.ScmConfiguration; import sonia.scm.repository.BrowserResult; -import sonia.scm.repository.Changeset; import sonia.scm.repository.ChangesetPagingResult; -import sonia.scm.repository.ChangesetPreProcessor; -import sonia.scm.repository.ChangesetPreProcessorFactory; import sonia.scm.repository.ChangesetViewerUtil; import sonia.scm.repository.PathNotFoundException; import sonia.scm.repository.Permission; @@ -63,7 +60,6 @@ import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryNotFoundException; import sonia.scm.repository.RevisionNotFoundException; import sonia.scm.util.HttpUtil; -import sonia.scm.util.Util; import sonia.scm.web.security.WebSecurityContext; //~--- JDK imports ------------------------------------------------------------ @@ -73,7 +69,6 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; -import java.util.Set; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; @@ -113,8 +108,6 @@ public class RepositoryResource * @param configuration * @param repositoryManager * @param securityContextProvider - * @param changesetPreProcessorSet - * @param changesetPreProcessorFactorySet * @param changesetViewerUtil * @param repositoryBrowserUtil */ @@ -122,8 +115,6 @@ public class RepositoryResource public RepositoryResource( ScmConfiguration configuration, RepositoryManager repositoryManager, Provider securityContextProvider, - Set changesetPreProcessorSet, - Set changesetPreProcessorFactorySet, ChangesetViewerUtil changesetViewerUtil, RepositoryBrowserUtil repositoryBrowserUtil) { @@ -131,8 +122,6 @@ public class RepositoryResource this.configuration = configuration; this.repositoryManager = repositoryManager; this.securityContextProvider = securityContextProvider; - this.changesetPreProcessorSet = changesetPreProcessorSet; - this.changesetPreProcessorFactorySet = changesetPreProcessorFactorySet; this.changesetViewerUtil = changesetViewerUtil; this.repositoryBrowserUtil = repositoryBrowserUtil; setDisableCache(false); @@ -217,12 +206,6 @@ public class RepositoryResource if (changesets != null) { - if (Util.isNotEmpty(changesets.getChangesets())) - { - callPreProcessors(changesets); - callPreProcessorFactories(id, changesets); - } - response = Response.ok(changesets).build(); } else @@ -394,63 +377,6 @@ public class RepositoryResource } } - /** - * Method description - * - * - * @param id - * @param changesets - */ - private void callPreProcessorFactories(String id, - ChangesetPagingResult changesets) - { - if (Util.isNotEmpty(changesetPreProcessorFactorySet)) - { - Repository repository = repositoryManager.get(id); - - if (repository != null) - { - for (ChangesetPreProcessorFactory factory : - changesetPreProcessorFactorySet) - { - ChangesetPreProcessor cpp = factory.createPreProcessor(repository); - - if (cpp != null) - { - for (Changeset c : changesets.getChangesets()) - { - cpp.process(c); - } - } - } - } - else if (logger.isWarnEnabled()) - { - logger.warn("could not find repository {}", id); - } - } - } - - /** - * Method description - * - * - * @param changesets - */ - private void callPreProcessors(ChangesetPagingResult changesets) - { - if (Util.isNotEmpty(changesetPreProcessorSet)) - { - for (Changeset c : changesets.getChangesets()) - { - for (ChangesetPreProcessor cpp : changesetPreProcessorSet) - { - cpp.process(c); - } - } - } - } - /** * Method description * @@ -577,12 +503,6 @@ public class RepositoryResource //~--- fields --------------------------------------------------------------- - /** Field description */ - private Set changesetPreProcessorFactorySet; - - /** Field description */ - private Set changesetPreProcessorSet; - /** Field description */ private ChangesetViewerUtil changesetViewerUtil;