提交 a55562c6 编写于 作者: N Nikita

UpdateMode.AFTER_REQUEST mode optimization. #1302

上级 233f3926
......@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.catalina.session.StandardSession;
import org.redisson.api.RMap;
import org.redisson.tomcat.RedissonSessionManager.ReadMode;
import org.redisson.tomcat.RedissonSessionManager.UpdateMode;
/**
* Redisson Session object for Apache Tomcat
......@@ -37,11 +38,13 @@ public class RedissonSession extends StandardSession {
private final Map<String, Object> attrs;
private RMap<String, Object> map;
private final RedissonSessionManager.ReadMode readMode;
private final UpdateMode updateMode;
public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode) {
public RedissonSession(RedissonSessionManager manager, ReadMode readMode, UpdateMode updateMode) {
super(manager);
this.redissonManager = manager;
this.readMode = readMode;
this.updateMode = updateMode;
try {
Field attr = StandardSession.class.getDeclaredField("attributes");
......@@ -144,7 +147,7 @@ public class RedissonSession extends StandardSession {
public void setAttribute(String name, Object value, boolean notify) {
super.setAttribute(name, value, notify);
if (map != null && value != null) {
if (updateMode == UpdateMode.DEFAULT && map != null && value != null) {
map.fastPut(name, value);
}
}
......@@ -153,7 +156,7 @@ public class RedissonSession extends StandardSession {
protected void removeAttributeInternal(String name, boolean notify) {
super.removeAttributeInternal(name, notify);
if (map != null) {
if (updateMode == UpdateMode.DEFAULT && map != null) {
map.fastRemove(name);
}
}
......
......@@ -17,7 +17,6 @@ package org.redisson.tomcat;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpSession;
......@@ -159,7 +158,7 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
@Override
public Session createEmptySession() {
return new RedissonSession(this, readMode);
return new RedissonSession(this, readMode, updateMode);
}
@Override
......@@ -218,18 +217,14 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
lifecycle.fireLifecycleEvent(STOP_EVENT, null);
}
public void store(HttpSession session) {
public void store(HttpSession session) throws IOException {
if (session == null) {
return;
}
if (updateMode == UpdateMode.AFTER_REQUEST) {
Enumeration<String> names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
Object value = session.getAttribute(name);
session.setAttribute(name, value);
}
RedissonSession sess = (RedissonSession) findSession(session.getId());
sess.save();
}
}
......
package org.redisson.tomcat;
import java.nio.file.Paths;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
......@@ -56,7 +58,8 @@ public class TomcatServer {
localHost.setAutoDeploy(false);
StandardContext rootContext = (StandardContext) server.createContext(contextPath, "webapp");
rootContext.setDefaultWebXml("web.xml");
String s = Paths.get("").toAbsolutePath().resolve("src/test/webapp/META-INF/context.xml").toString();
rootContext.setDefaultContextXml(s);
localHost.addChild(rootContext);
Engine engine = server.createEngine();
......
......@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.catalina.session.StandardSession;
import org.redisson.api.RMap;
import org.redisson.tomcat.RedissonSessionManager.ReadMode;
import org.redisson.tomcat.RedissonSessionManager.UpdateMode;
/**
* Redisson Session object for Apache Tomcat
......@@ -37,11 +38,14 @@ public class RedissonSession extends StandardSession {
private final Map<String, Object> attrs;
private RMap<String, Object> map;
private final RedissonSessionManager.ReadMode readMode;
private final UpdateMode updateMode;
public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode) {
public RedissonSession(RedissonSessionManager manager, ReadMode readMode, UpdateMode updateMode) {
super(manager);
this.redissonManager = manager;
this.readMode = readMode;
this.updateMode = updateMode;
try {
Field attr = StandardSession.class.getDeclaredField("attributes");
attrs = (Map<String, Object>) attr.get(this);
......@@ -148,7 +152,7 @@ public class RedissonSession extends StandardSession {
public void setAttribute(String name, Object value, boolean notify) {
super.setAttribute(name, value, notify);
if (map != null && value != null) {
if (updateMode == UpdateMode.DEFAULT && map != null && value != null) {
map.fastPut(name, value);
}
}
......@@ -157,7 +161,7 @@ public class RedissonSession extends StandardSession {
protected void removeAttributeInternal(String name, boolean notify) {
super.removeAttributeInternal(name, notify);
if (map != null) {
if (updateMode == UpdateMode.DEFAULT && map != null) {
map.fastRemove(name);
}
}
......
......@@ -17,12 +17,11 @@ package org.redisson.tomcat;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import org.apache.catalina.Context;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Session;
......@@ -138,7 +137,7 @@ public class RedissonSessionManager extends ManagerBase {
@Override
public Session createEmptySession() {
return new RedissonSession(this, readMode);
return new RedissonSession(this, readMode, updateMode);
}
@Override
......@@ -213,18 +212,14 @@ public class RedissonSessionManager extends ManagerBase {
}
public void store(HttpSession session) {
public void store(HttpSession session) throws IOException {
if (session == null) {
return;
}
if (updateMode == UpdateMode.AFTER_REQUEST) {
Enumeration<String> names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
Object value = session.getAttribute(name);
session.setAttribute(name, value);
}
RedissonSession sess = (RedissonSession) findSession(session.getId());
sess.save();
}
}
......
......@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.catalina.session.StandardSession;
import org.redisson.api.RMap;
import org.redisson.tomcat.RedissonSessionManager.ReadMode;
import org.redisson.tomcat.RedissonSessionManager.UpdateMode;
/**
* Redisson Session object for Apache Tomcat
......@@ -37,11 +38,13 @@ public class RedissonSession extends StandardSession {
private final Map<String, Object> attrs;
private RMap<String, Object> map;
private final RedissonSessionManager.ReadMode readMode;
private final UpdateMode updateMode;
public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode) {
public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode) {
super(manager);
this.redissonManager = manager;
this.readMode = readMode;
this.updateMode = updateMode;
try {
Field attr = StandardSession.class.getDeclaredField("attributes");
......@@ -149,7 +152,7 @@ public class RedissonSession extends StandardSession {
public void setAttribute(String name, Object value, boolean notify) {
super.setAttribute(name, value, notify);
if (map != null && value != null) {
if (updateMode == UpdateMode.DEFAULT && map != null && value != null) {
map.fastPut(name, value);
}
}
......@@ -158,7 +161,7 @@ public class RedissonSession extends StandardSession {
protected void removeAttributeInternal(String name, boolean notify) {
super.removeAttributeInternal(name, notify);
if (map != null) {
if (updateMode == UpdateMode.DEFAULT && map != null) {
map.fastRemove(name);
}
}
......
......@@ -17,7 +17,6 @@ package org.redisson.tomcat;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpSession;
......@@ -138,7 +137,7 @@ public class RedissonSessionManager extends ManagerBase {
@Override
public Session createEmptySession() {
return new RedissonSession(this, readMode);
return new RedissonSession(this, readMode, updateMode);
}
@Override
......@@ -208,18 +207,14 @@ public class RedissonSessionManager extends ManagerBase {
}
public void store(HttpSession session) {
public void store(HttpSession session) throws IOException {
if (session == null) {
return;
}
if (updateMode == UpdateMode.AFTER_REQUEST) {
Enumeration<String> names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
Object value = session.getAttribute(name);
session.setAttribute(name, value);
}
RedissonSession sess = (RedissonSession) findSession(session.getId());
sess.save();
}
}
......
......@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.catalina.session.StandardSession;
import org.redisson.api.RMap;
import org.redisson.tomcat.RedissonSessionManager.ReadMode;
import org.redisson.tomcat.RedissonSessionManager.UpdateMode;
/**
* Redisson Session object for Apache Tomcat
......@@ -37,11 +38,13 @@ public class RedissonSession extends StandardSession {
private final Map<String, Object> attrs;
private RMap<String, Object> map;
private final RedissonSessionManager.ReadMode readMode;
private final UpdateMode updateMode;
public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode) {
public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode) {
super(manager);
this.redissonManager = manager;
this.readMode = readMode;
this.updateMode = updateMode;
try {
Field attr = StandardSession.class.getDeclaredField("attributes");
......@@ -149,7 +152,7 @@ public class RedissonSession extends StandardSession {
public void setAttribute(String name, Object value, boolean notify) {
super.setAttribute(name, value, notify);
if (map != null && value != null) {
if (updateMode == UpdateMode.DEFAULT && map != null && value != null) {
map.fastPut(name, value);
}
}
......@@ -158,7 +161,7 @@ public class RedissonSession extends StandardSession {
protected void removeAttributeInternal(String name, boolean notify) {
super.removeAttributeInternal(name, notify);
if (map != null) {
if (updateMode == UpdateMode.DEFAULT && map != null) {
map.fastRemove(name);
}
}
......
......@@ -17,7 +17,6 @@ package org.redisson.tomcat;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpSession;
......@@ -138,7 +137,7 @@ public class RedissonSessionManager extends ManagerBase {
@Override
public Session createEmptySession() {
return new RedissonSession(this, readMode);
return new RedissonSession(this, readMode, updateMode);
}
@Override
......@@ -208,18 +207,14 @@ public class RedissonSessionManager extends ManagerBase {
}
public void store(HttpSession session) {
public void store(HttpSession session) throws IOException {
if (session == null) {
return;
}
if (updateMode == UpdateMode.AFTER_REQUEST) {
Enumeration<String> names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
Object value = session.getAttribute(name);
session.setAttribute(name, value);
}
RedissonSession sess = (RedissonSession) findSession(session.getId());
sess.save();
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册