Skip to content

Commit f946cba

Browse files
committed
Redis Client and HTTP Session
- Add redis module fix #1763 - Add redis session store fix #1746 - Minor updates to public documentation
1 parent 75943b6 commit f946cba

File tree

16 files changed

+2978
-2329
lines changed

16 files changed

+2978
-2329
lines changed

docs/asciidoc/index.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ Source code is available at https://github.com/jooby-project/jooby[GitHub]
6868

6969
=== Features
7070

71-
* Lightweight and fast. See https://www.techempower.com/benchmarks/#section=data-r18&hw=ph&test=fortune&l=zik0vz-f[Tech Empower Benchmark]
72-
* Fast startup time and <<hot-reload, Hot-Reload for development>> using Maven or Gradle
71+
* Lightweight and Fast. See https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=composite&l=xan9tr-1r[Tech Empower Benchmark]
72+
* Increase productivity with <<hot-reload, Hot-Reload for development>>
7373
* Script/lambda routes using fluent API
7474
* <<mvc-api, MVC routes>> using Jooby or JAX-RS annotations
7575
* link:modules/openapi[OpenAPI 3] supports
7676
* <<execution-model, Event Loop and blocking execution modes>>
7777
* <<responses, Reactive responses>> (Completable Futures, RxJava and Reactor types and Kotlin Coroutines)
7878
* <<server, Multi-server>> including https://www.eclipse.org/jetty[Jetty], https://netty.io[Netty] and http://undertow.io[Undertow]
79-
* Simple <<extensions-and-services, extension/plugin mechanism>> with a variety of <<modules, modules>>
79+
* Jump to full-stack framework with extension/plugin mechanism and variety of link:modules[]
8080

8181
=== Script API
8282

docs/asciidoc/modules.adoc

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1 @@
1-
== Modules
2-
3-
Modules are a key concept for building reusable and configurable pieces of software.
4-
5-
Modules (unlike in other frameworks) are thin and do a lot of work to bootstrap and configure an
6-
external library, but they DO NOT provide a new level of abstraction nor [do] they provide a custom
7-
API to access functionality in that library. Instead they expose the library components as they are.
8-
9-
Modules are distributed as separated jar/dependency and usually implement the javadoc:Extension[] API.
10-
11-
In general they provide a `builder` class to create the and configure the external library from
12-
configuration properties.
13-
14-
Available modules are listed next.
15-
16-
=== Cloud
17-
* link:modules/aws[AWS]: Amazon Web Service module.
18-
19-
=== Data
20-
* link:modules/ebean[Ebean]: Ebean ORM module.
21-
* link:modules/flyway[Flyway]: Flyway migration module.
22-
* link:modules/graphql[GraphQL]: GraphQL Java module.
23-
* link:modules/hikari[HikariCP]: A high-performance JDBC connection pool.
24-
* link:modules/hibernate[Hibernate]: Hibernate ORM module.
25-
* link:modules/jdbi[Jdbi]: Jdbi module.
26-
27-
=== Development Tools
28-
* link:#hot-reload[Jooby Run]: Run and hot reload your application.
29-
* link:modules/node[Node]: Download, Install and Run node locally.
30-
* link:modules/whoops[Whoops]: Pretty page stacktrace reporter.
31-
32-
=== JSON
33-
* link:modules/gson[Gson]: Gson module for Jooby.
34-
* link:modules/jackson[Jackson]: Jackson module for Jooby.
35-
36-
=== OpenAPI
37-
* link:modules/openapi[OpenAPI]: OpenAPI supports.
38-
39-
=== Template Engine
40-
* link:modules/handlebars[Handlebars]: Handlebars template engine.
41-
* link:modules/freemarker[Freemarker]: Freemarker template engine.
42-
* link:modules/pebble[Pebble]: Pebble template engine.
43-
* link:modules/rocker[Rocker]: Rocker template engine.
44-
* link:modules/thymeleaf[Thymeleaf]: Thymeleaf template engine.
45-
46-
=== Security
47-
* link:modules/pac4j[Pac4j]: Security engine module.
48-
49-
=== Scheduler
50-
* link:modules/quartz[Quartz]: Quartz scheduler module.
51-
52-
.
1+
include::modules/modules.adoc[]

docs/asciidoc/modules/ebean.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ import io.jooby.ebean.EbeanModule
9898
----
9999

100100
<1> Install and creates a `DataSource`
101-
<2> Install and Ebean
101+
<2> Install Ebean
102102
<3> Use Ebean Database
103103

