From: Fredrik Tolf Date: Mon, 26 Oct 2009 19:26:45 +0000 (+0100) Subject: Cache sessions per request. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=48ea770f8e60a40264ed002bd1cdb1f69cf0b780;p=jsvc.git Cache sessions per request. --- diff --git a/src/dolda/jsvc/util/PerSession.java b/src/dolda/jsvc/util/PerSession.java index deec822..716b154 100644 --- a/src/dolda/jsvc/util/PerSession.java +++ b/src/dolda/jsvc/util/PerSession.java @@ -88,7 +88,7 @@ public class PerSession implements Responder { public void respond(Request req) { Session sess = Session.get(req); Responder resp; - synchronized(sess) { + synchronized(this) { resp = (Responder)sess.get(rcl, null); if(resp == null) { resp = create(sess); diff --git a/src/dolda/jsvc/util/Session.java b/src/dolda/jsvc/util/Session.java index 31b65a7..cd4d916 100644 --- a/src/dolda/jsvc/util/Session.java +++ b/src/dolda/jsvc/util/Session.java @@ -6,6 +6,7 @@ import java.security.SecureRandom; public class Session implements java.io.Serializable { private static final Map sessions = new HashMap(); + private static final Map cache = new WeakHashMap(); private static final SecureRandom prng; private static long lastclean = 0; private final Map props = new HashMap(); @@ -24,38 +25,28 @@ public class Session implements java.io.Serializable { public void expire(Session sess); } - public void listen(Listener l) { - synchronized(ll) { - ll.add(l); - } + public synchronized void listen(Listener l) { + ll.add(l); } - public Object get(Object key, Object def) { - synchronized(props) { - if(props.containsKey(key)) - return(props.get(key)); - else - return(def); - } + public synchronized Object get(Object key, Object def) { + if(props.containsKey(key)) + return(props.get(key)); + else + return(def); } - public Object put(Object key, Object val) { - synchronized(props) { - return(props.put(key, val)); - } + public synchronized Object put(Object key, Object val) { + return(props.put(key, val)); } - private void expire() { - synchronized(ll) { - for(Listener l : ll) - l.expire(this); - } + private synchronized void expire() { + for(Listener l : ll) + l.expire(this); } - public static int num() { - synchronized(sessions) { - return(sessions.size()); - } + public synchronized static int num() { + return(sessions.size()); } private static String newid() { @@ -82,45 +73,47 @@ public class Session implements java.io.Serializable { return(sess); } - private static void clean() { + private synchronized static void clean() { long now = System.currentTimeMillis(); - synchronized(sessions) { - for(Iterator i = sessions.values().iterator(); i.hasNext();) { - Session sess = i.next(); - if(now > sess.atime + sess.etime) { - i.remove(); - sess.expire(); - } + for(Iterator i = sessions.values().iterator(); i.hasNext();) { + Session sess = i.next(); + if(now > sess.atime + sess.etime) { + i.remove(); + sess.expire(); } } } - public static Session get(Request req) { + public synchronized static Session get(Request req) { long now = System.currentTimeMillis(); if(now - lastclean > 3600 * 1000) { clean(); lastclean = now; } + Session sess = cache.get(req); + if(sess != null) + return(sess); + MultiMap cookies = Cookie.get(req); Cookie sc = cookies.get("jsvc-session"); - Session sess = null; - synchronized(sessions) { - if(sc != null) - sess = sessions.get(sc.value); - if(sess == null) { - String id = newid(); - sess = create(req); - sessions.put(id, sess); - sc = new Cookie("jsvc-session", id); - sc.expires = new Date(System.currentTimeMillis() + (86400L * 365L * 1000L)); - sc.path = req.ctx().sysconfig("jsvc.session.path", req.rooturl().getPath()); - String pd = req.ctx().sysconfig("jsvc.session.domain", null); - if(pd != null) - sc.domain = pd; - sc.addto(req); - } + + if(sc != null) + sess = sessions.get(sc.value); + if(sess == null) { + String id = newid(); + sess = create(req); + sessions.put(id, sess); + sc = new Cookie("jsvc-session", id); + sc.expires = new Date(System.currentTimeMillis() + (86400L * 365L * 1000L)); + sc.path = req.ctx().sysconfig("jsvc.session.path", req.rooturl().getPath()); + String pd = req.ctx().sysconfig("jsvc.session.domain", null); + if(pd != null) + sc.domain = pd; + sc.addto(req); } + + cache.put(req, sess); return(sess); }