Skip to content

Commit 7e9a6b0

Browse files
committed
1. Include sessionInMemory update in sessionDAO.doUpdate
2. Add sessionInMemoryEnabled to turn on/off sessionInMemory
1 parent 4a98b63 commit 7e9a6b0

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ These 4 Serializers are replaceable:
318318
| expire | `-2` | Redis cache key/value expire time. The expire time is in second.<br>Special values:<br>`-1`: no expire<br>`-2`: the same timeout with session<br>Default value: `-2`<br>**Note**: Make sure expire time is longer than session timeout. |
319319
| keyPrefix | `shiro:session:` | Custom your redis key prefix for session management<br>**Note**: Remember to add colon at the end of prefix. |
320320
| sessionInMemoryTimeout | `1000` | When we do signin, `doReadSession(sessionId)` will be called by shiro about 10 times. So shiro-redis save Session in ThreadLocal to remit this problem. sessionInMemoryTimeout is expiration of Session in ThreadLocal. <br>Most of time, you don't need to change it. |
321+
| sessionInMemoryEnabled | `true` | Whether or not enable temporary save session in ThreadLocal |
321322
| keySerializer | `org.crazycake.shiro.serializer.StringSerializer` | The key serializer of cache manager<br>You can change the implement of key serializer or the encoding of StringSerializer.<br>Supported encodings refer to [Supported Encodings](https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html). Such as `UTF-8`, `UTF-16`, `UTF-32`, `ISO-8859-1`, `GBK`, `Big5`, etc<br>For more detail, check [Serializer](#serializer) |
322323
| valueSerializer | `org.crazycake.shiro.serializer.ObjectSerializer` | The value serializer of cache manager<br>You can change the implement of value serializer<br>For more detail, check [Serializer](#serializer) |
323324

@@ -421,6 +422,7 @@ For full example, see [shiro-redis-spring-boot-tutorial](https://github.com/alex
421422
| shiro-redis.session-dao.expire | `-2` | Redis cache key/value expire time. The expire time is in second.<br>Special values:<br>`-1`: no expire<br>`-2`: the same timeout with session<br>Default value: `-2`<br>**Note**: Make sure expire time is longer than session timeout. |
422423
| shiro-redis.session-dao.key-prefix | `shiro:session:` | Custom your redis key prefix for session management<br>**Note**: Remember to add colon at the end of prefix. |
423424
| shiro-redis.session-dao.session-in-memory-timeout | `1000` | When we do signin, `doReadSession(sessionId)` will be called by shiro about 10 times. So shiro-redis save Session in ThreadLocal to remit this problem. sessionInMemoryTimeout is expiration of Session in ThreadLocal. <br>Most of time, you don't need to change it. |
425+
| shiro-redis.session-dao.session-in-memory-enabled | `true` | Whether or not enable temporary save session in ThreadLocal |
424426
| shiro-redis.cache-manager.principal-id-field-name | `id` | Principal id field name. The field which you can get unique id to identify this principal.<br>For example, if you use UserInfo as Principal class, the id field maybe `id`, `userId`, `email`, etc.<br>Remember to add getter to this id field. For example, `getId()`, `getUserId(`), `getEmail()`, etc.<br>Default value is `id`, that means your principal object must has a method called `getId()` |
425427
| shiro-redis.cache-manager.expire | `1800` | Redis cache key/value expire time. <br>The expire time is in second. |
426428
| shiro-redis.cache-manager.key-prefix | `shiro:cache:` | Custom your redis key prefix for cache management<br>**Note**: Remember to add colon at the end of prefix. |

src/main/java/org/crazycake/shiro/RedisSessionDAO.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public class RedisSessionDAO extends AbstractSessionDAO {
2929
*/
3030
private long sessionInMemoryTimeout = DEFAULT_SESSION_IN_MEMORY_TIMEOUT;
3131

32+
private static final boolean DEFAULT_SESSION_IN_MEMORY_ENABLED = true;
33+
34+
private boolean sessionInMemoryEnabled = DEFAULT_SESSION_IN_MEMORY_ENABLED;
35+
3236
// expire time in seconds
3337
private static final int DEFAULT_EXPIRE = -2;
3438
private static final int NO_EXPIRE = -1;
@@ -48,6 +52,9 @@ public class RedisSessionDAO extends AbstractSessionDAO {
4852
@Override
4953
public void update(Session session) throws UnknownSessionException {
5054
this.saveSession(session);
55+
if (this.sessionInMemoryEnabled) {
56+
this.setSessionToThreadLocal(session.getId(), session);
57+
}
5158
}
5259

5360
/**
@@ -131,20 +138,24 @@ protected Session doReadSession(Serializable sessionId) {
131138
logger.warn("session id is null");
132139
return null;
133140
}
134-
Session s = getSessionFromThreadLocal(sessionId);
135-
136-
if (s != null) {
137-
return s;
141+
if (this.sessionInMemoryEnabled) {
142+
Session session = getSessionFromThreadLocal(sessionId);
143+
if (session != null) {
144+
return session;
145+
}
138146
}
139147

148+
Session session = null;
140149
logger.debug("read session from redis");
141150
try {
142-
s = (Session) valueSerializer.deserialize(redisManager.get(keySerializer.serialize(getRedisSessionKey(sessionId))));
143-
setSessionToThreadLocal(sessionId, s);
151+
session = (Session) valueSerializer.deserialize(redisManager.get(keySerializer.serialize(getRedisSessionKey(sessionId))));
152+
if (this.sessionInMemoryEnabled) {
153+
setSessionToThreadLocal(sessionId, session);
154+
}
144155
} catch (SerializationException e) {
145156
logger.error("read session error. settionId=" + sessionId);
146157
}
147-
return s;
158+
return session;
148159
}
149160

150161
private void setSessionToThreadLocal(Serializable sessionId, Session s) {
@@ -234,4 +245,12 @@ public int getExpire() {
234245
public void setExpire(int expire) {
235246
this.expire = expire;
236247
}
248+
249+
public boolean getSessionInMemoryEnabled() {
250+
return sessionInMemoryEnabled;
251+
}
252+
253+
public void setSessionInMemoryEnabled(boolean sessionInMemoryEnabled) {
254+
this.sessionInMemoryEnabled = sessionInMemoryEnabled;
255+
}
237256
}

src/test/java/org/crazycake/shiro/RedisSessionDAOTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ public void testUpdateEmptySession() {
8686
@Test
8787
public void testUpdate() {
8888
redisSessionDAO.doCreate(session1);
89+
redisSessionDAO.doReadSession(session1.getId());
90+
doChangeSessionName(session1, name1);
91+
redisSessionDAO.update(session1);
92+
FakeSession actualSession = (FakeSession)redisSessionDAO.doReadSession(session1.getId());
93+
assertEquals(actualSession.getName(), name1);
94+
}
95+
96+
@Test
97+
public void testUpdateWithoutSessionInMemory() {
98+
redisSessionDAO.setSessionInMemoryEnabled(false);
99+
redisSessionDAO.doCreate(session1);
100+
redisSessionDAO.doReadSession(session1.getId());
89101
doChangeSessionName(session1, name1);
90102
redisSessionDAO.update(session1);
91103
FakeSession actualSession = (FakeSession)redisSessionDAO.doReadSession(session1.getId());

0 commit comments

Comments
 (0)