104104
=== Transactional Request
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
=== JWT Session
2+
3+
The javadoc:JWTSession[] session store works it also a stateless session that
4+
uses https://jwt.io[JSON Web Token] standard to decode/encode data.
5+
6+
To use the javadoc:JWTSession[] session store you need to add the `jooby-jwt` dependency:
7+
8+
[dependency, artifactId="jooby-jwt"]
9+
.
10+
11+
.JWT Session
12+
[source,java,role="primary"]
13+
----
14+
15+
import io.jooby.session.JwtSessionStore;
16+
17+
{
18+
String secret = "super secret key"; // <1>
19+
20+
setSessionStore(new JwtSessionStore(secret)); // <2>
21+
22+
get("/", ctx -> {
23+
Session session = ctx.session();
24+
25+
session.put("foo", "bar");
26+
27+
return session.get("foo").value();
28+
});
29+
}
30+
----
31+
32+
.Kotlin
33+
[source,kotlin,role="secondary"]
34+
----
35+
36+
import io.jooby.session.JwtSessionStore
37+
38+
{
39+
val secret = "super secret key" // <1>
40+
41+
sessionStore = JwtSessionStore(secret) // <2>
42+
43+
get("/") {
44+
val session = ctx.session()
45+
46+
session.put("foo", "bar")
47+
48+
session.get("foo").value()
49+
}
50+
}
51+
----
52+
53+
<1> Set a secret key
54+
<2> Use `JwtSessionStore`

docs/asciidoc/modules/modules.adoc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
== Modules
2+
3+
Modules are a key concept for building reusable and configurable pieces of software.
4+
5+
Modules (unlike in other frameworks) are thin and do a lot of work to bootstrap and configure an
6+
external library, but they DO NOT provide a new level of abstraction nor [do] they provide a custom
7+
API to access functionality in that library. Instead they expose the library components as they are.
8+
9+
Modules are distributed as separated jar/dependency and usually implement the javadoc:Extension[] API.
10+
11+
In general they provide a `builder` class to create the and configure the external library from
12+
configuration properties.
13+
14+
Available modules are listed next.
15+
16+
=== Cloud
17+
* link:modules/aws[AWS]: Amazon Web Service module.
18+
19+
=== Data
20+
* link:modules/ebean[Ebean]: Ebean ORM module.
21+
* link:modules/flyway[Flyway]: Flyway migration module.
22+
* link:modules/graphql[GraphQL]: GraphQL Java module.
23+
* link:modules/hikari[HikariCP]: A high-performance JDBC connection pool.
24+
* link:modules/hibernate[Hibernate]: Hibernate ORM module.
25+
* link:modules/jdbi[Jdbi]: Jdbi module.
26+
* link:modules/redis[Redis]: Redis module.
27+
28+
=== Development Tools
29+
* link:#hot-reload[Jooby Run]: Run and hot reload your application.
30+
* link:modules/node[Node]: Download, Install and Run node locally.
31+
* link:modules/whoops[Whoops]: Pretty page stacktrace reporter.
32+
33+
=== JSON
34+
* link:modules/gson[Gson]: Gson module for Jooby.
35+
* link:modules/jackson[Jackson]: Jackson module for Jooby.
36+
37+
=== OpenAPI
38+
* link:modules/openapi[OpenAPI]: OpenAPI supports.
39+
40+
=== Template Engine
41+
* link:modules/handlebars[Handlebars]: Handlebars template engine.
42+
* link:modules/freemarker[Freemarker]: Freemarker template engine.
43+
* link:modules/pebble[Pebble]: Pebble template engine.
44+
* link:modules/rocker[Rocker]: Rocker template engine.
45+
* link:modules/thymeleaf[Thymeleaf]: Thymeleaf template engine.
46+
47+
=== Security
48+
* link:modules/pac4j[Pac4j]: Security engine module.
49+
50+
=== Session Store
51+
* link:modules/jwt-session-store[JWT Session Store]: JSON Web Token session store.
52+
* link:modules/redis#redis-http-session[Redis Session Store]: Save session data on redis.
53+
54+
=== Scheduler
55+
* link:modules/quartz[Quartz]: Quartz scheduler module.
56+
57+
.

