Stateless requests conditionally
See: http://groups.google.com/group/liftweb/browse_thread/thread/fdb9d0df47de6f70#
For background, see: http://www.assembla.com/wiki/show/liftweb/Stateless_Requests
Many hits to our site are stateless (no session), but sessions are created and kept (for up to 30 mins) for all visitors (human or robot). We could benefit from the ability to:
1. Avoid creating sessions until necessary (e.g. imagine a user surfs for a while, then clicks add-to-cart, upon clicking add-to-cart a session could be established)
2. Avoid creating sessions for some visitors based on criteria such as user agent (e.g. never give googlebot crawlers a session)
I'll use some code here as an illustration of the concept only:
For background, see: http://www.assembla.com/wiki/show/liftweb/Stateless_Requests
Many hits to our site are stateless (no session), but sessions are created and kept (for up to 30 mins) for all visitors (human or robot). We could benefit from the ability to:
1. Avoid creating sessions until necessary (e.g. imagine a user surfs for a while, then clicks add-to-cart, upon clicking add-to-cart a session could be established)
2. Avoid creating sessions for some visitors based on criteria such as user agent (e.g. never give googlebot crawlers a session)
I'll use some code here as an illustration of the concept only:
object LiftRules {
type StatelessTestPF = PartialFunction[Req, StatelessMode]
val statelessTest = RulesSeq[StatelessTestPF]
}
object StatelessMode {
/** Establish sessions for visitors */
val stateful = 1
/** Never establish a session, throw an exception if state is stored */
val stateless = 2
/** Establish a session if anything is written to the session, e.g. as soon as something is added to the cart */
val statefulOnWrite = 3
/** Never establish a session, but allow writes to the session, data will be discarded at the end of the request */
val statelessAllowWrite = 4
}
LiftRules.statelessTest.append {
case GoogleBotReq(req) => StatelessMode.statelessAllowWrite
case _ => StatelessMode.statefulOnWrite
}
Leave a comment
(In revision:23bfc86a1619fe012b34c555b5757f65d956ee07) Closes #1094. Closes #1086. Improved handling of statelessness including a snippet trait that handles a given snippet in stateless mode and the ability to inspect cookies and user agents to determine if a request should be treated as stateless
Branch: master
Branch: master