refactor CacheClearHook of RepositoryServiceFactory

This commit is contained in:
Sebastian Sdorra
2016-11-10 22:05:24 +01:00
parent 5cb32b268f
commit 8628fd9e11

View File

@@ -37,6 +37,7 @@ package sonia.scm.repository.api;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
@@ -316,51 +317,60 @@ public final class RepositoryServiceFactory
//~--- inner classes -------------------------------------------------------- //~--- inner classes --------------------------------------------------------
/** /**
* TODO find a more elegant way * Hook and listener to clear all relevant repository caches.
*
*
* @version Enter version here..., 12/06/16
* @author Enter your name here...
*/ */
private static class CacheClearHook extends PostReceiveRepositoryHook private static class CacheClearHook extends PostReceiveRepositoryHook
implements RepositoryListener implements RepositoryListener
{ {
private final Set<Cache<?, ?>> caches = Sets.newHashSet();
/** /**
* Constructs ... * Constructs a new instance and collect all repository relevant
* caches from the {@link CacheManager}.
* *
* * @param cacheManager cache manager
* @param cacheManager
*/ */
public CacheClearHook(CacheManager cacheManager) public CacheClearHook(CacheManager cacheManager)
{ {
this.blameCache = this.caches.add(cacheManager.getCache(
cacheManager.getCache(BlameCommandBuilder.CacheKey.class, BlameCommandBuilder.CacheKey.class,
BlameResult.class, BlameCommandBuilder.CACHE_NAME); BlameResult.class, BlameCommandBuilder.CACHE_NAME)
this.browseCache = );
cacheManager.getCache(BrowseCommandBuilder.CacheKey.class, this.caches.add(cacheManager.getCache(
BrowserResult.class, BrowseCommandBuilder.CACHE_NAME); BrowseCommandBuilder.CacheKey.class,
this.logCache = cacheManager.getCache(LogCommandBuilder.CacheKey.class, BrowserResult.class, BrowseCommandBuilder.CACHE_NAME)
ChangesetPagingResult.class, LogCommandBuilder.CACHE_NAME); );
this.tagsCache = cacheManager.getCache(TagsCommandBuilder.CacheKey.class, this.caches.add(cacheManager.getCache(
Tags.class, TagsCommandBuilder.CACHE_NAME); LogCommandBuilder.CacheKey.class,
this.branchesCache = ChangesetPagingResult.class, LogCommandBuilder.CACHE_NAME)
cacheManager.getCache(BranchesCommandBuilder.CacheKey.class, );
Branches.class, BranchesCommandBuilder.CACHE_NAME); this.caches.add(cacheManager.getCache(
TagsCommandBuilder.CacheKey.class,
Tags.class, TagsCommandBuilder.CACHE_NAME)
);
this.caches.add(cacheManager.getCache(
BranchesCommandBuilder.CacheKey.class,
Branches.class, BranchesCommandBuilder.CACHE_NAME)
);
} }
//~--- methods ------------------------------------------------------------ //~--- methods ------------------------------------------------------------
/**
* Clear caches on explicit repository cache clear event.
*
* @param event clear event
*/
@Subscribe @Subscribe
public void onEvent(ClearRepositoryCacheEvent event) { public void onEvent(ClearRepositoryCacheEvent event) {
clearCaches(event.getRepository().getId()); clearCaches(event.getRepository().getId());
} }
/** /**
* Method description * Clear caches on repository push.
* *
* * @param event hook event
* @param event
*/ */
@Override @Override
public void onEvent(RepositoryHookEvent event) public void onEvent(RepositoryHookEvent event)
@@ -376,11 +386,10 @@ public final class RepositoryServiceFactory
} }
/** /**
* Method description * Clear caches on repository delete event.
* *
* * @param repository changed repository
* @param repository * @param event repository event
* @param event
*/ */
@Override @Override
public void onEvent(Repository repository, HandlerEvent event) public void onEvent(Repository repository, HandlerEvent event)
@@ -390,13 +399,7 @@ public final class RepositoryServiceFactory
clearCaches(repository.getId()); clearCaches(repository.getId());
} }
} }
/**
* Method description
*
*
* @param repositoryId
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void clearCaches(final String repositoryId) private void clearCaches(final String repositoryId)
{ {
@@ -405,32 +408,11 @@ public final class RepositoryServiceFactory
logger.debug("clear caches for repository id {}", repositoryId); logger.debug("clear caches for repository id {}", repositoryId);
} }
RepositoryCacheKeyFilter filter = RepositoryCacheKeyFilter filter = new RepositoryCacheKeyFilter(repositoryId);
new RepositoryCacheKeyFilter(repositoryId); for (Cache<?,?> cache : caches) {
cache.removeAll(filter);
blameCache.removeAll(filter); }
browseCache.removeAll(filter);
logCache.removeAll(filter);
tagsCache.removeAll(filter);
branchesCache.removeAll(filter);
} }
//~--- fields -------------------------------------------------------------
/** Field description */
private Cache<BlameCommandBuilder.CacheKey, BlameResult> blameCache;
/** Field description */
private Cache<BranchesCommandBuilder.CacheKey, Branches> branchesCache;
/** Field description */
private Cache<BrowseCommandBuilder.CacheKey, BrowserResult> browseCache;
/** Field description */
private Cache<LogCommandBuilder.CacheKey, ChangesetPagingResult> logCache;
/** Field description */
private Cache<TagsCommandBuilder.CacheKey, Tags> tagsCache;
} }