docs/asciidoc/modules/redis.adoc

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
== Redis
2+
3+
Redis using https://lettuce.io[Lettuce] library.
4+
5+
=== Usage
6+
7+
1) Add the dependency:
8+
9+
[dependency, artifactId="jooby-redis"]
10+
.
11+
12+
2) Set redis property
13+
14+
.application.conf
15+
[source, properties]
16+
----
17+
redis = "redis://localhost:6379"
18+
----
19+
20+
3) Install and use Redis
21+
22+
.Java
23+
[source, java, role="primary"]
24+
----
25+
import io.jooby.redis.RedisModule;
26+
import io.lettuce.core.api.StatefulRedisConnection;
27+
28+
{
29+
install(new RedisModule()); <1>
30+
31+
get("/", ctx -> {
32+
StatefulRedisConnection redis = require(StatefulRedisConnection.class); <2>
33+
// work with redis
34+
});
35+
}
36+
----
37+
38+
.Kotlin
39+
[source, kt, role="secondary"]
40+
----
41+
import io.jooby.redis.RedisModule
42+
import io.lettuce.core.api.StatefulRedisConnection
43+
44+
{
45+
install(RedisModule()) <1>
46+
47+
get("/") {
48+
val redis = require(StatefulRedisConnection::class) <2>
49+
// work with Database
50+
}
51+
}
52+
----
53+
54+
<1> Install Redis
55+
<2> Use Redis
56+
57+
The `io.lettuce.core.pubsub.StatefulRedisPubSubConnection` is also available.
58+
59+
=== Redis URI
60+
61+
Redis URI are allowed to pass it directly or from application configuration file:
62+
63+
----
64+
{
65+
new RedisModule("redis://localhost:6379");
66+
}
67+
----
68+
69+
Or read it from configuration:
70+
71+
----
72+
{
73+
new RedisModule("redis");
74+
}
75+
----
76+
77+
Either `redis` or `redis.uri` must be present in your application configuration file and must be a
78+
https://lettuce.io/core/release/reference/index.html#redisuri.uri-syntax[valid Redis URI].
79+
80+
=== Multiple Connections
81+
82+
Support for multiple connection is done using multiple configuration keys:
83+
84+
.Java
85+
[source, java, role="primary"]
86+
----
87+
import io.jooby.redis.RedisModule;
88+
import io.lettuce.core.api.StatefulRedisConnection;
89+
90+
{
91+
install(new RedisModule("foo"));
92+
93+
install(new RedisModule("bar"));
94+
95+
get("/", ctx -> {
96+
StatefulRedisConnection foo = require(StatefulRedisConnection.class, "foo");
97+
// work with redis
98+
});
99+
}
100+
----
101+
102+
.Kotlin
103+
[source, kt, role="secondary"]
104+
----
105+
import io.jooby.redis.RedisModule
106+
import io.lettuce.core.api.StatefulRedisConnection
107+
108+
{
109+
install(RedisModule("foo"))
110+
111+
install(RedisModule("bar"))
112+
113+
get("/") {
114+
val redis = require(StatefulRedisConnection::class, "bar")
115+
// work with Database
116+
}
117+
}
118+
----
119+
120+
Where `foo` and `bar` are redis URI in your application configuration file:
121+
122+
.application.conf
123+
[source, properties]
124+
----
125+
foo = "redis://server1:6379"
126+
127+
bar = "redis://server2:6379"
128+
----
129+
130+
=== HTTP Session
131+
132+
The Redis module provides a HTTP Session Store.
133+
134+
.Usage
135+
[source, java, role="primary"]
136+
----
137+
import io.jooby.redis.RedisModule;
138+
import io.jooby.redis.RedisSessionStore;
139+
140+
import io.lettuce.core.api.StatefulRedisConnection;
141+
142+
{
143+
install(new RedisModule()); <1>
144+
145+
setSessionStore(new RedisSessionStore(require(StatefulRedisConnection.class))); <2>
146+
147+
get("/", ctx -> {
148+
Session httpSession = ctx.session(); <3>
149+
// HTTP session is backed by Redis
150+
});
151+
}
152+
----
153+
154+
.Kotlin
155+
[source, kt, role="secondary"]
156+
----
157+
import io.jooby.redis.RedisModule
158+
import io.jooby.redis.RedisSessionStore
159+
160+
import io.lettuce.core.api.StatefulRedisConnection
161+
162+
{
163+
install(RedisModule()) <1>
164+
165+
sessionStore = RedisSessionStore(require(StatefulRedisConnection::class)) <2>
166+
167+
get("/") {
168+
val httpSession = ctx.session() <3>
169+
// HTTP session is backed by Redis
170+
}
171+
}
172+
----
173+
174+
<1> Install Redis module
175+
<2> Configure session store to use Redis
176+
<3> Session data is persisted in Redis
177+
178+
More Options:
179+
180+
- javadoc:redis.RedisSessionStore[setTimeout, java.time.Duraction]: Set session timeout. Default is: `30 minutes`
181+
- javadoc:redis.RedisSessionStore[setNamespace, java.lang.String]: Set key prefix. Default is: `sessions`
182+
- javadoc:redis.RedisSessionStore[setToken, io.jooby.SessionToken]: Set session token. Default is a cookie token: `jooby.sid`

0 commit comments

Comments
 (0)