public class Redis extends Object implements Jooby.Module
Jedis service.
It is pretty straightforward:
# define a database URI db = "redis://localhost:6379"
{
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:
db = "redis://localhost:6379" # increase pool size to 200 jedis.pool.maxTotal = 200
In case you need two or more Redis connection, just do:
{
use(new Redis()); // default is "db"
use(new Redis("db1"));
get("/:key/:value", req -> {
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:
db1 = "redis://localhost:6379/1" # ONLY 10 for db1 jedis.db1.maxTotal = 10
For more information about Jedis checkout the wiki
That's all folks! Enjoy it!
TBD: Object mapping? https://github.com/xetorthio/johm?| Constructor and Description |
|---|
Redis()
A new
Redis instance, with a default database name of: db. |
Redis(String name)
Creates a new
Redis instance and connect to the provided database. |
| Modifier and Type | Method and Description |
|---|---|
com.typesafe.config.Config |
config() |
void |
configure(Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
Redis |
named()
Bind
JedisPool and Jedis services with a Named annotation. |
Redis |
unnamed()
Bind
JedisPool and Jedis services without a Named annotation. |
public Redis(String name)
Creates a new Redis instance and connect to the provided database. Please note, the
name is a property in your application.conf file with Redis URI.
{
use(new Redis("db1"));
}
application.conf
db1 = ""redis://localhost:6379""Default database name is:
dbname - A database name.public Redis()
Redis instance, with a default database name of: db.public Redis named()
JedisPool and Jedis services with a Named annotation.public Redis unnamed()
JedisPool and Jedis services without a Named annotation.public void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
configure in interface Jooby.Modulepublic com.typesafe.config.Config config()
config in interface Jooby.ModuleCopyright © 2015. All rights reserved.