DATACOUCH-86 - Implement Spring4 cache get with cast.
This changeset extends the CouchbaseCache to be compatible with the newly added method of the Spring 4 cache interface.
This commit is contained in:
@@ -81,6 +81,12 @@ public class CouchbaseCache implements Cache {
|
||||
return (result != null ? new SimpleValueWrapper(result) : null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public final <T> T get(final Object key, final Class<T> clazz) {
|
||||
String documentId = key.toString();
|
||||
return (T) client.get(documentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a object in Couchbase.
|
||||
*
|
||||
|
||||
@@ -25,6 +25,8 @@ import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -77,6 +79,21 @@ public class CouchbaseCacheTests {
|
||||
assertEquals(value, loaded.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSetWithCast() {
|
||||
CouchbaseCache cache = new CouchbaseCache(cacheName, client);
|
||||
|
||||
String key = "couchbase-cache-user";
|
||||
User user = new User();
|
||||
user.firstname = "Michael";
|
||||
|
||||
cache.put(key, user);
|
||||
|
||||
User loaded = cache.get(key, User.class);
|
||||
assertNotNull(loaded);
|
||||
assertEquals(user.firstname, loaded.firstname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the deletion of cache objects.
|
||||
*
|
||||
@@ -114,4 +131,8 @@ public class CouchbaseCacheTests {
|
||||
assertNull(cache.get(key));
|
||||
}
|
||||
|
||||
static class User implements Serializable {
|
||||
public String firstname;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user