mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
fixes usage of named cache configurations, see issue #943
This commit is contained in:
@@ -78,15 +78,22 @@ public class GuavaCacheManager implements CacheManager, org.apache.shiro.cache.C
|
|||||||
protected GuavaCacheManager(GuavaCacheManagerConfiguration config) {
|
protected GuavaCacheManager(GuavaCacheManagerConfiguration config) {
|
||||||
defaultConfiguration = config.getDefaultCache();
|
defaultConfiguration = config.getDefaultCache();
|
||||||
|
|
||||||
for (GuavaNamedCacheConfiguration ncc : config.getCaches()) {
|
for (GuavaNamedCacheConfiguration namedCacheConfiguration : config.getCaches()) {
|
||||||
logger.debug("create cache {} from configured configuration {}", ncc.getName(), ncc);
|
logger.debug("create cache {} from configured configuration {}",
|
||||||
cacheMap.put(ncc.getName(), new CacheWithConfiguration(
|
namedCacheConfiguration.getName(), namedCacheConfiguration
|
||||||
GuavaCaches.create(defaultConfiguration, ncc.getName()),
|
|
||||||
defaultConfiguration)
|
|
||||||
);
|
);
|
||||||
|
cacheMap.put(namedCacheConfiguration.getName(), createCacheWithConfiguration(namedCacheConfiguration));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CacheWithConfiguration createCacheWithConfiguration(GuavaNamedCacheConfiguration namedCacheConfiguration) {
|
||||||
|
return createCacheWithConfiguration(namedCacheConfiguration, namedCacheConfiguration.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private CacheWithConfiguration createCacheWithConfiguration(GuavaCacheConfiguration configuration, String name) {
|
||||||
|
return new CacheWithConfiguration(GuavaCaches.create(configuration, name), configuration);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
logger.info("close guava cache manager");
|
logger.info("close guava cache manager");
|
||||||
@@ -110,7 +117,7 @@ public class GuavaCacheManager implements CacheManager, org.apache.shiro.cache.C
|
|||||||
"cache {} does not exists, creating a new instance from default configuration: {}",
|
"cache {} does not exists, creating a new instance from default configuration: {}",
|
||||||
name, defaultConfiguration
|
name, defaultConfiguration
|
||||||
);
|
);
|
||||||
cache = new CacheWithConfiguration(GuavaCaches.create(defaultConfiguration, name), defaultConfiguration);
|
cache = createCacheWithConfiguration(defaultConfiguration, name);
|
||||||
cacheMap.put(name, cache);
|
cacheMap.put(name, cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,15 +32,33 @@
|
|||||||
|
|
||||||
package sonia.scm.cache;
|
package sonia.scm.cache;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class GuavaCacheManagerTest extends CacheManagerTestBase
|
public class GuavaCacheManagerTest extends CacheManagerTestBase
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@Mock(answer = Answers.CALLS_REAL_METHODS)
|
||||||
|
private GuavaCacheConfiguration defaultConfiguration;
|
||||||
|
|
||||||
|
@Mock(answer = Answers.CALLS_REAL_METHODS)
|
||||||
|
private GuavaNamedCacheConfiguration configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -61,5 +79,23 @@ public class GuavaCacheManagerTest extends CacheManagerTestBase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void configuration() {
|
||||||
|
when(configuration.getName()).thenReturn("my-crazy-cache");
|
||||||
|
when(configuration.getCopyStrategy()).thenReturn(CopyStrategy.READWRITE);
|
||||||
|
when(defaultConfiguration.getCopyStrategy()).thenReturn(CopyStrategy.READ);
|
||||||
|
|
||||||
|
List<GuavaNamedCacheConfiguration> configurations = Lists.newArrayList(configuration);
|
||||||
|
GuavaCacheManagerConfiguration managerConfiguration = new GuavaCacheManagerConfiguration(defaultConfiguration, configurations);
|
||||||
|
GuavaCacheManager guavaCacheManager = new GuavaCacheManager(managerConfiguration);
|
||||||
|
|
||||||
|
// default cache
|
||||||
|
GuavaSecurityCache<?,?> sorbotCache = guavaCacheManager.getCache("sorbot-cache");
|
||||||
|
assertEquals(CopyStrategy.READ, sorbotCache.copyStrategy);
|
||||||
|
|
||||||
|
// configured cache
|
||||||
|
GuavaSecurityCache<?,?> crazyCache = guavaCacheManager.getCache("my-crazy-cache");
|
||||||
|
assertEquals(CopyStrategy.READWRITE, crazyCache.copyStrategy);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user