Skip to content

Commit 5f4eda9

Browse files
committed
redis session store fix #58
1 parent 86c7289 commit 5f4eda9

4 files changed

Lines changed: 173 additions & 0 deletions

File tree

coverage-report/src/test/java/org/jooby/jedis/JedisFeature.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.jooby.jedis;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
import org.jooby.test.ServerFeature;
47
import org.junit.Test;
58

@@ -20,6 +23,12 @@ public class JedisFeature extends ServerFeature {
2023
get("/:key/:value", req -> {
2124
try (Jedis jedis = req.require(Jedis.class)) {
2225
jedis.set(req.param("key").value(), req.param("value").value());
26+
27+
Map<String, String> attrs = new HashMap<>();
28+
attrs.put("name", "edgar");
29+
attrs.put("age", "34");
30+
jedis.hmset("session:1", attrs);
31+
2332
return jedis.get(req.param("key").value());
2433
}
2534
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.jooby.jedis.session;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.jooby.Session;
6+
import org.jooby.jedis.Redis;
7+
import org.jooby.jedis.RedisSessionStore;
8+
import org.jooby.test.ServerFeature;
9+
import org.junit.Test;
10+
11+
import com.typesafe.config.ConfigFactory;
12+
import com.typesafe.config.ConfigValueFactory;
13+
14+
public class RedisSessionDataFeature extends ServerFeature {
15+
16+
{
17+
use(ConfigFactory.empty()
18+
.withValue("db", ConfigValueFactory.fromAnyRef("redis://localhost:6379")));
19+
20+
use(new Redis());
21+
22+
session(RedisSessionStore.class);
23+
24+
get("/s1", req -> {
25+
Session session = req.session();
26+
session
27+
.set("name", "edgar")
28+
.set("age", 34);
29+
return session.attributes();
30+
});
31+
32+
get("/s2", req -> {
33+
Session session = req.session();
34+
return session.attributes();
35+
});
36+
37+
get("/destroy", req -> {
38+
req.session().destroy();;
39+
return "done";
40+
});
41+
}
42+
43+
@Test
44+
public void save() throws Exception {
45+
request()
46+
.get("/s1")
47+
.expect(rsp -> {
48+
assertTrue(rsp.equals("{name=edgar, age=34}") || rsp.equals("{age=34, name=edgar}"));
49+
})
50+
.request(r1 ->
51+
r1.get("/s2")
52+
.expect(rsp -> {
53+
assertTrue(rsp.equals("{name=edgar, age=34}")
54+
|| rsp.equals("{age=34, name=edgar}"));
55+
}).request(r2 -> {
56+
// cleanup
57+
r2.get("/destroy")
58+
.expect("done");
59+
})
60+
);
61+
62+
}
63+
}

jooby-jedis/src/main/java/org/jooby/jedis/RedisProvider.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.jedis;
220

321
import java.net.URI;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jooby.jedis;
20+
21+
import static java.util.Objects.requireNonNull;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import javax.inject.Inject;
27+
import javax.inject.Singleton;
28+
29+
import org.jooby.Session;
30+
import org.jooby.Session.Builder;
31+
32+
import redis.clients.jedis.Jedis;
33+
import redis.clients.jedis.JedisPool;
34+
35+
@Singleton
36+
public class RedisSessionStore implements Session.Store {
37+
38+
private JedisPool pool;
39+
40+
@Inject
41+
public RedisSessionStore(final JedisPool pool) {
42+
this.pool = requireNonNull(pool, "Jedis pool is required.");
43+
}
44+
45+
@Override
46+
public Session get(final Builder builder) {
47+
try (Jedis jedis = pool.getResource()) {
48+
Map<String, String> attrs = jedis.hgetAll("sessions:" + builder.sessionId());
49+
return builder
50+
.accessedAt(Long.parseLong(attrs.remove("accessedAt")))
51+
.createdAt(Long.parseLong(attrs.remove("createdAt")))
52+
.savedAt(Long.parseLong(attrs.remove("savedAt")))
53+
.set(attrs).build();
54+
}
55+
}
56+
57+
@Override
58+
public void save(final Session session) {
59+
try (Jedis jedis = pool.getResource()) {
60+
String key = "sessions:" + session.id();
61+
Map<String, String> attrs = new HashMap<>(session.attributes());
62+
attrs.put("createdAt", Long.toString(session.createdAt()));
63+
attrs.put("accessedAt", Long.toString(session.accessedAt()));
64+
attrs.put("savedAt", Long.toString(session.savedAt()));
65+
jedis.hmset(key, attrs);
66+
}
67+
}
68+
69+
@Override
70+
public void create(final Session session) {
71+
save(session);
72+
}
73+
74+
@Override
75+
public void delete(final String id) {
76+
try (Jedis jedis = pool.getResource()) {
77+
String key = "sessions:" + id;
78+
jedis.del(key);
79+
}
80+
81+
}
82+
83+
}

0 commit comments

Comments
 (0)