Skip to content

Commit 546e40c

Browse files
committed
Redis support fix #56
It is pretty straightforward: ```properties db = "redis://localhost:6379" ``` ```java { use(new Redis()); get("/:key/:value", req -> { try (Jedis jedis = req.require(Jedis.class)) { jedis.set(req.param("key").value(), req.param("value").value()); return jedis.get(req.param("key").value()); } }); } ``` This module creates a [JedisPool]. A default pool is created with a max of ```128``` instances. The pool can be customized from your ```application.conf```: ```properties db = "redis://localhost:6379" jedis.pool.maxTotal = 200 ``` In case you need two or more Redis connection, just do: ```java { use(new Redis()); // default is "db" use(new Redis("db1")); get("/:key/:value", req {@literal ->} { try (Jedis jedis = req.require("db1", Jedis.class)) { jedis.set(req.param("key").value(), req.param("value").value()); return jedis.get(req.param("key").value()); } }); } ``` application.conf: ``` db = "redis://localhost:6379/0" db1 = "redis://localhost:6379/1" ``` Pool configuration for ```db1``` is inherited from ```jedis.pool```. If you need to tweak the pool configuration for ```db1``` just do: ```properties db1 = "redis://localhost:6379/1" jedis.db1.maxTotal = 10 ``` For more information about [Jedis](https://github.com/xetorthio/jedis) checkout the [wiki](https://github.com/xetorthio/jedis/wiki) That's all folks! Enjoy it!
1 parent a2bc7b2 commit 546e40c

13 files changed

Lines changed: 998 additions & 0 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ jdk:
66
- oraclejdk8
77
after_success:
88
- mvn -Dlogback.configurationFile=logback-travis.xml clean package coveralls:report -P coverage
9+
services:
10+
- redis-server

coverage-report/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<source>${project.parent.basedir}/jooby-jdbi/src/main/java</source>
4747
<source>${project.parent.basedir}/jooby-ftl/src/main/java</source>
4848
<source>${project.parent.basedir}/jooby-camel/src/main/java</source>
49+
<source>${project.parent.basedir}/jooby-jedis/src/main/java</source>
4950
</sources>
5051
</configuration>
5152
</execution>
@@ -70,6 +71,7 @@
7071
<source>${project.parent.basedir}/jooby-jdbi/src/test/java</source>
7172
<source>${project.parent.basedir}/jooby-ftl/src/test/java</source>
7273
<source>${project.parent.basedir}/jooby-camel/src/test/java</source>
74+
<source>${project.parent.basedir}/jooby-jedis/src/test/java</source>
7375
</sources>
7476
</configuration>
7577
</execution>
@@ -174,6 +176,12 @@
174176
<version>${project.version}</version>
175177
</dependency>
176178

179+
<dependency>
180+
<groupId>org.jooby</groupId>
181+
<artifactId>jooby-jedis</artifactId>
182+
<version>${project.version}</version>
183+
</dependency>
184+
177185
<!-- H2 database -->
178186
<dependency>
179187
<groupId>com.h2database</groupId>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.jooby.jedis;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
import redis.clients.jedis.Jedis;
7+
8+
import com.google.inject.Key;
9+
import com.google.inject.name.Names;
10+
import com.typesafe.config.ConfigFactory;
11+
import com.typesafe.config.ConfigValueFactory;
12+
13+
public class JedisCustomDbPropertyFeature extends ServerFeature {
14+
15+
{
16+
17+
use(ConfigFactory.empty()
18+
.withValue("mydb", ConfigValueFactory.fromAnyRef("redis://localhost:6379"))
19+
.withValue("jedis.mydb.maxTotal", ConfigValueFactory.fromAnyRef(8)));
20+
21+
22+
use(new Redis("mydb"));
23+
24+
get("/:key/:value", req -> {
25+
try (Jedis jedis = req.require(Key.get(Jedis.class, Names.named("mydb")))) {
26+
jedis.set(req.param("key").value(), req.param("value").value());
27+
return jedis.get(req.param("key").value());
28+
}
29+
});
30+
}
31+
32+
@Test
33+
public void connect() throws Exception {
34+
request()
35+
.get("/foo/bar")
36+
.expect("bar");
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.jooby.jedis;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
import redis.clients.jedis.Jedis;
7+
8+
import com.typesafe.config.ConfigFactory;
9+
import com.typesafe.config.ConfigValueFactory;
10+
11+
public class JedisFeature extends ServerFeature {
12+
13+
{
14+
15+
use(ConfigFactory.empty()
16+
.withValue("db", ConfigValueFactory.fromAnyRef("redis://localhost:6379")));
17+
18+
use(new Redis());
19+
20+
get("/:key/:value", req -> {
21+
try (Jedis jedis = req.require(Jedis.class)) {
22+
jedis.set(req.param("key").value(), req.param("value").value());
23+
return jedis.get(req.param("key").value());
24+
}
25+
});
26+
}
27+
28+
@Test
29+
public void connect() throws Exception {
30+
request()
31+
.get("/foo/bar")
32+
.expect("bar");
33+
}
34+
}

jooby-jedis/pom.xml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<parent>
6+
<groupId>org.jooby</groupId>
7+
<artifactId>jooby-project</artifactId>
8+
<version>0.5.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>jooby-jedis</artifactId>
13+
14+
<name>jedis module</name>
15+
<description>Redis key-value cache and store for Jooby</description>
16+
17+
<build>
18+
<plugins>
19+
<!-- sure-fire -->
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-surefire-plugin</artifactId>
23+
<configuration>
24+
<includes>
25+
<include>**/*Test.java</include>
26+
<include>**/*Feature.java</include>
27+
<include>**/Issue*.java</include>
28+
</includes>
29+
</configuration>
30+
</plugin>
31+
32+
</plugins>
33+
</build>
34+
35+
<dependencies>
36+
<!-- Jooby -->
37+
<dependency>
38+
<groupId>org.jooby</groupId>
39+
<artifactId>jooby</artifactId>
40+
<version>${project.version}</version>
41+
</dependency>
42+
43+
<!-- Jedis -->
44+
<dependency>
45+
<groupId>redis.clients</groupId>
46+
<artifactId>jedis</artifactId>
47+
</dependency>
48+
49+
<!-- Test dependencies -->
50+
<dependency>
51+
<groupId>org.jooby</groupId>
52+
<artifactId>jooby</artifactId>
53+
<version>${project.version}</version>
54+
<scope>test</scope>
55+
<classifier>tests</classifier>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>junit</groupId>
60+
<artifactId>junit</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>org.easymock</groupId>
66+
<artifactId>easymock</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>org.powermock</groupId>
72+
<artifactId>powermock-api-easymock</artifactId>
73+
<scope>test</scope>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>org.powermock</groupId>
78+
<artifactId>powermock-module-junit4</artifactId>
79+
<scope>test</scope>
80+
</dependency>
81+
82+
<dependency>
83+
<groupId>org.jacoco</groupId>
84+
<artifactId>org.jacoco.agent</artifactId>
85+
<classifier>runtime</classifier>
86+
<scope>test</scope>
87+
</dependency>
88+
89+
</dependencies>
90+
91+
</project>

0 commit comments

Comments
 (0)