Skip to content

Commit dcd0a18

Browse files
committed
Fix update to cache documentation. (#1359)
Closes #1358.
1 parent 416e770 commit dcd0a18

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/main/asciidoc/caching.adoc

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[[couchbase.caching]]
2+
= Caching
3+
4+
This chapter describes additional support for caching and `@Cacheable`.
5+
6+
[[caching.usage]]
7+
== Configuration & Usage
8+
9+
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.
10+
11+
Couchbase supports both binary and JSON data, so you can get both out of the same database.
12+
13+
To make it work, you need to add the `@EnableCaching` annotation and configure the `cacheManager` bean:
14+
15+
.`AbstractCouchbaseConfiguration` for Caching
16+
====
17+
[source,java]
18+
----
19+
20+
@Configuration
21+
@EnableCaching
22+
public class Config extends AbstractCouchbaseConfiguration {
23+
// general methods
24+
25+
@Bean
26+
public CouchbaseCacheManager cacheManager(CouchbaseTemplate couchbaseTemplate) throws Exception {
27+
CouchbaseCacheManager.CouchbaseCacheManagerBuilder builder = CouchbaseCacheManager.CouchbaseCacheManagerBuilder
28+
.fromConnectionFactory(couchbaseTemplate.getCouchbaseClientFactory());
29+
return builder.build();
30+
}
31+
32+
----
33+
====
34+
35+
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).
36+
37+
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.
38+
39+
.Caching example
40+
====
41+
[source,java]
42+
----
43+
@Cacheable(value="persistent", key="'longrunsim-'+#time")
44+
public String simulateLongRun(long time) {
45+
try {
46+
Thread.sleep(time);
47+
} catch(Exception ex) {
48+
System.out.println("This shouldnt happen...");
49+
}
50+
return "I've slept " + time + " miliseconds.;
51+
}
52+
----
53+
====
54+
55+
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.
56+
57+
Note that to use cache.clear() or catch.invalidate(), the bucket must have a primary key.

src/test/java/org/springframework/data/couchbase/domain/Config.java

+12
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
package org.springframework.data.couchbase.domain;
1818

1919
import java.lang.reflect.InvocationTargetException;
20+
import java.util.HashMap;
21+
import java.util.Map;
2022

23+
import org.springframework.cache.annotation.EnableCaching;
2124
import org.springframework.context.annotation.Bean;
2225
import org.springframework.context.annotation.Configuration;
2326
import org.springframework.data.auditing.DateTimeProvider;
2427
import org.springframework.data.couchbase.CouchbaseClientFactory;
2528
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
29+
import org.springframework.data.couchbase.cache.CouchbaseCacheConfiguration;
30+
import org.springframework.data.couchbase.cache.CouchbaseCacheManager;
2631
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
2732
import org.springframework.data.couchbase.core.CouchbaseTemplate;
2833
import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate;
@@ -214,6 +219,13 @@ public TranslationService couchbaseTranslationService() {
214219
return jacksonTranslationService;
215220
}
216221

222+
@Bean
223+
public CouchbaseCacheManager cacheManager(CouchbaseTemplate couchbaseTemplate) throws Exception {
224+
CouchbaseCacheManager.CouchbaseCacheManagerBuilder builder = CouchbaseCacheManager.CouchbaseCacheManagerBuilder
225+
.fromConnectionFactory(couchbaseTemplate.getCouchbaseClientFactory());
226+
return builder.build();
227+
}
228+
217229
@Override
218230
public String typeKey() {
219231
return "t"; // this will override '_class', is passed in to new CustomMappingCouchbaseConverter

0 commit comments

Comments
 (0)