diff --git a/src/main/asciidoc/caching.adoc b/src/main/asciidoc/caching.adoc new file mode 100644 index 000000000..5aacff6a5 --- /dev/null +++ b/src/main/asciidoc/caching.adoc @@ -0,0 +1,57 @@ +[[couchbase.caching]] += Caching + +This chapter describes additional support for caching and `@Cacheable`. + +[[caching.usage]] +== Configuration & Usage + +Technically, caching is not part of spring-data, but is implemented directly in the spring core. Most database implementations in the spring-data package can't support `@Cacheable`, because it is not possible to store arbitrary data. + +Couchbase supports both binary and JSON data, so you can get both out of the same database. + +To make it work, you need to add the `@EnableCaching` annotation and configure the `cacheManager` bean: + +.`AbstractCouchbaseConfiguration` for Caching +==== +[source,java] +---- + +@Configuration +@EnableCaching +public class Config extends AbstractCouchbaseConfiguration { + // general methods + + @Bean + public CouchbaseCacheManager cacheManager(CouchbaseTemplate couchbaseTemplate) throws Exception { + CouchbaseCacheManager.CouchbaseCacheManagerBuilder builder = CouchbaseCacheManager.CouchbaseCacheManagerBuilder + .fromConnectionFactory(couchbaseTemplate.getCouchbaseClientFactory()); + builder.withCacheConfiguration("mySpringCache", CouchbaseCacheConfiguration.defaultCacheConfig()); + return builder.build(); + } +---- +==== + +The `persistent` identifier can then be used on the `@Cacheable` annotation to identify the cache manager to use (you can have more than one configured). + +Once it is set up, you can annotate every method with the `@Cacheable` annotation to transparently cache it in your couchbase bucket. You can also customize how the key is generated. + +.Caching example +==== +[source,java] +---- +@Cacheable(value="persistent", key="'longrunsim-'+#time") +public String simulateLongRun(long time) { + try { + Thread.sleep(time); + } catch(Exception ex) { + System.out.println("This shouldnt happen..."); + } + return "I've slept " + time + " miliseconds.; +} +---- +==== + +If you run the method multiple times, you'll see a set operation happening first, followed by multiple get operations and no sleep time (which fakes the expensive execution). You can store whatever you want, if it is JSON of course you can access it through views and look at it in the Web UI. + +Note that to use cache.clear() or catch.invalidate(), the bucket must have a primary key. diff --git a/src/main/asciidoc/index.adoc b/src/main/asciidoc/index.adoc index 650779563..96f910bdd 100644 --- a/src/main/asciidoc/index.adoc +++ b/src/main/asciidoc/index.adoc @@ -26,6 +26,7 @@ include::template.adoc[] include::transactions.adoc[] include::collections.adoc[] include::ansijoins.adoc[] +include::caching.adoc[] :leveloffset: -1 [[appendix]] diff --git a/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java index f434f9ca2..65aea1e39 100644 --- a/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java @@ -36,7 +36,7 @@ * * @author Michael Reiche */ -@IgnoreWhen(clusterTypes = ClusterType.MOCKED, missesCapabilities = { Capabilities.COLLECTIONS }) +@IgnoreWhen(clusterTypes = ClusterType.MOCKED, missesCapabilities = { Capabilities.COLLECTIONS }) class CouchbaseCacheCollectionIntegrationTests extends CollectionAwareIntegrationTests { volatile CouchbaseCache cache; @@ -58,7 +58,6 @@ private void clear(CouchbaseCache c) { QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)); } - @Test void cachePutGet() { CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1"); diff --git a/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java index 8dadb39c3..1500fa810 100644 --- a/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java @@ -21,10 +21,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import java.util.List; import java.util.UUID; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.couchbase.domain.Config; +import org.springframework.data.couchbase.domain.User; +import org.springframework.data.couchbase.domain.UserRepository; import org.springframework.data.couchbase.util.ClusterType; import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.data.couchbase.util.JavaIntegrationTests; @@ -40,6 +48,8 @@ class CouchbaseCacheIntegrationTests extends JavaIntegrationTests { volatile CouchbaseCache cache; + @Autowired CouchbaseCacheManager cacheManager; // autowired not working + @Autowired UserRepository userRepository; // autowired not working @BeforeEach @Override @@ -48,6 +58,16 @@ public void beforeEach() { cache = CouchbaseCacheManager.create(couchbaseTemplate.getCouchbaseClientFactory()).createCouchbaseCache("myCache", CouchbaseCacheConfiguration.defaultCacheConfig()); clear(cache); + ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); + cacheManager = ac.getBean(CouchbaseCacheManager.class); + userRepository = ac.getBean(UserRepository.class); + } + + @AfterEach + @Override + public void afterEach() { + clear(cache); + super.afterEach(); } private void clear(CouchbaseCache c) { @@ -69,6 +89,19 @@ void cachePutGet() { assertEquals(user2, cache.get(user2.getId()).get()); // get user2 } + @Test + void cacheable() { + User user = new User("cache_92", "Dave", "Wilson"); + cacheManager.getCache("mySpringCache").clear(); + userRepository.save(user); + long t0 = System.currentTimeMillis(); + List users = userRepository.getByFirstname(user.getFirstname()); + assert (System.currentTimeMillis() - t0 > 1000 * 5); + t0 = System.currentTimeMillis(); + users = userRepository.getByFirstname(user.getFirstname()); + assert (System.currentTimeMillis() - t0 < 100); + } + @Test void cacheEvict() { CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1"); @@ -98,4 +131,22 @@ void cachePutIfAbsent() { assertEquals(user1, cache.get(user1.getId()).get()); // user1.getId() is still user1 } + @Test // this test FAILS (local empty (i.e. fast) Couchbase installation) + public void clearFail() { + cache.put("KEY", "VALUE"); // no delay between put and clear, entry will not be + cache.clear(); // will not be indexed when clear() executes + assertNotNull(cache.get("KEY")); // will still find entry, clear failed to delete + } + + @Test // this WORKS + public void clearWithDelayOk() throws InterruptedException { + cache.put("KEY", "VALUE"); + Thread.sleep(50); // give main index time to update + cache.clear(); + assertNull(cache.get("KEY")); + } + + @Test + public void noOpt() {} + } diff --git a/src/test/java/org/springframework/data/couchbase/domain/Config.java b/src/test/java/org/springframework/data/couchbase/domain/Config.java index bfedc1b9f..2966bbeec 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/Config.java +++ b/src/test/java/org/springframework/data/couchbase/domain/Config.java @@ -17,12 +17,17 @@ package org.springframework.data.couchbase.domain; import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.auditing.DateTimeProvider; import org.springframework.data.couchbase.CouchbaseClientFactory; import org.springframework.data.couchbase.SimpleCouchbaseClientFactory; +import org.springframework.data.couchbase.cache.CouchbaseCacheConfiguration; +import org.springframework.data.couchbase.cache.CouchbaseCacheManager; import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; @@ -53,7 +58,7 @@ @EnableReactiveCouchbaseRepositories @EnableCouchbaseAuditing(dateTimeProviderRef = "dateTimeProviderRef") @EnableReactiveCouchbaseAuditing(dateTimeProviderRef = "dateTimeProviderRef") - +@EnableCaching public class Config extends AbstractCouchbaseConfiguration { String bucketname = "travel-sample"; String username = "Administrator"; @@ -214,6 +219,14 @@ public TranslationService couchbaseTranslationService() { return jacksonTranslationService; } + @Bean + public CouchbaseCacheManager cacheManager(CouchbaseTemplate couchbaseTemplate) throws Exception { + CouchbaseCacheManager.CouchbaseCacheManagerBuilder builder = CouchbaseCacheManager.CouchbaseCacheManagerBuilder + .fromConnectionFactory(couchbaseTemplate.getCouchbaseClientFactory()); + //CouchbaseCacheConfiguration cfg = CouchbaseCacheConfiguration.defaultCacheConfig().withCacheConfiguration("mySpringCache", CouchbaseCacheConfiguration.defaultCacheConfig()); + return builder.build(); + } + @Override public String typeKey() { return "t"; // this will override '_class', is passed in to new CustomMappingCouchbaseConverter diff --git a/src/test/java/org/springframework/data/couchbase/domain/User.java b/src/test/java/org/springframework/data/couchbase/domain/User.java index 55ca67ebf..c228e6bf4 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/User.java +++ b/src/test/java/org/springframework/data/couchbase/domain/User.java @@ -19,6 +19,8 @@ import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.couchbase.core.mapping.Document; +import java.io.Serializable; + /** * User entity for tests * @@ -27,7 +29,7 @@ */ @Document -public class User extends AbstractUser { +public class User extends AbstractUser implements Serializable { @PersistenceConstructor public User(final String id, final String firstname, final String lastname) { diff --git a/src/test/java/org/springframework/data/couchbase/domain/UserRepository.java b/src/test/java/org/springframework/data/couchbase/domain/UserRepository.java index 229ae00c0..31b5eab3c 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/UserRepository.java +++ b/src/test/java/org/springframework/data/couchbase/domain/UserRepository.java @@ -19,7 +19,7 @@ import java.util.List; import java.util.stream.Stream; -import com.couchbase.client.java.query.QueryScanConsistency; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.couchbase.repository.CouchbaseRepository; import org.springframework.data.couchbase.repository.Query; import org.springframework.data.couchbase.repository.ScanConsistency; @@ -27,6 +27,7 @@ import org.springframework.stereotype.Repository; import com.couchbase.client.java.json.JsonArray; +import com.couchbase.client.java.query.QueryScanConsistency; /** * User Repository for tests @@ -35,7 +36,7 @@ * @author Michael Reiche */ @Repository -@ScanConsistency(query=QueryScanConsistency.REQUEST_PLUS) +@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS) public interface UserRepository extends CouchbaseRepository { List findByFirstname(String firstname); @@ -57,4 +58,13 @@ public interface UserRepository extends CouchbaseRepository { List findByIdIsNotNullAndFirstnameEquals(String firstname); List findByVersionEqualsAndFirstnameEquals(Long version, String firstname); + + // simulate a slow operation + @Cacheable("mySpringCache") + default List getByFirstname(String firstname) { + try { + Thread.sleep(1000 * 5); + } catch (InterruptedException ie) {} + return findByFirstname(firstname); + } }