simplify cache api

This commit is contained in:
Sebastian Sdorra
2014-01-09 20:06:07 +01:00
parent 2e6748cb23
commit 908f2fe6c8
17 changed files with 107 additions and 209 deletions

View File

@@ -51,14 +51,11 @@ public interface CacheManager extends Closeable
* Returns the cache with the specified types and name.
* If the cache does not exist, a new cache is created.
*
*
* @param key - The type of the keys for the cache
* @param value - The type of cache elements
* @param name - The name of the cache
* @param <K> - The type of the keys for the cache
* @param <V> - The type of cache elements
*
* @return the cache with the specified types and name
*/
public <K, V> Cache<K, V> getCache(Class<K> key, Class<V> value, String name);
public <K, V> Cache<K, V> getCache(String name);
}

View File

@@ -42,13 +42,12 @@ import sonia.scm.event.Event;
* through the repository. This class is wrapper of {@link RepositoryHookEvent}
* for the event system of SCM-Manager.
*
* TODO: make final for version 2.0
*
* @author Sebastian Sdorra
* @since 1.23
*/
@Event
public class PreReceiveRepositoryHookEvent extends WrappedRepositoryHookEvent
public final class PreReceiveRepositoryHookEvent
extends WrappedRepositoryHookEvent
{
/**

View File

@@ -105,8 +105,7 @@ public final class BlameCommandBuilder
BlameCommandBuilder(CacheManager cacheManager, BlameCommand blameCommand,
Repository repository, PreProcessorUtil preProcessorUtil)
{
this.cache = cacheManager.getCache(CacheKey.class, BlameResult.class,
CACHE_NAME);
this.cache = cacheManager.getCache(CACHE_NAME);
this.blameCommand = blameCommand;
this.repository = repository;
this.preProcessorUtil = preProcessorUtil;
@@ -151,7 +150,7 @@ public final class BlameCommandBuilder
requestClone.setPath(path);
BlameResult result = null;
BlameResult result;
if (disableCache)
{
@@ -351,21 +350,21 @@ public final class BlameCommandBuilder
//~--- fields -------------------------------------------------------------
/** Field description */
private String repositoryId;
/** repository id */
private final String repositoryId;
/** Field description */
private BlameCommandRequest request;
/** request object */
private final BlameCommandRequest request;
}
//~--- fields ---------------------------------------------------------------
/** implementation of the blame command */
private BlameCommand blameCommand;
private final BlameCommand blameCommand;
/** the cache */
private Cache<CacheKey, BlameResult> cache;
private final Cache<CacheKey, BlameResult> cache;
/** disable escaping */
private boolean disableEscaping = false;
@@ -377,11 +376,11 @@ public final class BlameCommandBuilder
private boolean disablePreProcessors = false;
/** Field description */
private PreProcessorUtil preProcessorUtil;
private final PreProcessorUtil preProcessorUtil;
/** the repository */
private Repository repository;
private final Repository repository;
/** request for the blame command implementation */
private BlameCommandRequest request = new BlameCommandRequest();
private final BlameCommandRequest request = new BlameCommandRequest();
}

View File

@@ -94,8 +94,7 @@ public final class BranchesCommandBuilder
BranchesCommandBuilder(CacheManager cacheManager,
BranchesCommand branchesCommand, Repository repository)
{
this.cache = cacheManager.getCache(CacheKey.class, Branches.class,
CACHE_NAME);
this.cache = cacheManager.getCache(CACHE_NAME);
this.branchesCommand = branchesCommand;
this.repository = repository;
}
@@ -113,7 +112,7 @@ public final class BranchesCommandBuilder
*/
public Branches getBranches() throws RepositoryException, IOException
{
Branches branches = null;
Branches branches;
if (disableCache)
{
@@ -270,22 +269,22 @@ public final class BranchesCommandBuilder
//~--- fields -------------------------------------------------------------
/** Field description */
private String repositoryId;
/** repository id */
private final String repositoryId;
}
//~--- fields ---------------------------------------------------------------
/** branches command implementation */
private BranchesCommand branchesCommand;
private final BranchesCommand branchesCommand;
/** cache for branches */
private Cache<CacheKey, Branches> cache;
private final Cache<CacheKey, Branches> cache;
/** disable cache */
private boolean disableCache = false;
/** repository */
private Repository repository;
private final Repository repository;
}

