You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 {
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.
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.
Couchbase supports https://docs.couchbase.com/server/current/learn/data/scopes-and-collections.html[Scopes and Collections]. This section documents on how to use it with Spring Data Couchbase.
5
+
6
+
The https://github.com/couchbaselabs/try-cb-spring[try-cb-spring] sample application is a working example of using Scopes and Collections in Spring Data Couchbase.
7
+
8
+
The 2021 Couchbase Connect presentation on Collections in Spring Data can be found at https://www.youtube.com/watch?v=MrplTeEFItk[Presentation Only] and https://web.cvent.com/hub/events/1dce8283-986d-4de9-8368-94c98f60df01/sessions/9ee89a85-833c-4e0c-81b0-807864fa351b?goBackHref=%2Fevents%2F1dce8283-986d-4de9-8368-94c98f60df01%2Fsessions&goBackName=Add%2FView+Sessions&goBackTab=all[Presentation with Slide Deck]
9
+
10
+
== Requirements
11
+
12
+
- Couchbase Server 7.0 or above.
13
+
- Spring Data Couchbase 4.3.1 or above.
14
+
15
+
== Getting Started & Configuration
16
+
17
+
18
+
=== Scope and Collection Specification
19
+
There are several mechanisms of specifying scopes and collections, and these may be combined, or one mechanism may override another.
20
+
First some definitions for scopes and collections. An unspecified scope indicates that the default scope is to be used, likewise, an
21
+
unspecified collection indicates that the default collection is to be used.
22
+
There are only three combinations of scopes and collections that are valid. (1) the default scope and the default collection; (2) the default
23
+
scope and a non-default collection; and (3) a non-default scope and a non-default collection. It is not possible to have a non-default
24
+
scope and a default collection as non-default scopes do not contain a default collections, neither can one be created.
25
+
26
+
A scope can be specified in the configuration:
27
+
[source,java]
28
+
----
29
+
@Configuration
30
+
static class Config extends AbstractCouchbaseConfiguration {
31
+
32
+
// Usual Setup
33
+
@Override public String getConnectionString() { /* ... */ }
34
+
35
+
// optionally specify the scope in the Configuration
36
+
@Override
37
+
protected String getScopeName() {
38
+
return "myScope"; // or a variable etc.;
39
+
}
40
+
41
+
}
42
+
----
43
+
Scopes and Collections can be specified as annotations on entity classes and repositories:
44
+
[source,java]
45
+
----
46
+
@Document
47
+
@Scope("travel")
48
+
@Collection("airport")
49
+
public class Airport {...
50
+
----
51
+
52
+
[source,java]
53
+
----
54
+
@Scope("travel")
55
+
@Collection("airport")
56
+
public interface AirportRepository extends CouchbaseRepository<Airport, String> ...
57
+
----
58
+
59
+
Scopes and Collections can be specified on templates using the inScope(scopeName) and inCollection(collectionName) fluent APIs:
Scopes and Collections can be specified on repositories that extend DynamicProxyable using the withScope(scopeName) and withCollection(collectionName) APIs:
66
+
[source,java]
67
+
----
68
+
public interface AirportRepository extends CouchbaseRepository<Airport, String>, DynamicProxyable<AirportRepository>{...}
NOTE: Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
10
10
@@ -24,8 +24,9 @@ include::repository.adoc[]
24
24
include::reactiverepository.adoc[]
25
25
include::template.adoc[]
26
26
include::transactions.adoc[]
27
-
// (daschl) disabled the ansijoins docs since it is being overhauled for 4.x
0 commit comments