From 9eb2ac899e1fb05248c75af8a0809ca7db568956 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Fri, 30 Mar 2018 21:58:06 +0200 Subject: [PATCH] [1.5] redis ^.^ --- .../java/dst/ass1/kv/impl/SessionManager.java | 73 +++++++++++++++++++ .../ass1/kv/impl/SessionManagerFactory.java | 9 +-- 2 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 ass1-kv/src/main/java/dst/ass1/kv/impl/SessionManager.java diff --git a/ass1-kv/src/main/java/dst/ass1/kv/impl/SessionManager.java b/ass1-kv/src/main/java/dst/ass1/kv/impl/SessionManager.java new file mode 100644 index 0000000..02fc964 --- /dev/null +++ b/ass1-kv/src/main/java/dst/ass1/kv/impl/SessionManager.java @@ -0,0 +1,73 @@ +package dst.ass1.kv.impl; + +import dst.ass1.kv.ISessionManager; +import dst.ass1.kv.SessionCreationFailedException; +import dst.ass1.kv.SessionNotFoundException; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.Transaction; + +import java.util.UUID; + +public class SessionManager implements ISessionManager { + // "sUser:${userId}" -> UUID - Reverse-sessiondata-userId-mapping. + private final static String SUSER = "sUser:"; + // "sData:${UUID}" -> hashmap - Sessiondata. + private final static String SDATA = "sData:"; + + private final Jedis jedis; // no lightsabers :( + + public SessionManager(String host, int port) { + jedis = new Jedis(host, port); + } + + @Override + public String createSession(Long userId, int timeToLive) throws SessionCreationFailedException { + jedis.del(SUSER + userId.toString()); + return requireSession(userId, timeToLive); + } + + @Override + public void setSessionVariable(String sessionId, String key, String value) throws SessionNotFoundException { + if (!jedis.exists(SDATA + sessionId)) throw new SessionNotFoundException("Unknown sessionId:" + sessionId); + jedis.hset(SDATA + sessionId, key, value); + } + + @Override + public String getSessionVariable(String sessionId, String key) throws SessionNotFoundException { + if (!jedis.exists(SDATA + sessionId)) throw new SessionNotFoundException("Unknown sessionId:" + sessionId); + return jedis.hget(SDATA + sessionId, key); + } + + @Override + public Long getUserId(String sessionId) throws SessionNotFoundException { + if (!jedis.exists(SDATA + sessionId)) throw new SessionNotFoundException("Unknown sessionId:" + sessionId); + return Long.valueOf(jedis.hget(SDATA + sessionId, "userId")); + } + + @Override + public int getTimeToLive(String sessionId) throws SessionNotFoundException { + if (!jedis.exists(SDATA + sessionId)) throw new SessionNotFoundException("Unknown sessionId:" + sessionId); + return jedis.ttl(SDATA + sessionId).intValue(); + } + + @Override + public String requireSession(Long userId, int timeToLive) throws SessionCreationFailedException { + String uuid = jedis.get(SUSER + userId.toString()); + if (uuid == null) { + uuid = UUID.randomUUID().toString(); + jedis.watch(SUSER + userId.toString()); + Transaction t = jedis.multi(); + t.set(SUSER + userId.toString(), uuid); + t.expire(SUSER + userId.toString(), timeToLive); + t.hset(SDATA + uuid, "userId", userId.toString()); + t.expire(SDATA + uuid, timeToLive); + t.exec(); + } + return uuid; + } + + @Override + public void close() { + jedis.close(); + } +} diff --git a/ass1-kv/src/main/java/dst/ass1/kv/impl/SessionManagerFactory.java b/ass1-kv/src/main/java/dst/ass1/kv/impl/SessionManagerFactory.java index 4b0a721..2177f26 100644 --- a/ass1-kv/src/main/java/dst/ass1/kv/impl/SessionManagerFactory.java +++ b/ass1-kv/src/main/java/dst/ass1/kv/impl/SessionManagerFactory.java @@ -1,17 +1,14 @@ package dst.ass1.kv.impl; -import java.util.Properties; - import dst.ass1.kv.ISessionManager; import dst.ass1.kv.ISessionManagerFactory; +import java.util.Properties; + public class SessionManagerFactory implements ISessionManagerFactory { @Override public ISessionManager createSessionManager(Properties properties) { - // TODO - // read "redis.host" and "redis.port" from the properties - - return null; + return new SessionManager(properties.getProperty("redis.host"), Integer.parseInt(properties.getProperty("redis.port"))); } } -- 2.43.0