On Mon, Jan 30, 2012 at 10:40 AM, John Moore <[email protected]> wrote: > On 30/01/12 18:22, Les Hazlewood wrote: >> >> There could be, depending on how long your TTL is. However, this is >> easily enough solved IMO using a ThreadLocal implementation that >> simulates a local cache: >> >> Look up the object in the ThreadLocal - if its ID matches the DAO ID >> argument, return it. If it doesn't match, clear out the ThreadLocal, >> look up the session object from the underlying store and then store it >> in the ThreadLocal. Also overwrite the ThreadLocal value during a >> write operation. >> >> This logic probably will go into Shiro's implementation for 1.3, but >> that kind of gives you an idea if you need this right away. (Of >> course if you do work on this and you are capable, any patches would >> be appreciated!) >> > What I've done for the meantime, to check things out, is to use EHCache for > my local cache, with a TTL of 1 second. In my 'get' method I first check to > see if the session object exists in the cache and if so, use it, otherwise I > get it from Redis and store it in the local cache. In the 'put' I put the > object in Redis and the local cache as well. Does this sound right? It looks > like it should reduce the network calls by 90%.
Sounds good - should work well. I'd also ensure that the EHCache config reflects local/in-memory storage only - overflow to disk would probably slow things down unnecessarily. I'm not sure what you mean > by "if its ID matches the DAO ID argument, return it" above. That comment was targeted specifically at a thread-local solution. The reason is that if the ThreadLocal isn't cleared properly (for whatever reason), a ThreadLocally-cached SimpleSession instance may span across requests. Therefore you'd only want to use the ThreadLocal value IFF its ID matched the session ID used as a method argument to SessionDAO.getSession. If they don't match, the ThreadLocal value would be considered stale and unusable. HTH, -- Les Hazlewood CTO, Katasoft | http://www.katasoft.com | 888.391.5282 twitter: @lhazlewood | http://twitter.com/lhazlewood katasoft blog: http://www.katasoft.com/blogs/lhazlewood personal blog: http://leshazlewood.com
