Spring cache config simplified. #346

pull/382/head
Nikita 9 years ago
parent 0eaba2ac59
commit 90e8fed310

@ -21,7 +21,6 @@ import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.net.URL; import java.net.URL;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
/** /**
* Cache config object used for Spring cache configuration. * Cache config object used for Spring cache configuration.
@ -33,12 +32,8 @@ public class CacheConfig {
private long ttl; private long ttl;
private TimeUnit ttlUnit;
private long maxIdleTime; private long maxIdleTime;
private TimeUnit maxIdleUnit;
/** /**
* Creates config object with * Creates config object with
* <code>ttl = 0</code> and <code>maxIdleTime = 0</code>. * <code>ttl = 0</code> and <code>maxIdleTime = 0</code>.
@ -50,22 +45,20 @@ public class CacheConfig {
/** /**
* Creates config object. * Creates config object.
* *
* @param ttl - time to live for key\value entry. * @param ttl - time to live for key\value entry in milliseconds.
* If <code>0</code> then time to live doesn't affect entry expiration. * If <code>0</code> then time to live doesn't affect entry expiration.
* @param ttlUnit * @param ttlUnit
* @param maxIdleTime - max idle time for key\value entry. * @param maxIdleTime - max idle time for key\value entry in milliseconds.
* If <code>0</code> then max idle time doesn't affect entry expiration. * If <code>0</code> then max idle time doesn't affect entry expiration.
* @param maxIdleUnit * @param maxIdleUnit
* <p/> * <p/>
* if <code>maxIdleTime</code> and <code>ttl</code> params are equal to <code>0</code> * if <code>maxIdleTime</code> and <code>ttl</code> params are equal to <code>0</code>
* then entry stores infinitely. * then entry stores infinitely.
*/ */
public CacheConfig(long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { public CacheConfig(long ttl, long maxIdleTime) {
super(); super();
this.ttl = ttl; this.ttl = ttl;
this.ttlUnit = ttlUnit;
this.maxIdleTime = maxIdleTime; this.maxIdleTime = maxIdleTime;
this.maxIdleUnit = maxIdleUnit;
} }
public long getTTL() { public long getTTL() {
@ -73,44 +66,29 @@ public class CacheConfig {
} }
/** /**
* Set time to live for key\value entry. * Set time to live for key\value entry in milliseconds.
* *
* @param ttl - time to live for key\value entry. * @param ttl - time to live for key\value entry in milliseconds.
* If <code>0</code> then time to live doesn't affect entry expiration. * If <code>0</code> then time to live doesn't affect entry expiration.
*/ */
public void setTTL(long ttl) { public void setTTL(long ttl) {
this.ttl = ttl; this.ttl = ttl;
} }
public TimeUnit getTTLUnit() {
return ttlUnit;
}
public void setTTLUnit(TimeUnit ttlUnit) {
this.ttlUnit = ttlUnit;
}
public long getMaxIdleTime() { public long getMaxIdleTime() {
return maxIdleTime; return maxIdleTime;
} }
/** /**
* Set max idle time for key\value entry. * Set max idle time for key\value entry in milliseconds.
* *
* @param maxIdleTime - max idle time for key\value entry. * @param maxIdleTime - max idle time for key\value entry in milliseconds.
* If <code>0</code> then max idle time doesn't affect entry expiration. * If <code>0</code> then max idle time doesn't affect entry expiration.
*/ */
public void setMaxIdleTime(long maxIdleTime) { public void setMaxIdleTime(long maxIdleTime) {
this.maxIdleTime = maxIdleTime; this.maxIdleTime = maxIdleTime;
} }
public TimeUnit getMaxIdleUnit() {
return maxIdleUnit;
}
public void setMaxIdleUnit(TimeUnit maxIdleUnit) {
this.maxIdleUnit = maxIdleUnit;
}
/** /**
* Read config objects stored in JSON format from <code>String</code> * Read config objects stored in JSON format from <code>String</code>
* *

@ -15,6 +15,8 @@
*/ */
package org.redisson.spring.cache; package org.redisson.spring.cache;
import java.util.concurrent.TimeUnit;
import org.redisson.core.RMap; import org.redisson.core.RMap;
import org.redisson.core.RMapCache; import org.redisson.core.RMapCache;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
@ -75,7 +77,7 @@ public class RedissonCache implements Cache {
@Override @Override
public void put(Object key, Object value) { public void put(Object key, Object value) {
if (mapCache != null) { if (mapCache != null) {
mapCache.fastPut(key, value, config.getTTL(), config.getTTLUnit(), config.getMaxIdleTime(), config.getMaxIdleUnit()); mapCache.fastPut(key, value, config.getTTL(), TimeUnit.MILLISECONDS, config.getMaxIdleTime(), TimeUnit.MILLISECONDS);
} else { } else {
map.fastPut(key, value); map.fastPut(key, value);
} }
@ -84,7 +86,7 @@ public class RedissonCache implements Cache {
public ValueWrapper putIfAbsent(Object key, Object value) { public ValueWrapper putIfAbsent(Object key, Object value) {
Object prevValue; Object prevValue;
if (mapCache != null) { if (mapCache != null) {
prevValue = mapCache.putIfAbsent(key, value, config.getTTL(), config.getTTLUnit(), config.getMaxIdleTime(), config.getMaxIdleUnit()); prevValue = mapCache.putIfAbsent(key, value, config.getTTL(), TimeUnit.MILLISECONDS, config.getMaxIdleTime(), TimeUnit.MILLISECONDS);
} else { } else {
prevValue = map.putIfAbsent(key, value); prevValue = map.putIfAbsent(key, value);
} }

@ -2,6 +2,7 @@ package org.redisson.spring.cache;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -88,9 +89,9 @@ public class RedissonSpringCacheTest {
} }
@Bean @Bean
CacheManager cacheManager(RedissonClient redissonClient) { CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
Map<String, CacheConfig> config = new HashMap<String, CacheConfig>(); Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
config.put("testMap", new CacheConfig(24, TimeUnit.HOURS, 12, TimeUnit.SECONDS)); config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
return new RedissonCacheManager(redissonClient, config); return new RedissonCacheManager(redissonClient, config);
} }

Loading…
Cancel
Save