From 1723e68bed544f90738d3d0057c01a4888cba270 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 29 Jan 2016 18:29:00 +0300 Subject: [PATCH] RedissonCacheManager renamed to RedissonSpringCacheManager. More params added to constructor. #388 --- .../redisson/spring/cache/RedissonCache.java | 2 +- .../spring/cache/RedissonCacheManager.java | 87 ------- .../cache/RedissonSpringCacheManager.java | 215 ++++++++++++++++++ .../spring/cache/RedissonSpringCacheTest.java | 50 +++- .../redisson/spring/cache/cache-config.json | 1 + 5 files changed, 258 insertions(+), 97 deletions(-) delete mode 100644 src/main/java/org/redisson/spring/cache/RedissonCacheManager.java create mode 100644 src/main/java/org/redisson/spring/cache/RedissonSpringCacheManager.java create mode 100644 src/test/resources/org/redisson/spring/cache/cache-config.json diff --git a/src/main/java/org/redisson/spring/cache/RedissonCache.java b/src/main/java/org/redisson/spring/cache/RedissonCache.java index 740ff8a69..2cbb70c48 100644 --- a/src/main/java/org/redisson/spring/cache/RedissonCache.java +++ b/src/main/java/org/redisson/spring/cache/RedissonCache.java @@ -31,7 +31,7 @@ public class RedissonCache implements Cache { private RMapCache mapCache; - private RMap map; + private final RMap map; private CacheConfig config; diff --git a/src/main/java/org/redisson/spring/cache/RedissonCacheManager.java b/src/main/java/org/redisson/spring/cache/RedissonCacheManager.java deleted file mode 100644 index 52fc8a3f5..000000000 --- a/src/main/java/org/redisson/spring/cache/RedissonCacheManager.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Copyright 2014 Nikita Koksharov, Nickolay Borbit - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.redisson.spring.cache; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.redisson.RedissonClient; -import org.springframework.cache.Cache; -import org.springframework.cache.CacheManager; - -/** - * - * @author Nikita Koksharov - * - */ -public class RedissonCacheManager implements CacheManager { - - private RedissonClient redisson; - - private Map configMap = new HashMap(); - - public RedissonCacheManager() { - } - - public RedissonCacheManager(RedissonClient redisson) { - this.redisson = redisson; - } - - public RedissonCacheManager(RedissonClient redisson, Map config) { - this.redisson = redisson; - this.configMap = config; - } - - /** - * Set cache config mapped by cache name - * - * @param config - */ - public void setConfig(Map config) { - this.configMap = config; - } - - /** - * Set Redisson instance - * - * @param config - */ - public void setRedisson(RedissonClient redisson) { - this.redisson = redisson; - } - - @Override - public Cache getCache(String name) { - CacheConfig config = configMap.get(name); - if (config == null) { - config = new CacheConfig(); - configMap.put(name, config); - return new RedissonCache(redisson.getMap(name)); - } - if (config.getMaxIdleTime() == 0 && config.getTTL() == 0) { - return new RedissonCache(redisson.getMap(name)); - } - return new RedissonCache(redisson.getMapCache(name), config); - } - - @Override - public Collection getCacheNames() { - return Collections.unmodifiableSet(configMap.keySet()); - } - -} diff --git a/src/main/java/org/redisson/spring/cache/RedissonSpringCacheManager.java b/src/main/java/org/redisson/spring/cache/RedissonSpringCacheManager.java new file mode 100644 index 000000000..2c4f84830 --- /dev/null +++ b/src/main/java/org/redisson/spring/cache/RedissonSpringCacheManager.java @@ -0,0 +1,215 @@ +/** + * Copyright 2014 Nikita Koksharov, Nickolay Borbit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.spring.cache; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.redisson.RedissonClient; +import org.redisson.client.codec.Codec; +import org.redisson.core.RMap; +import org.redisson.core.RMapCache; +import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.context.ResourceLoaderAware; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; + +/** + * A {@link org.springframework.cache.CacheManager} implementation + * backed by Redisson instance. + * + * @author Nikita Koksharov + * + */ +public class RedissonSpringCacheManager implements CacheManager, ResourceLoaderAware, InitializingBean { + + private ResourceLoader resourceLoader; + + private Codec codec; + + private RedissonClient redisson; + + private Map configMap = new HashMap(); + + private String configLocation; + + public RedissonSpringCacheManager() { + } + + /** + * Creates CacheManager supplied by Redisson instance and + * Cache config mapped by Cache name + * + * @param redisson + * @param config + */ + public RedissonSpringCacheManager(RedissonClient redisson, Map config) { + this(redisson, config, null); + } + + /** + * Creates CacheManager supplied by Redisson instance, Codec instance + * and Cache config mapped by Cache name. + *

