Ability to config Spring cache. #346

pull/382/head
Nikita 9 years ago
parent 0cc1f11ec1
commit fef863ea4c

@ -0,0 +1,73 @@
/**
* 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.concurrent.TimeUnit;
public class CacheConfig {
private long ttl;
private TimeUnit ttlUnit;
private long maxIdleTime;
private TimeUnit maxIdleUnit;
public CacheConfig() {
}
public CacheConfig(long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) {
super();
this.ttl = ttl;
this.ttlUnit = ttlUnit;
this.maxIdleTime = maxIdleTime;
this.maxIdleUnit = maxIdleUnit;
}
public long getTTL() {
return ttl;
}
public void setTTL(long ttl) {
this.ttl = ttl;
}
public TimeUnit getTTLUnit() {
return ttlUnit;
}
public void setTTLUnit(TimeUnit ttlUnit) {
this.ttlUnit = ttlUnit;
}
public long getMaxIdleTime() {
return maxIdleTime;
}
public void setMaxIdleTime(long maxIdleTime) {
this.maxIdleTime = maxIdleTime;
}
public TimeUnit getMaxIdleUnit() {
return maxIdleUnit;
}
public void setMaxIdleUnit(TimeUnit maxIdleUnit) {
this.maxIdleUnit = maxIdleUnit;
}
}

@ -16,6 +16,7 @@
package org.redisson.spring.cache;
import org.redisson.core.RMap;
import org.redisson.core.RMapCache;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
@ -26,7 +27,17 @@ import org.springframework.cache.support.SimpleValueWrapper;
*/
public class RedissonCache implements Cache {
private final RMap<Object, Object> map;
private RMapCache<Object, Object> mapCache;
private RMap<Object, Object> map;
private CacheConfig config;
public RedissonCache(RMapCache<Object, Object> mapCache, CacheConfig config) {
this.mapCache = mapCache;
this.map = mapCache;
this.config = config;
}
public RedissonCache(RMap<Object, Object> map) {
this.map = map;
@ -63,11 +74,20 @@ public class RedissonCache implements Cache {
@Override
public void put(Object key, Object value) {
if (mapCache != null) {
mapCache.fastPut(key, value, config.getTTL(), config.getTTLUnit(), config.getMaxIdleTime(), config.getMaxIdleUnit());
} else {
map.fastPut(key, value);
}
}
public ValueWrapper putIfAbsent(Object key, Object value) {
Object prevValue = map.putIfAbsent(key, value);
Object prevValue;
if (mapCache != null) {
prevValue = mapCache.putIfAbsent(key, value, config.getTTL(), config.getTTLUnit(), config.getMaxIdleTime(), config.getMaxIdleUnit());
} else {
prevValue = map.putIfAbsent(key, value);
}
return toValueWrapper(prevValue);
}

@ -17,8 +17,8 @@ package org.redisson.spring.cache;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
import java.util.Map;
import org.redisson.RedissonClient;
import org.springframework.cache.Cache;
@ -31,15 +31,20 @@ import org.springframework.cache.CacheManager;
*/
public class RedissonCacheManager implements CacheManager {
private final Set<String> cacheNames = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
private RedissonClient redisson;
private Map<String, CacheConfig> configMap = new HashMap<String, CacheConfig>();
public RedissonCacheManager() {
}
public RedissonCacheManager(RedissonClient redisson) {
public RedissonCacheManager(RedissonClient redisson, Map<String, CacheConfig> config) {
this.redisson = redisson;
this.configMap = config;
}
public void setConfig(Map<String, CacheConfig> config) {
this.configMap = config;
}
public void setRedisson(RedissonClient redisson) {
@ -48,13 +53,21 @@ public class RedissonCacheManager implements CacheManager {
@Override
public Cache getCache(String name) {
cacheNames.add(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<String> getCacheNames() {
return Collections.unmodifiableSet(cacheNames);
return Collections.unmodifiableSet(configMap.keySet());
}
}

@ -1,10 +1,13 @@
package org.redisson.spring.cache;
import org.assertj.core.api.Assertions;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.redisson.Redisson;
import org.redisson.RedissonClient;
@ -18,7 +21,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import static org.assertj.core.api.Assertions.*;
public class RedissonSpringCacheTest {
@ -87,7 +89,9 @@ public class RedissonSpringCacheTest {
@Bean
CacheManager cacheManager(RedissonClient redissonClient) {
return new RedissonCacheManager(redissonClient);
Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
config.put("testMap", new CacheConfig(24, TimeUnit.HOURS, 12, TimeUnit.SECONDS));
return new RedissonCacheManager(redissonClient, config);
}
}

Loading…
Cancel
Save