View File

@@ -108,8 +108,7 @@ public final class BrowseCommandBuilder
BrowseCommandBuilder(CacheManager cacheManager, BrowseCommand browseCommand,
Repository repository, PreProcessorUtil preProcessorUtil)
{
this.cache = cacheManager.getCache(CacheKey.class, BrowserResult.class,
CACHE_NAME);
this.cache = cacheManager.getCache(CACHE_NAME);
this.browseCommand = browseCommand;
this.repository = repository;
this.preProcessorUtil = preProcessorUtil;
@@ -424,21 +423,21 @@ public final class BrowseCommandBuilder
//~--- fields -------------------------------------------------------------
/** Field description */
private String repositoryId;
/** repository id */
private final String repositoryId;
/** Field description */
private BrowseCommandRequest request;
/** request object */
private final BrowseCommandRequest request;
}
//~--- fields ---------------------------------------------------------------
/** implementation of the browse command */
private BrowseCommand browseCommand;
private final BrowseCommand browseCommand;
/** cache */
private Cache<CacheKey, BrowserResult> cache;
private final Cache<CacheKey, BrowserResult> cache;
/** disable escaping */
private boolean disableEscaping = false;
@@ -450,11 +449,11 @@ public final class BrowseCommandBuilder
private boolean disablePreProcessors = false;
/** Field description */
private PreProcessorUtil preProcessorUtil;
private final PreProcessorUtil preProcessorUtil;
/** the repsitory */
private Repository repository;
private final Repository repository;
/** request for the command */
private BrowseCommandRequest request = new BrowseCommandRequest();
private final BrowseCommandRequest request = new BrowseCommandRequest();
}

View File

