From 3c8a999b0ad3dc0a4df59ba7a40155b3adb66888 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 16 Jun 2012 17:58:27 +0200 Subject: [PATCH] clear caches after received changes --- .../repository/api/BlameCommandBuilder.java | 17 ++- .../repository/api/BrowseCommandBuilder.java | 17 ++- .../scm/repository/api/LogCommandBuilder.java | 17 ++- .../api/RepositoryServiceFactory.java | 113 ++++++++++++++++++ 4 files changed, 158 insertions(+), 6 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java index 9376bc9e24..868cd2e6b4 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java @@ -66,7 +66,7 @@ public final class BlameCommandBuilder { /** name of the cache */ - private static final String CACHE_NAME = "sonia.scm.cache.blame"; + static final String CACHE_NAME = "sonia.scm.cache.blame"; /** * the logger for BlameCommandBuilder @@ -213,7 +213,7 @@ public final class BlameCommandBuilder * @version Enter version here..., 12/06/05 * @author Enter your name here... */ - private static class CacheKey implements Serializable + static class CacheKey implements Serializable { /** @@ -270,6 +270,19 @@ public final class BlameCommandBuilder return Objects.hashCode(repositoryId, request); } + //~--- get methods -------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + String getRepositoryId() + { + return repositoryId; + } + //~--- fields ------------------------------------------------------------- /** Field description */ diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java index c46b9a9b8e..a4df503d27 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java @@ -65,7 +65,7 @@ public final class BrowseCommandBuilder { /** Name of the cache */ - private static final String CACHE_NAME = "sonia.scm.cache.browse"; + static final String CACHE_NAME = "sonia.scm.cache.browse"; /** * the logger for BrowseCommandBuilder @@ -243,7 +243,7 @@ public final class BrowseCommandBuilder * @version Enter version here..., 12/06/05 * @author Enter your name here... */ - private static class CacheKey implements Serializable + static class CacheKey implements Serializable { /** @@ -300,6 +300,19 @@ public final class BrowseCommandBuilder return Objects.hashCode(repositoryId, request); } + //~--- get methods -------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + String getRepositoryId() + { + return repositoryId; + } + //~--- fields ------------------------------------------------------------- /** Field description */ diff --git a/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java index ad0ef37d11..a04570002b 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java @@ -105,7 +105,7 @@ public final class LogCommandBuilder { /** name of the cache */ - private static final String CACHE_NAME = "sonia.scm.cache.log"; + static final String CACHE_NAME = "sonia.scm.cache.log"; /** * the logger for LogCommandBuilder @@ -423,7 +423,7 @@ public final class LogCommandBuilder * @version Enter version here..., 12/06/05 * @author Enter your name here... */ - private static class CacheKey implements Serializable + static class CacheKey implements Serializable { /** @@ -495,6 +495,19 @@ public final class LogCommandBuilder return Objects.hashCode(repositoryId, changesetId, request); } + //~--- get methods -------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + String getRepositoryId() + { + return repositoryId; + } + //~--- fields ------------------------------------------------------------- /** Field description */ diff --git a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java index fd5904b1ec..7e5bdf2f86 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java @@ -44,11 +44,18 @@ import com.google.inject.Singleton; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sonia.scm.Filter; +import sonia.scm.cache.Cache; import sonia.scm.cache.CacheManager; +import sonia.scm.repository.BlameResult; +import sonia.scm.repository.BrowserResult; +import sonia.scm.repository.ChangesetPagingResult; import sonia.scm.repository.PermissionType; import sonia.scm.repository.PermissionUtil; +import sonia.scm.repository.PostReceiveRepositoryHook; import sonia.scm.repository.PreProcessorUtil; import sonia.scm.repository.Repository; +import sonia.scm.repository.RepositoryHookEvent; import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryNotFoundException; import sonia.scm.repository.spi.RepositoryServiceProvider; @@ -124,6 +131,7 @@ public final class RepositoryServiceFactory this.securityContextProvider = securityContextProvider; this.resolvers = resolvers; this.preProcessorUtil = preProcessorUtil; + repositoryManager.addHook(new CacheClearHook(cacheManager)); } //~--- methods -------------------------------------------------------------- @@ -256,6 +264,111 @@ public final class RepositoryServiceFactory return service; } + //~--- inner classes -------------------------------------------------------- + + /** + * TODO find a more elegant way + * + * + * @version Enter version here..., 12/06/16 + * @author Enter your name here... + */ + private static class CacheClearHook extends PostReceiveRepositoryHook + { + + /** + * Constructs ... + * + * + * @param cacheManager + */ + public CacheClearHook(CacheManager cacheManager) + { + this.blameCache = + cacheManager.getCache(BlameCommandBuilder.CacheKey.class, + BlameResult.class, + BlameCommandBuilder.CACHE_NAME); + this.browseCache = + cacheManager.getCache(BrowseCommandBuilder.CacheKey.class, + BrowserResult.class, + BrowseCommandBuilder.CACHE_NAME); + this.logCache = cacheManager.getCache(LogCommandBuilder.CacheKey.class, + ChangesetPagingResult.class, LogCommandBuilder.CACHE_NAME); + } + + //~--- methods ------------------------------------------------------------ + + /** + * Method description + * + * + * @param event + */ + @Override + public void onEvent(RepositoryHookEvent event) + { + Repository repository = event.getRepository(); + + if (repository != null) + { + String id = repository.getId(); + + clearCaches(id); + } + } + + /** + * Method description + * + * + * @param repositoryId + */ + private void clearCaches(final String repositoryId) + { + if (logger.isDebugEnabled()) + { + logger.debug("clear caches for repository id {}", repositoryId); + } + + blameCache.removeAll(new Filter() + { + @Override + public boolean accept(BlameCommandBuilder.CacheKey item) + { + return repositoryId.equals(item.getRepositoryId()); + } + }); + browseCache.removeAll(new Filter() + { + @Override + public boolean accept(BrowseCommandBuilder.CacheKey item) + { + return repositoryId.equals(item.getRepositoryId()); + } + }); + logCache.removeAll(new Filter() + { + @Override + public boolean accept(LogCommandBuilder.CacheKey item) + { + return repositoryId.equals(item.getRepositoryId()); + } + }); + } + + //~--- fields ------------------------------------------------------------- + + /** Field description */ + private Cache blameCache; + + /** Field description */ + private Cache browseCache; + + /** Field description */ + private Cache logCache; + } + + //~--- fields --------------------------------------------------------------- /** Field description */