Skip to content

Commit 366a333

Browse files
committed
prepare project structure, import some files from generation 1
the documentation will have to be revisited and has been put into a "old-to-migrate" subfolder. the resources is lacking Spring specific manifests among other things.
1 parent 274e26f commit 366a333

File tree

15 files changed

+1291
-0
lines changed

15 files changed

+1291
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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() throws Exception {
27+
HashMap<String, CouchbaseClient> instances = new HashMap<String, CouchbaseClient>();
28+
instances.put("persistent", couchbaseClient());
29+
return new CouchbaseCacheManager(instances);
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+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
[[couchbase.configuration]]
2+
= Installation & Configuration
3+
4+
This chapter describes the common installation and configuration steps needed when working with the library.
5+
6+
[[installation]]
7+
== Installation
8+
9+
All versions intented for production use are distributed across Maven Central and the Spring release repository. As a result, the library can be included like any other maven dependency:
10+
11+
.Including the dependency through maven
12+
====
13+
[source,xml]
14+
----
15+
<dependency>
16+
<groupId>org.springframework.data</groupId>
17+
<artifactId>spring-data-couchbase</artifactId>
18+
<version>1.0.0.RELEASE</version>
19+
</dependency>
20+
----
21+
====
22+
23+
This will pull in several dependencies, including the underlying Couchbase Java SDK, common Spring dependencies and also Jackson as the JSON mapping infrastructure.
24+
25+
You can also grab snapshots from the http://repo.spring.io/libs-snapshot[spring snapshot repository] and milestone releases from the http://repo.spring.io/libs-milestone[milestone repository]. Here is an example on how to use the current SNAPSHOT dependency:
26+
27+
.Using a snapshot version
28+
====
29+
[source,xml]
30+
----
31+
<dependency>
32+
<groupId>org.springframework.data</groupId>
33+
<artifactId>spring-data-couchbase</artifactId>
34+
<version>1.1.0.BUILD-SNAPSHOT</version>
35+
</dependency>
36+
37+
<repository>
38+
<id>spring-libs-snapshot</id>
39+
<name>Spring Snapshot Repository</name>
40+
<url>https://repo.spring.io/libs-snapshot</url>
41+
</repository>
42+
----
43+
====
44+
45+
Once you have all needed dependencies on the classpath, you can start configuring it. Both Java and XML config are supported. The next sections describe both approaches in detail.
46+
47+
[[configuration-java]]
48+
== Annotation-based Configuration ("JavaConfig")
49+
50+
The annotation based configuration approach is getting more and more popular. It allows you to get rid of XML configuration and treat configuration as part of your code directly. To get started, all you need to do is sublcass the `AbstractCouchbaseConfiguration` and implement the abstract methods.
51+
52+
Please make sure to have cglib support in the classpath so that the annotation based configuration works.
53+
54+
.Extending the `AbstractCouchbaseConfiguration`
55+
====
56+
[source,java]
57+
----
58+
59+
@Configuration
60+
public class Config extends AbstractCouchbaseConfiguration {
61+
62+
@Override
63+
protected List<String> bootstrapHosts() {
64+
return Collections.singletonList("127.0.0.1");
65+
}
66+
67+
@Override
68+
protected String getBucketName() {
69+
return "beer-sample";
70+
}
71+
72+
@Override
73+
protected String getBucketPassword() {
74+
return "";
75+
}
76+
}
77+
----
78+
====
79+
80+
All you need to provide is a list of Couchbase nodes to bootstrap into (without any ports, just the IP address or hostname). Please note that while one host is sufficient in development, it is recommended to add 3 to 5 bootstrap nodes here. Couchbase will pick up all nodes from the cluster automatically, but it could be the case that the only node you've provided is experiencing issues while you are starting the application.
81+
82+
The `bucketName` and `password` should be the same as configured in Couchbase Server itself. In the example given, we are connecting to the `beer-sample` bucket which is one of the sample buckets shipped with Couchbase Server and has no password set by default.
83+
84+
Depending on how your environment is setup, the configuration will be automatically picked up by the context or you need to instantiate your own one. How to manage configurations is not scope of this manual, please refer to the spring documentation for more information on that topic.
85+
86+
While not immediately obvious, much more things can be customized and overridden as custom beans from this configuration - we'll touch them in the individual manual sections as needed (for example repositories, validation and custom converters).
87+
88+
[[configuration-xml]]
89+
== XML-based Configuration
90+
91+
The library provides a custom namespace that you can use in your XML configuration:
92+
93+
.Basic XML configuration
94+
====
95+
[source,xml]
96+
----
97+
<?xml version="1.0" encoding="UTF-8"?>
98+
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
99+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
100+
xmlns="http://www.springframework.org/schema/data/couchbase
101+
xsi:schemaLocation="http://www.springframework.org/schema/beans
102+
http://www.springframework.org/schema/beans/spring-beans.xsd
103+
http://www.springframework.org/schema/data/couchbase
104+
http://www.springframework.org/schema/data/couchbase/spring-couchbase.xsd">
105+
106+
<couchbase:couchbase bucket="beer-sample" password="" host="127.0.0.1" />
107+
108+
</beans:beans>
109+
----
110+
====
111+
This code is equivalent to the java configuration approach shown above. It is also possible to configure templates and repositories, which is shown in the appropriate sections.
112+
113+
If you start your application, you should see Couchbase INFO level logging in the logs, indicating that the underlying Couchbase Java SDK is connecting to the database. If any errors are reported, make sure that the given credentials and host information is correct.
114+

0 commit comments

Comments
 (0)