@@ -116,8 +116,7 @@ public final class LogCommandBuilder
LogCommandBuilder(CacheManager cacheManager, LogCommand logCommand,
Repository repository, PreProcessorUtil preProcessorUtil)
{
this.cache = cacheManager.getCache(CacheKey.class,
ChangesetPagingResult.class, CACHE_NAME);
this.cache = cacheManager.getCache(CACHE_NAME);
this.logCommand = logCommand;
this.repository = repository;
this.preProcessorUtil = preProcessorUtil;
@@ -172,7 +171,7 @@ public final class LogCommandBuilder
public Changeset getChangeset(String id)
throws IOException, RepositoryException
{
Changeset changeset = null;
Changeset changeset;
if (disableCache)
{
@@ -234,7 +233,7 @@ public final class LogCommandBuilder
public ChangesetPagingResult getChangesets()
throws IOException, RepositoryException
{
ChangesetPagingResult cpr = null;
ChangesetPagingResult cpr;
if (disableCache)
{
@@ -453,6 +452,7 @@ public final class LogCommandBuilder
{
this.repositoryId = repository.getId();
this.request = request;
this.changesetId = null;
}
/**
@@ -467,6 +467,7 @@ public final class LogCommandBuilder
{
this.repositoryId = repository.getId();
this.changesetId = changesetId;
this.request = null;
}
//~--- methods ------------------------------------------------------------
@@ -528,20 +529,29 @@ public final class LogCommandBuilder
//~--- fields -------------------------------------------------------------
/** Field description */
private String changesetId;
private final String changesetId;
/** Field description */
private String repositoryId;
private final String repositoryId;
/** Field description */
private LogCommandRequest request;
private final LogCommandRequest request;
}
//~--- fields ---------------------------------------------------------------
/** cache for changesets */
private Cache<CacheKey, ChangesetPagingResult> cache;
private final Cache<CacheKey, ChangesetPagingResult> cache;
/** Implementation of the log command */
private final LogCommand logCommand;
/** Field description */
private final PreProcessorUtil preProcessorUtil;
/** repository to query */
private final Repository repository;
/** disable escaping */
private boolean disableEscaping = false;
@@ -552,15 +562,6 @@ public final class LogCommandBuilder
/** disable the execution of pre processors */
private boolean disablePreProcessors = false;
/** Implementation of the log command */
private LogCommand logCommand;
/** Field description */
private PreProcessorUtil preProcessorUtil;
/** repository to query */
private Repository repository;
/** request for the log command */
private LogCommandRequest request = new LogCommandRequest();
private final LogCommandRequest request = new LogCommandRequest();
}

View File

@@ -307,19 +307,11 @@ public final class RepositoryServiceFactory
*/
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);
this.tagsCache = cacheManager.getCache(TagsCommandBuilder.CacheKey.class,
Tags.class, TagsCommandBuilder.CACHE_NAME);
this.branchesCache =
cacheManager.getCache(BranchesCommandBuilder.CacheKey.class,
Branches.class, BranchesCommandBuilder.CACHE_NAME);
this.blameCache = cacheManager.getCache(BlameCommandBuilder.CACHE_NAME);
this.browseCache = cacheManager.getCache(BrowseCommandBuilder.CACHE_NAME);
this.logCache = cacheManager.getCache(LogCommandBuilder.CACHE_NAME);
this.tagsCache = cacheManager.getCache(TagsCommandBuilder.CACHE_NAME);
this.branchesCache =cacheManager.getCache(BranchesCommandBuilder.CACHE_NAME);
}
//~--- methods ------------------------------------------------------------

View File

@@ -30,6 +30,7 @@
*/
package sonia.scm.repository.api;
//~--- non-JDK imports --------------------------------------------------------
@@ -94,7 +95,7 @@ public final class TagsCommandBuilder
TagsCommandBuilder(CacheManager cacheManager, TagsCommand command,
Repository repository)
{
this.cache = cacheManager.getCache(CacheKey.class, Tags.class, CACHE_NAME);
this.cache = cacheManager.getCache(CACHE_NAME);
this.command = command;
this.repository = repository;
}
@@ -112,7 +113,7 @@ public final class TagsCommandBuilder
*/
public Tags getTags() throws RepositoryException, IOException
{
Tags tags = null;
Tags tags;
if (disableCache)
{
@@ -270,22 +271,22 @@ public final class TagsCommandBuilder
//~--- fields -------------------------------------------------------------
/** Field description */
private String repositoryId;
/** repository id */
private final String repositoryId;
}
//~--- fields ---------------------------------------------------------------
/** cache for changesets */
private Cache<CacheKey, Tags> cache;
private final Cache<CacheKey, Tags> cache;
/** Field description */
private TagsCommand command;
/** command implementation */
private final TagsCommand command;
/** Field description */
/** repository */
private final Repository repository;
/** disable cache */
private boolean disableCache = false;
/** Field description */
private Repository repository;
}

View File

@@ -91,7 +91,7 @@ public class HgPackageReader
public HgPackageReader(CacheManager cacheManager,
Provider<HttpClient> httpClientProvider)
{
cache = cacheManager.getCache(String.class, HgPackages.class, CACHENAME);
cache = cacheManager.getCache(CACHENAME);
this.httpClientProvider = httpClientProvider;
}
@@ -248,8 +248,8 @@ public class HgPackageReader
//~--- fields ---------------------------------------------------------------
/** Field description */
private Cache<String, HgPackages> cache;
private final Cache<String, HgPackages> cache;
/** Field description */
private Provider<HttpClient> httpClientProvider;
private final Provider<HttpClient> httpClientProvider;
}

View File

