diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/session/HttpSessionManager.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/session/HttpSessionManager.java index 4dc95688c..2c8758044 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/session/HttpSessionManager.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/session/HttpSessionManager.java @@ -46,7 +46,7 @@ public class HttpSessionManager { long now = System.currentTimeMillis(); List 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; @@ -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; + }); } @@ -105,7 +112,7 @@ 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); @@ -113,7 +120,7 @@ public static void setSessionId(MvcContext context, boolean exists, FullHttpResp } 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); diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/MapTest.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/MapTest.java new file mode 100644 index 000000000..66e7b9fe1 --- /dev/null +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/MapTest.java @@ -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 goodjava@qq.com + * @date 2024/7/5 08:18 + */ +public class MapTest { + + @Test + public void testMap() { + MutableObject mo = new MutableObject(); + ConcurrentHashMap 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); + } +}