DATACOUCH-25 - added support for TTL properties in Couchbase Cache Manager
This commit is contained in:
committed by
Michael Nitschinger
parent
97f989c47c
commit
ff15112959
@@ -17,9 +17,11 @@
|
||||
package org.springframework.data.couchbase.cache;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -27,6 +29,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Verifies the correct functionality of the CouchbaseCacheManager.
|
||||
@@ -53,7 +56,55 @@ public class CouchbaseCacheManagerTests {
|
||||
instances.put("test", client);
|
||||
|
||||
CouchbaseCacheManager manager = new CouchbaseCacheManager(instances);
|
||||
manager.afterPropertiesSet();
|
||||
|
||||
assertEquals(instances, manager.getClients());
|
||||
|
||||
Cache cache = manager.getCache("test");
|
||||
|
||||
assertNotNull(cache);
|
||||
|
||||
assertEquals(cache.getClass(), CouchbaseCache.class);
|
||||
assertEquals(((CouchbaseCache) cache).getName(), "test");
|
||||
assertEquals(((CouchbaseCache) cache).getTtl(), 0); // default TTL value
|
||||
assertEquals(((CouchbaseCache) cache).getNativeCache(), client);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test cache creation with custom TTL values.
|
||||
*/
|
||||
@Test
|
||||
public void testCacheInitWithTtl() {
|
||||
HashMap<String, CouchbaseClient> instances = new HashMap<String, CouchbaseClient>();
|
||||
instances.put("cache1", client);
|
||||
instances.put("cache2", client);
|
||||
|
||||
HashMap<String, Integer> ttlConfiguration = new HashMap<String, Integer>();
|
||||
ttlConfiguration.put("cache1", 100);
|
||||
ttlConfiguration.put("cache2", 200);
|
||||
|
||||
CouchbaseCacheManager manager = new CouchbaseCacheManager(instances, ttlConfiguration);
|
||||
manager.afterPropertiesSet();
|
||||
|
||||
assertEquals(instances, manager.getClients());
|
||||
|
||||
Cache cache1 = manager.getCache("cache1");
|
||||
Cache cache2 = manager.getCache("cache2");
|
||||
|
||||
assertNotNull(cache1);
|
||||
assertNotNull(cache2);
|
||||
|
||||
assertEquals(cache1.getClass(), CouchbaseCache.class);
|
||||
assertEquals(cache2.getClass(), CouchbaseCache.class);
|
||||
|
||||
assertEquals(((CouchbaseCache) cache1).getName(), "cache1");
|
||||
assertEquals(((CouchbaseCache) cache2).getName(), "cache2");
|
||||
|
||||
assertEquals(((CouchbaseCache) cache1).getTtl(), 100);
|
||||
assertEquals(((CouchbaseCache) cache2).getTtl(), 200);
|
||||
|
||||
assertEquals(((CouchbaseCache) cache1).getNativeCache(), client);
|
||||
assertEquals(((CouchbaseCache) cache2).getNativeCache(), client);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.data.couchbase.cache;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -33,6 +34,7 @@ import static org.junit.Assert.*;
|
||||
* Tests the CouchbaseCache class and verifies its functionality.
|
||||
*
|
||||
* @author Michael Nitschinger
|
||||
* @author Konrad Król
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@@ -78,6 +80,24 @@ public class CouchbaseCacheTests {
|
||||
ValueWrapper loaded = cache.get(key);
|
||||
assertEquals(value, loaded.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies set() with TTL value.
|
||||
*/
|
||||
@Test
|
||||
public void testSetWithTtl() throws InterruptedException {
|
||||
CouchbaseCache cache = new CouchbaseCache(cacheName, client, 1); // cache for 1 second
|
||||
|
||||
String key = "couchbase-cache-test";
|
||||
String value = "Hello World!";
|
||||
cache.put(key, value);
|
||||
|
||||
// wait for TTL to expire (double time of TTL)
|
||||
Thread.sleep(2000);
|
||||
|
||||
String stored = (String) client.get(key);
|
||||
assertNull(stored);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSetWithCast() {
|
||||
|
||||
Reference in New Issue
Block a user