@@ -70,8 +70,6 @@ public class MapCacheManager implements CacheManager
* Method description
*
*
* @param key
* @param value
* @param name
* @param <K>
* @param <V>
@@ -79,103 +77,22 @@ public class MapCacheManager implements CacheManager
* @return
*/
@Override
public <K, V> Cache<K, V> getCache(Class<K> key, Class<V> value, String name)
public <K, V> Cache<K, V> getCache(String name)
{
CacheKey cacheKey = new CacheKey(key, value, name);
Cache<K, V> cache = cacheMap.get(cacheKey);
Cache<K, V> cache = cacheMap.get(name);
if (cache == null)
{
cache = new MapCache<K, V>();
cacheMap.put(cacheKey, cache);
cacheMap.put(name, cache);
}
return cache;
}
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 12/06/24
* @author Enter your name here...
*/
private static class CacheKey
{
/**
* Constructs ...
*
*
* @param key
* @param value
* @param name
*/
public CacheKey(Class key, Class value, String name)
{
this.key = key;
this.value = value;
this.name = name;
}
//~--- methods ------------------------------------------------------------
/**
* Method description
*
*
* @param obj
*
* @return
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final CacheKey other = (CacheKey) obj;
return Objects.equal(key, other.key) && Objects.equal(value, other.value)
&& Objects.equal(name, other.name);
}
/**
* Method description
*
*
* @return
*/
@Override
public int hashCode()
{
return Objects.hashCode(key, value, name);
}
//~--- fields -------------------------------------------------------------
/** Field description */
private Class key;
/** Field description */
private String name;
/** Field description */
private Class value;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Map<CacheKey, Cache> cacheMap = Maps.newHashMap();
private final Map<String, Cache> cacheMap = Maps.newHashMap();
}

View File

