I think there was a recent change in web2py whereby the session is no longer
saved if it has not been changed, and because session.forget() merely
prevents saving, I'm not sure it would be useful to you (unless you do
actually change the session but then for some reason do not want to save
it).
However, if you call session.forget(response), that will not only prevent
the session from being saved, but it will also immediately unlock the
session file. That could be useful if you will have multiple simultaneous
requests coming from the same user/session (e.g., Ajax calls, or multiple
browser tabs open) and don't want each request to have to wait for the
previous one to complete.
As for where to put session.forget() or session.forget(response), you should
insert it as early as it makes sense. For example, if every single function
in an entire controller file does not need the session, then right at the
top of your controller file (before any functions are defined), you could
do:
session.forget(response)
If you don't want it to apply to the entire controller, though, then you'll
have to put it within individual functions. Assuming the function does not
need the session at all, then you should put the call right at the beginning
of the function:
def your_function():
session.forget(response)
# remaining function code
If you'd rather manage session forgetting/unlocking centrally, I suppose you
could put some logic somewhere in a model file (all model files are run on
every request, so the logic would get checked on every request). For
example, you could do something like this:
if request.controller=='default' and request.function in ('index', 'func1',
'func2'):
session.forget(response)
If the above were in a model file, it would forget the session whenever
'index', 'func1', or 'func2' in the 'default' controller are called.
Anthony
On Saturday, April 9, 2011 3:02:10 AM UTC-4, 黄祥 wrote:
> hi,
>
> i want to follow the suggestion on web2py books
> http://web2py.com/book/default/chapter/11#Efficiency-and-Scalability
> :
> Set session.forget() in all controllers and/or functions that do not
> change the session.
> is it means that every controller i must use :
>
> def call():
> session.forget()
> return service()
>
> or i pass session.forget() to every function that i defined in
> controller, i mean.
> e.g.
>
> def blog():
> table = db.blog
> search, rows = crud.search(table)
> session.forget()
> return dict(search = search, rows = rows)
>
> def news():
> table = db.news
> search, rows = crud.search(table)
> session.forget()
> return dict(search = search, rows = rows)
>
> thank you so much