Redis cache and key/value data store for Jooby. exports a Jedis service.
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-jedis</artifactId>
<version>1.2.1</version>
</dependency>It is pretty straightforward:
# define a database URI
db = "redis://localhost:6379"{
use(new Redis());
get("/:key/:value", req -> {
try (Jedis jedis = 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 = 200In 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 = 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 = 10For more information about Jedis checkout the wiki
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-jedis</artifactId>
<version>1.2.1</version>
</dependency>{
use(new Redis());
session(RedisSessionStore.class);
get("/", req -> {
req.session().set("name", "jooby");
});
}The name attribute and value will be stored in a Redis. Sessions are persisted as hashes.
By default, a Redis session will expire after 30 minutes. Changing the default timeout is as simple as:
# 8 hours
session.timeout = 8h
# 15 seconds
session.timeout = 15
# 120 minutes
session.timeout = 120m
# no timeout
session.timeout = -1Default redis key prefix is sessions. Sessions in [redis] will look like: sessions:ID
It's possible to change the default key setting the jedis.sesssion.prefix properties
That's all folks! Enjoy it!
TBD: Object mapping? https://github.com/xetorthio/johm?