diff --git a/resources/RedisDataCache.properties b/resources/RedisDataCache.properties index 0ab2265..b5e4abe 100644 --- a/resources/RedisDataCache.properties +++ b/resources/RedisDataCache.properties @@ -1,6 +1,14 @@ # redis hosts ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, .... redis.hosts=127.0.0.1:6379 + # Redis Password redis.password= + # set true to enable redis cluster mode -redis.cluster.enabled=false \ No newline at end of file +redis.cluster.enabled=false + +# Redis database (default 0) +#redis.database=0 + +# Redis connection timeout (default 2000) +#redis.timeout=2000 \ No newline at end of file diff --git a/src/com/r/tomcat/session/data/cache/RedisClusterManager.java b/src/com/r/tomcat/session/data/cache/RedisClusterManager.java index 1714bd5..001d372 100644 --- a/src/com/r/tomcat/session/data/cache/RedisClusterManager.java +++ b/src/com/r/tomcat/session/data/cache/RedisClusterManager.java @@ -59,7 +59,8 @@ public class RedisClusterManager poolConfig.setNumTestsPerEvictionRun(testNumPerEviction); long timeBetweenEviction = Long.parseLong(properties.getProperty(RedisConstants.TIME_BETWEENEVICTION, RedisConstants.DEFAULT_TIME_BETWEENEVICTION_VALUE)); poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEviction); - jedisCluster = new JedisCluster(getJedisClusterNodesSet(properties.getProperty(RedisConstants.HOSTS, Protocol.DEFAULT_HOST.concat(":").concat(String.valueOf(Protocol.DEFAULT_PORT)))), poolConfig); + int timeout = Integer.parseInt(properties.getProperty(RedisConstants.TIMEOUT, String.valueOf(Protocol.DEFAULT_TIMEOUT))); + jedisCluster = new JedisCluster(getJedisClusterNodesSet(properties.getProperty(RedisConstants.HOSTS, Protocol.DEFAULT_HOST.concat(":").concat(String.valueOf(Protocol.DEFAULT_PORT)))), (timeout < Protocol.DEFAULT_TIMEOUT ? Protocol.DEFAULT_TIMEOUT : timeout), poolConfig); } /** diff --git a/src/com/r/tomcat/session/data/cache/RedisManager.java b/src/com/r/tomcat/session/data/cache/RedisManager.java index 807f4c7..b49ce56 100644 --- a/src/com/r/tomcat/session/data/cache/RedisManager.java +++ b/src/com/r/tomcat/session/data/cache/RedisManager.java @@ -71,11 +71,9 @@ public class RedisManager } } String password = properties.getProperty(RedisConstants.PASSWORD); - if (password == null || password == "" || password.isEmpty()) { - pool = new JedisPool(poolConfig, host, port); - } else { - pool = new JedisPool(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, password); - } + int database = Integer.parseInt(properties.getProperty(RedisConstants.DATABASE, String.valueOf(Protocol.DEFAULT_DATABASE))); + int timeout = Integer.parseInt(properties.getProperty(RedisConstants.TIMEOUT, String.valueOf(Protocol.DEFAULT_TIMEOUT))); + pool = new JedisPool(poolConfig, host, port, (timeout < Protocol.DEFAULT_TIMEOUT ? Protocol.DEFAULT_TIMEOUT : timeout), ((password == null || password == "" || password.isEmpty()) ? null : password), database); } public Jedis getJedis() { diff --git a/src/com/r/tomcat/session/data/cache/constants/RedisConstants.java b/src/com/r/tomcat/session/data/cache/constants/RedisConstants.java index 7f01483..a42aa50 100644 --- a/src/com/r/tomcat/session/data/cache/constants/RedisConstants.java +++ b/src/com/r/tomcat/session/data/cache/constants/RedisConstants.java @@ -24,7 +24,9 @@ public class RedisConstants public static final String HOSTS = "redis.hosts"; public static final String PASSWORD = "redis.password"; public static final String IS_CLUSTER_ENABLED = "redis.cluster.enabled"; - + public static final String DATABASE = "redis.database"; + public static final String TIMEOUT = "redis.timeout"; + // Redis property default values public static final String DEFAULT_MAX_ACTIVE_VALUE = "10"; public static final String DEFAULT_TEST_ONBORROW_VALUE = "true"; diff --git a/src/com/r/tomcat/session/management/RequestSessionManager.java b/src/com/r/tomcat/session/management/RequestSessionManager.java index 843c7f5..6a41389 100644 --- a/src/com/r/tomcat/session/management/RequestSessionManager.java +++ b/src/com/r/tomcat/session/management/RequestSessionManager.java @@ -229,7 +229,7 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle @Override public void remove(Session session, boolean update) { - requestSessionCacheUtils.deleteKey(session.getId()); + requestSessionCacheUtils.expire(session.getId(), 10); } @Override @@ -296,8 +296,11 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle currentSessionSerializationMetadata.set(updatedSerializationMetadata); currentSessionIsPersisted.set(true); } - log.trace("Setting expire timeout on session [" + customSession.getId() + "] to " + (getContext().getSessionTimeout() * 60)); - requestSessionCacheUtils.expire(customSession.getId(), (getContext().getSessionTimeout() * 60)); + + int timeout = getContext().getSessionTimeout() * 60; + timeout = timeout < 1800 ? 1800 : timeout; + log.trace("Setting expire timeout on session [" + customSession.getId() + "] to " + timeout); + requestSessionCacheUtils.expire(customSession.getId(), timeout); } catch (IOException e) { log.error("Error occured while storing the session object into redis", e); }