UpdateMode.AFTER_REQUEST mode optimization. #1302

pull/1253/merge
Nikita 7 years ago
parent 233f392682
commit a55562c672

@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.catalina.session.StandardSession; import org.apache.catalina.session.StandardSession;
import org.redisson.api.RMap; import org.redisson.api.RMap;
import org.redisson.tomcat.RedissonSessionManager.ReadMode; import org.redisson.tomcat.RedissonSessionManager.ReadMode;
import org.redisson.tomcat.RedissonSessionManager.UpdateMode;
/** /**
* Redisson Session object for Apache Tomcat * Redisson Session object for Apache Tomcat
@ -37,11 +38,13 @@ public class RedissonSession extends StandardSession {
private final Map<String, Object> attrs; private final Map<String, Object> attrs;
private RMap<String, Object> map; private RMap<String, Object> map;
private final RedissonSessionManager.ReadMode readMode; 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); super(manager);
this.redissonManager = manager; this.redissonManager = manager;
this.readMode = readMode; this.readMode = readMode;
this.updateMode = updateMode;
try { try {
Field attr = StandardSession.class.getDeclaredField("attributes"); Field attr = StandardSession.class.getDeclaredField("attributes");
@ -144,7 +147,7 @@ public class RedissonSession extends StandardSession {
public void setAttribute(String name, Object value, boolean notify) { public void setAttribute(String name, Object value, boolean notify) {
super.setAttribute(name, value, notify); super.setAttribute(name, value, notify);
if (map != null && value != null) { if (updateMode == UpdateMode.DEFAULT && map != null && value != null) {
map.fastPut(name, value); map.fastPut(name, value);
} }
} }
@ -153,7 +156,7 @@ public class RedissonSession extends StandardSession {
protected void removeAttributeInternal(String name, boolean notify) { protected void removeAttributeInternal(String name, boolean notify) {
super.removeAttributeInternal(name, notify); super.removeAttributeInternal(name, notify);
if (map != null) { if (updateMode == UpdateMode.DEFAULT && map != null) {
map.fastRemove(name); map.fastRemove(name);
} }
} }

@ -17,7 +17,6 @@ package org.redisson.tomcat;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Enumeration;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
@ -159,7 +158,7 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
@Override @Override
public Session createEmptySession() { public Session createEmptySession() {
return new RedissonSession(this, readMode); return new RedissonSession(this, readMode, updateMode);
} }
@Override @Override
@ -218,18 +217,14 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
lifecycle.fireLifecycleEvent(STOP_EVENT, null); lifecycle.fireLifecycleEvent(STOP_EVENT, null);
} }
public void store(HttpSession session) { public void store(HttpSession session) throws IOException {
if (session == null) { if (session == null) {
return; return;
} }
if (updateMode == UpdateMode.AFTER_REQUEST) { if (updateMode == UpdateMode.AFTER_REQUEST) {
Enumeration<String> names = session.getAttributeNames(); RedissonSession sess = (RedissonSession) findSession(session.getId());
while (names.hasMoreElements()) { sess.save();
String name = names.nextElement();
Object value = session.getAttribute(name);
session.setAttribute(name, value);
}
} }
} }

@ -1,5 +1,7 @@
package org.redisson.tomcat; package org.redisson.tomcat;
import java.nio.file.Paths;
import org.apache.catalina.Engine; import org.apache.catalina.Engine;
import org.apache.catalina.Host; import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleException;
@ -56,7 +58,8 @@ public class TomcatServer {
localHost.setAutoDeploy(false); localHost.setAutoDeploy(false);
StandardContext rootContext = (StandardContext) server.createContext(contextPath, "webapp"); 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); localHost.addChild(rootContext);
Engine engine = server.createEngine(); Engine engine = server.createEngine();

@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.catalina.session.StandardSession; import org.apache.catalina.session.StandardSession;
import org.redisson.api.RMap; import org.redisson.api.RMap;
import org.redisson.tomcat.RedissonSessionManager.ReadMode; import org.redisson.tomcat.RedissonSessionManager.ReadMode;
import org.redisson.tomcat.RedissonSessionManager.UpdateMode;
/** /**
* Redisson Session object for Apache Tomcat * Redisson Session object for Apache Tomcat
@ -37,11 +38,14 @@ public class RedissonSession extends StandardSession {
private final Map<String, Object> attrs; private final Map<String, Object> attrs;
private RMap<String, Object> map; private RMap<String, Object> map;
private final RedissonSessionManager.ReadMode readMode; 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); super(manager);
this.redissonManager = manager; this.redissonManager = manager;
this.readMode = readMode; this.readMode = readMode;
this.updateMode = updateMode;
try { try {
Field attr = StandardSession.class.getDeclaredField("attributes"); Field attr = StandardSession.class.getDeclaredField("attributes");
attrs = (Map<String, Object>) attr.get(this); 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) { public void setAttribute(String name, Object value, boolean notify) {
super.setAttribute(name, value, notify); super.setAttribute(name, value, notify);
if (map != null && value != null) { if (updateMode == UpdateMode.DEFAULT && map != null && value != null) {
map.fastPut(name, value); map.fastPut(name, value);
} }
} }
@ -157,7 +161,7 @@ public class RedissonSession extends StandardSession {
protected void removeAttributeInternal(String name, boolean notify) { protected void removeAttributeInternal(String name, boolean notify) {
super.removeAttributeInternal(name, notify); super.removeAttributeInternal(name, notify);
if (map != null) { if (updateMode == UpdateMode.DEFAULT && map != null) {
map.fastRemove(name); map.fastRemove(name);
} }
} }

@ -17,12 +17,11 @@ package org.redisson.tomcat;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Enumeration;
import java.util.Map; import java.util.Map;
import org.apache.catalina.Context;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState; import org.apache.catalina.LifecycleState;
import org.apache.catalina.Session; import org.apache.catalina.Session;
@ -138,7 +137,7 @@ public class RedissonSessionManager extends ManagerBase {
@Override @Override
public Session createEmptySession() { public Session createEmptySession() {
return new RedissonSession(this, readMode); return new RedissonSession(this, readMode, updateMode);
} }
@Override @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) { if (session == null) {
return; return;
} }
if (updateMode == UpdateMode.AFTER_REQUEST) { if (updateMode == UpdateMode.AFTER_REQUEST) {
Enumeration<String> names = session.getAttributeNames(); RedissonSession sess = (RedissonSession) findSession(session.getId());
while (names.hasMoreElements()) { sess.save();
String name = names.nextElement();
Object value = session.getAttribute(name);
session.setAttribute(name, value);
}
} }
} }

@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.catalina.session.StandardSession; import org.apache.catalina.session.StandardSession;
import org.redisson.api.RMap; import org.redisson.api.RMap;
import org.redisson.tomcat.RedissonSessionManager.ReadMode; import org.redisson.tomcat.RedissonSessionManager.ReadMode;
import org.redisson.tomcat.RedissonSessionManager.UpdateMode;
/** /**
* Redisson Session object for Apache Tomcat * Redisson Session object for Apache Tomcat
@ -37,11 +38,13 @@ public class RedissonSession extends StandardSession {
private final Map<String, Object> attrs; private final Map<String, Object> attrs;
private RMap<String, Object> map; private RMap<String, Object> map;
private final RedissonSessionManager.ReadMode readMode; 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); super(manager);
this.redissonManager = manager; this.redissonManager = manager;
this.readMode = readMode; this.readMode = readMode;
this.updateMode = updateMode;
try { try {
Field attr = StandardSession.class.getDeclaredField("attributes"); Field attr = StandardSession.class.getDeclaredField("attributes");
@ -149,7 +152,7 @@ public class RedissonSession extends StandardSession {
public void setAttribute(String name, Object value, boolean notify) { public void setAttribute(String name, Object value, boolean notify) {
super.setAttribute(name, value, notify); super.setAttribute(name, value, notify);
if (map != null && value != null) { if (updateMode == UpdateMode.DEFAULT && map != null && value != null) {
map.fastPut(name, value); map.fastPut(name, value);
} }
} }
@ -158,7 +161,7 @@ public class RedissonSession extends StandardSession {
protected void removeAttributeInternal(String name, boolean notify) { protected void removeAttributeInternal(String name, boolean notify) {
super.removeAttributeInternal(name, notify); super.removeAttributeInternal(name, notify);
if (map != null) { if (updateMode == UpdateMode.DEFAULT && map != null) {
map.fastRemove(name); map.fastRemove(name);
} }
} }

@ -17,7 +17,6 @@ package org.redisson.tomcat;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Enumeration;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
@ -138,7 +137,7 @@ public class RedissonSessionManager extends ManagerBase {
@Override @Override
public Session createEmptySession() { public Session createEmptySession() {
return new RedissonSession(this, readMode); return new RedissonSession(this, readMode, updateMode);
} }
@Override @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) { if (session == null) {
return; return;
} }
if (updateMode == UpdateMode.AFTER_REQUEST) { if (updateMode == UpdateMode.AFTER_REQUEST) {
Enumeration<String> names = session.getAttributeNames(); RedissonSession sess = (RedissonSession) findSession(session.getId());
while (names.hasMoreElements()) { sess.save();
String name = names.nextElement();
Object value = session.getAttribute(name);
session.setAttribute(name, value);
}
} }
} }

@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.catalina.session.StandardSession; import org.apache.catalina.session.StandardSession;
import org.redisson.api.RMap; import org.redisson.api.RMap;
import org.redisson.tomcat.RedissonSessionManager.ReadMode; import org.redisson.tomcat.RedissonSessionManager.ReadMode;
import org.redisson.tomcat.RedissonSessionManager.UpdateMode;
/** /**
* Redisson Session object for Apache Tomcat * Redisson Session object for Apache Tomcat
@ -37,11 +38,13 @@ public class RedissonSession extends StandardSession {
private final Map<String, Object> attrs; private final Map<String, Object> attrs;
private RMap<String, Object> map; private RMap<String, Object> map;
private final RedissonSessionManager.ReadMode readMode; 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); super(manager);
this.redissonManager = manager; this.redissonManager = manager;
this.readMode = readMode; this.readMode = readMode;
this.updateMode = updateMode;
try { try {
Field attr = StandardSession.class.getDeclaredField("attributes"); Field attr = StandardSession.class.getDeclaredField("attributes");
@ -149,7 +152,7 @@ public class RedissonSession extends StandardSession {
public void setAttribute(String name, Object value, boolean notify) { public void setAttribute(String name, Object value, boolean notify) {
super.setAttribute(name, value, notify); super.setAttribute(name, value, notify);
if (map != null && value != null) { if (updateMode == UpdateMode.DEFAULT && map != null && value != null) {
map.fastPut(name, value); map.fastPut(name, value);
} }
} }
@ -158,7 +161,7 @@ public class RedissonSession extends StandardSession {
protected void removeAttributeInternal(String name, boolean notify) { protected void removeAttributeInternal(String name, boolean notify) {
super.removeAttributeInternal(name, notify); super.removeAttributeInternal(name, notify);
if (map != null) { if (updateMode == UpdateMode.DEFAULT && map != null) {
map.fastRemove(name); map.fastRemove(name);
} }
} }

@ -17,7 +17,6 @@ package org.redisson.tomcat;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Enumeration;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
@ -138,7 +137,7 @@ public class RedissonSessionManager extends ManagerBase {
@Override @Override
public Session createEmptySession() { public Session createEmptySession() {
return new RedissonSession(this, readMode); return new RedissonSession(this, readMode, updateMode);
} }
@Override @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) { if (session == null) {
return; return;
} }
if (updateMode == UpdateMode.AFTER_REQUEST) { if (updateMode == UpdateMode.AFTER_REQUEST) {
Enumeration<String> names = session.getAttributeNames(); RedissonSession sess = (RedissonSession) findSession(session.getId());
while (names.hasMoreElements()) { sess.save();
String name = names.nextElement();
Object value = session.getAttribute(name);
session.setAttribute(name, value);
}
} }
} }

Loading…
Cancel
Save