+ * Each Cache instance share one Codec instance. + * + * @param redisson + * @param config + */ + public RedissonSpringCacheManager(RedissonClient redisson, Map config, Codec codec) { + this.redisson = redisson; + this.configMap = config; + this.codec = codec; + } + + /** + * Creates CacheManager supplied by Redisson instance + * and Cache config mapped by Cache name. + *

+ * Loads the config file from the class path, interpreting plain paths as class path resource names + * that include the package path (e.g. "mypackage/myresource.txt"). + * + * @param redisson + * @param config + */ + public RedissonSpringCacheManager(RedissonClient redisson, String configLocation) { + this(redisson, configLocation, null); + } + + /** + * Creates CacheManager supplied by Redisson instance, Codec instance + * and Config location path. + *

+ * Each Cache instance share one Codec instance. + *

+ * Loads the config file from the class path, interpreting plain paths as class path resource names + * that include the package path (e.g. "mypackage/myresource.txt"). + * + * @param redisson + * @param config + */ + public RedissonSpringCacheManager(RedissonClient redisson, String configLocation, Codec codec) { + this.redisson = redisson; + this.configLocation = configLocation; + this.codec = codec; + } + + /** + * Set cache config location + * + * @param config + * @throws IOException + */ + public void setConfigLocation(String configLocation) { + this.configLocation = configLocation; + } + + /** + * Set cache config mapped by cache name + * + * @param config + */ + public void setConfig(Map config) { + this.configMap = config; + } + + /** + * Set Redisson instance + * + * @param config + */ + public void setRedisson(RedissonClient redisson) { + this.redisson = redisson; + } + + /** + * Set Codec instance shared between all Cache instances + * + * @param codec + */ + public void setCodec(Codec codec) { + this.codec = codec; + } + + @Override + public Cache getCache(String name) { + CacheConfig config = configMap.get(name); + if (config == null) { + config = new CacheConfig(); + configMap.put(name, config); + + RMap map = createMap(name); + return new RedissonCache(map); + } + if (config.getMaxIdleTime() == 0 && config.getTTL() == 0) { + RMap map = createMap(name); + return new RedissonCache(map); + } + RMapCache map = createMapCache(name); + return new RedissonCache(map, config); + } + + private RMap createMap(String name) { + if (codec != null) { + return redisson.getMap(name, codec); + } + return redisson.getMap(name); + } + + private RMapCache createMapCache(String name) { + if (codec != null) { + return redisson.getMapCache(name, codec); + } + return redisson.getMapCache(name); + } + + @Override + public Collection getCacheNames() { + return Collections.unmodifiableSet(configMap.keySet()); + } + + @Override + public void setResourceLoader(ResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + + @Override + public void afterPropertiesSet() throws Exception { + if (configLocation == null) { + return; + } + + Resource resource = resourceLoader.getResource(configLocation); + try { + this.configMap = CacheConfig.fromJSON(resource.getInputStream()); + } catch (IOException e) { + // try to read yaml + try { + this.configMap = CacheConfig.fromYAML(resource.getInputStream()); + } catch (IOException e1) { + throw new BeanDefinitionStoreException( + "Could not parse cache configuration at [" + configLocation + "]", e1); + } + } + } + +} diff --git a/src/test/java/org/redisson/spring/cache/RedissonSpringCacheTest.java b/src/test/java/org/redisson/spring/cache/RedissonSpringCacheTest.java index 4c8411362..a1c7d0bcf 100644 --- a/src/test/java/org/redisson/spring/cache/RedissonSpringCacheTest.java +++ b/src/test/java/org/redisson/spring/cache/RedissonSpringCacheTest.java @@ -2,27 +2,37 @@ package org.redisson.spring.cache; import static org.assertj.core.api.Assertions.assertThat; +import java.io.Closeable; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.TimeUnit; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import org.redisson.Redisson; import org.redisson.RedissonClient; +import org.redisson.client.codec.Codec; +import org.redisson.codec.JsonJacksonCodec; +import org.redisson.codec.SerializationCodec; +import org.springframework.beans.factory.DisposableBean; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Service; +@RunWith(Parameterized.class) public class RedissonSpringCacheTest { public static class SampleObject { @@ -92,21 +102,43 @@ public class RedissonSpringCacheTest { CacheManager cacheManager(RedissonClient redissonClient) throws IOException { Map config = new HashMap(); config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000)); - return new RedissonCacheManager(redissonClient, config); + return new RedissonSpringCacheManager(redissonClient, config); } } - private AnnotationConfigApplicationContext context; + @Configuration + @ComponentScan + @EnableCaching + public static class JsonConfigApplication { + + @Bean(destroyMethod="shutdown") + RedissonClient redisson() { + return Redisson.create(); + } + + @Bean + CacheManager cacheManager(RedissonClient redissonClient) throws IOException { + return new RedissonSpringCacheManager(redissonClient, "classpath:/org/redisson/spring/cache/cache-config.json"); + } - @Before - public void before() { - context = new AnnotationConfigApplicationContext(Application.class); } - @After - public void after() { - context.destroy(); + + @Parameterized.Parameters(name= "{index} - {0}") + public static Iterable data() { + return Arrays.asList(new Object[][] { + {new AnnotationConfigApplicationContext(Application.class)}, + {new AnnotationConfigApplicationContext(JsonConfigApplication.class)} + }); + } + + @Parameterized.Parameter(0) + public AnnotationConfigApplicationContext context; + + @AfterClass + public static void after() { + RedissonSpringCacheTest.data().forEach(e -> ((ConfigurableApplicationContext)e[0]).close()); } @Test diff --git a/src/test/resources/org/redisson/spring/cache/cache-config.json b/src/test/resources/org/redisson/spring/cache/cache-config.json new file mode 100644 index 000000000..40f5839aa --- /dev/null +++ b/src/test/resources/org/redisson/spring/cache/cache-config.json @@ -0,0 +1 @@ +{"testMap":{"ttl":1440000,"maxIdleTime":720000}} \ No newline at end of file