DATACOUCH-25 - added support for TTL properties in Couchbase Cache Manager

This commit is contained in:
kkrol89
2015-02-07 12:50:21 +01:00
committed by Michael Nitschinger
parent 97f989c47c
commit ff15112959
4 changed files with 131 additions and 3 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.cache.support.SimpleValueWrapper;
* @see <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/cache.html">
* Official Spring Cache Reference</a>
* @author Michael Nitschinger
* @author Konrad Król
*/
public class CouchbaseCache implements Cache {
@@ -39,7 +40,12 @@ public class CouchbaseCache implements Cache {
* The name of the cache.
*/
private final String name;
/**
* TTL value for objects in this cache
*/
private final int ttl;
/**
* Construct the cache and pass in the CouchbaseClient instance.
*
@@ -49,6 +55,20 @@ public class CouchbaseCache implements Cache {
public CouchbaseCache(final String name, final CouchbaseClient client) {
this.name = name;
this.client = client;
this.ttl = 0;
}
/**
* Construct the cache and pass in the CouchbaseClient instance.
*
* @param name the name of the cache reference.
* @param client the CouchbaseClient instance.
* @param ttl TTL value for objects in this cache
*/
public CouchbaseCache(final String name, final CouchbaseClient client, int ttl) {
this.name = name;
this.client = client;
this.ttl = ttl;
}
/**
@@ -68,6 +88,15 @@ public class CouchbaseCache implements Cache {
public final CouchbaseClient getNativeCache() {
return client;
}
/**
* Returns the TTL value for this cache.
*
* @return TTL value
*/
public final int getTtl() {
return ttl;
}
/**
* Get an element from the cache.
@@ -96,7 +125,7 @@ public class CouchbaseCache implements Cache {
public final void put(final Object key, final Object value) {
if (value != null) {
String documentId = key.toString();
client.set(documentId, 0, value);
client.set(documentId, ttl, value);
} else {
evict(key);
}

View File

@@ -32,6 +32,7 @@ import java.util.Map;
* {@link CouchbaseCacheManager} orchestrates and handles them for the Spring Cache abstraction layer.
*
* @author Michael Nitschinger
* @author Konrad Król
*/
public class CouchbaseCacheManager extends AbstractCacheManager {
@@ -39,6 +40,11 @@ public class CouchbaseCacheManager extends AbstractCacheManager {
* Holds the reference to all stored CouchbaseClient cache connections.
*/
private final HashMap<String, CouchbaseClient> clients;
/**
* Holds the TTL configuration for each cache.
*/
private final HashMap<String, Integer> ttlConfiguration;
/**
* Construct a new CouchbaseCacheManager.
@@ -47,6 +53,18 @@ public class CouchbaseCacheManager extends AbstractCacheManager {
*/
public CouchbaseCacheManager(final HashMap<String, CouchbaseClient> clients) {
this.clients = clients;
this.ttlConfiguration = new HashMap<String, Integer>();
}
/**
* Construct a new CouchbaseCacheManager.
*
* @param clients one ore more CouchbaseClients to reference.
* @param ttlConfiguration one or more TTL values (in seconds)
*/
public CouchbaseCacheManager(final HashMap<String, CouchbaseClient> clients, final HashMap<String, Integer> ttlConfiguration) {
this.clients = clients;
this.ttlConfiguration = ttlConfiguration;
}
/**
@@ -68,10 +86,20 @@ public class CouchbaseCacheManager extends AbstractCacheManager {
Collection<Cache> caches = new LinkedHashSet<Cache>();
for (Map.Entry<String, CouchbaseClient> cache : clients.entrySet()) {
caches.add(new CouchbaseCache(cache.getKey(), cache.getValue()));
caches.add(new CouchbaseCache(cache.getKey(), cache.getValue(), getTtl(cache.getKey())));
}
return caches;
}
/**
* Returns TTL value for single cache
* @param name cache name
* @return either the cache TTL value or 0 as a default value
*/
private int getTtl ( String name ) {
Integer expirationTime = ttlConfiguration.get(name);
return (expirationTime != null ? expirationTime : 0);
}
}