@@ -36,6 +36,7 @@ package sonia.scm.api.rest.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.github.legman.Subscribe;
import com.google.common.base.Function;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@@ -45,11 +46,13 @@ import org.codehaus.enunciate.modules.jersey.ExternallyManagedLifecycle;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.group.Group;
import sonia.scm.group.GroupEvent;
import sonia.scm.group.GroupManager;
import sonia.scm.search.SearchHandler;
import sonia.scm.search.SearchResult;
import sonia.scm.search.SearchResults;
import sonia.scm.user.User;
import sonia.scm.user.UserEvent;
import sonia.scm.user.UserManager;
//~--- JDK imports ------------------------------------------------------------
@@ -59,8 +62,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import sonia.scm.group.GroupEvent;
import sonia.scm.user.UserEvent;
/**
*
@@ -94,14 +95,13 @@ public class SearchResource
{
// create user searchhandler
Cache<String, SearchResults> userCache =
cacheManager.getCache(String.class, SearchResults.class, CACHE_USER);
Cache<String, SearchResults> userCache = cacheManager.getCache(CACHE_USER);
this.userSearchHandler = new SearchHandler<User>(userCache, userManager);
// create group searchhandler
Cache<String, SearchResults> groupCache =
cacheManager.getCache(String.class, SearchResults.class, CACHE_GROUP);
cacheManager.getCache(CACHE_GROUP);
this.groupSearchHandler = new SearchHandler<Group>(groupCache,
groupManager);
@@ -118,7 +118,8 @@ public class SearchResource
@Subscribe
public void onEvent(UserEvent event)
{
if ( event.getEventType().isPost() ){
if (event.getEventType().isPost())
{
userSearchHandler.clearCache();
}
}
@@ -132,7 +133,8 @@ public class SearchResource
@Subscribe
public void onEvent(GroupEvent event)
{
if ( event.getEventType().isPost() ){
if (event.getEventType().isPost())
{
groupSearchHandler.clearCache();
}
}

View File

@@ -118,8 +118,6 @@ public class GuavaCacheManager implements CacheManager
* Method description
*
*
* @param key
* @param value
* @param name
* @param <K>
* @param <V>
@@ -127,8 +125,7 @@ public class GuavaCacheManager implements CacheManager
* @return
*/
@Override
public synchronized <K, V> GuavaCache<K, V> getCache(Class<K> key,
Class<V> value, String name)
public synchronized <K, V> GuavaCache<K, V> getCache(String name)
{
logger.trace("try to retrieve cache {}", name);

View File

@@ -130,8 +130,7 @@ public class DefaultPluginManager implements PluginManager
{
this.context = context;
this.configuration = configuration;
this.cache = cacheManager.getCache(String.class, PluginCenter.class,
CACHE_NAME);
this.cache = cacheManager.getCache(CACHE_NAME);
this.clientProvider = clientProvider;
installedPlugins = new HashMap<String, Plugin>();

View File

@@ -106,8 +106,7 @@ public class AuthorizationCollector
RepositoryDAO repositoryDAO, SecuritySystem securitySystem,
PermissionResolver resolver)
{
this.cache = cacheManager.getCache(CacheKey.class, AuthorizationInfo.class,
CACHE_NAME);
this.cache = cacheManager.getCache(CACHE_NAME);
this.repositoryDAO = repositoryDAO;
this.securitySystem = securitySystem;
this.resolver = resolver;
@@ -524,15 +523,15 @@ public class AuthorizationCollector
//~--- fields ---------------------------------------------------------------
/** Field description */
private Cache<CacheKey, AuthorizationInfo> cache;
/** authorization cache */
private final Cache<CacheKey, AuthorizationInfo> cache;
/** Field description */
private RepositoryDAO repositoryDAO;
/** repository dao */
private final RepositoryDAO repositoryDAO;
/** Field description */
private PermissionResolver resolver;
/** permission resolver */
private final PermissionResolver resolver;
/** Field description */
private SecuritySystem securitySystem;
/** security system */
private final SecuritySystem securitySystem;
}

View File

@@ -100,8 +100,7 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
AssertUtil.assertIsNotNull(cacheManager);
this.authenticationHandlers = sort(userManager, authenticationHandlerSet);
this.encryptionHandler = encryptionHandler;
this.cache = cacheManager.getCache(String.class,
AuthenticationCacheValue.class, CACHE_NAME);
this.cache = cacheManager.getCache(CACHE_NAME);
}
//~--- methods --------------------------------------------------------------
@@ -368,12 +367,12 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
//~--- fields ---------------------------------------------------------------
/** Field description */
private List<AuthenticationHandler> authenticationHandlers;
/** authentication handlers */
private final List<AuthenticationHandler> authenticationHandlers;
/** Field description */
private Cache<String, AuthenticationCacheValue> cache;
/** authentication cache */
private final Cache<String, AuthenticationCacheValue> cache;
/** Field description */
private EncryptionHandler encryptionHandler;
/** encryption handler */
private final EncryptionHandler encryptionHandler;
}

View File

@@ -89,10 +89,8 @@ public abstract class CacheManagerTestBase<C extends Cache>
@Test
public void testSameReference()
{
Cache<String, String> c1 = cacheManager.getCache(String.class,
String.class, "test-1");
Cache<String, String> c2 = cacheManager.getCache(String.class,
String.class, "test-1");
Cache<String, String> c1 = cacheManager.getCache("test-1");
Cache<String, String> c2 = cacheManager.getCache("test-1");
assertIsSame(c1, c2);
}
@@ -203,16 +201,16 @@ public abstract class CacheManagerTestBase<C extends Cache>
@Override
public Cache call() throws Exception
{
return cacheManager.getCache(String.class, String.class, name);
return cacheManager.getCache(name);
}
//~--- fields -------------------------------------------------------------
/** Field description */
private CacheManager cacheManager;
private final CacheManager cacheManager;
/** Field description */
private String name;
private final String name;
}

View File

@@ -76,7 +76,7 @@ public abstract class CacheTestBase
public void before()
{
cm = createCacheManager();
cache = cm.getCache(String.class, String.class, "test");
cache = cm.getCache("test");
}
/**