Skip to content

Commit

Permalink
[enhancement] Enhance Session Expiry Time for Docean
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava committed Jul 5, 2024
1 parent 280045f commit ab2f2e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class HttpSessionManager {
long now = System.currentTimeMillis();
List<String> ids = SESSION_MAP.values().stream().filter(it -> {
DefaultHttpSession hs = (DefaultHttpSession) it;
if (now - hs.getUpdateTime() > TimeUnit.MINUTES.toMillis(10)) {
if (now - hs.getUpdateTime() > TimeUnit.MINUTES.toMillis(60)) {
return true;
}
return false;
Expand Down Expand Up @@ -82,7 +82,14 @@ public static void invalidate(String sessionId) {
}

public static HttpSession getSession(String sessionId) {
return SESSION_MAP.get(sessionId);
return SESSION_MAP.compute(sessionId, (k, v) -> {
if (null != v) {
if (v instanceof DefaultHttpSession dhs) {
dhs.setUpdateTime(System.currentTimeMillis());
}
}
return v;
});
}


Expand All @@ -105,15 +112,15 @@ public static HttpSession getSession(MvcContext context) {

public static void setSessionId(MvcContext context, boolean exists, FullHttpResponse response) {
if (!context.getSessionId().equals("")) {
Cookie cookie = new DefaultCookie(HttpSession.SESSIONID,context.getSessionId());
Cookie cookie = new DefaultCookie(HttpSession.SESSIONID, context.getSessionId());
cookie.setPath("/");
String encodeCookie = ServerCookieEncoder.STRICT.encode(cookie);
response.headers().set(HttpHeaderNames.SET_COOKIE, encodeCookie);
return;
}

if (exists == false) {
Cookie cookie = new DefaultCookie(HttpSession.SESSIONID,HttpSessionManager.createSession());
Cookie cookie = new DefaultCookie(HttpSession.SESSIONID, HttpSessionManager.createSession());
cookie.setPath("/");
String encodeCookie = ServerCookieEncoder.STRICT.encode(cookie);
response.headers().set(HttpHeaderNames.SET_COOKIE, encodeCookie);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.xiaomi.youpin.docean.test;

import com.xiaomi.youpin.docean.common.MutableObject;
import org.junit.Test;

import java.util.concurrent.ConcurrentHashMap;

/**
* @author [email protected]
* @date 2024/7/5 08:18
*/
public class MapTest {

@Test
public void testMap() {
MutableObject mo = new MutableObject();
ConcurrentHashMap<String,MutableObject> map = new ConcurrentHashMap<>();
mo.setObj("123");
map.put("aa",mo);
MutableObject e = map.compute("a", (k, v) -> {
if (null != v) {
v.setObj("234");
}
return v;
});
System.out.println(e);
}
}

0 comments on commit ab2f2e4

Please sign in to comment.