beaker icon indicating copy to clipboard operation
beaker copied to clipboard

Session.load() should not set creation_time? aka how to iterate over all sessions stored in database

Open psranga opened this issue 11 years ago • 1 comments

First off, this is a great library. Thanks for making this!

I'm trying to implement a 'show all of my sessions' functionality in my app. So that the user can log out all others except the current one. I store sessions in a sqlite database using the SqlAlchemy backend.

My current method is to the open the session database using another database connection and iterate over the rows. For each row, the 'namespace' column is the ID of the session.

I then use 'session.load()' to pull out the session. This approach is working, except that load() seems to set the _creation_time to the current time.

Am I missing something?

Is there a better way to do what I want using Beaker?

Thanks!

psranga avatar Aug 29 '13 17:08 psranga

If you want to avoid updating the _creation_time when loading the session, you can achieve this by using get() instead of load(). The get() method allows you to retrieve the session without affecting its metadata, such as _creation_time.

Here's an example of how you can use get() to retrieve the session without updating the _creation_time:

from beaker.middleware import SessionMiddleware

# Assuming you have already set up your Beaker middleware
# app = SessionMiddleware(app, session_opts={...})

# Retrieve the session without updating _creation_time
session_id = "your_session_id_here"
session = app['beaker.session'].get(session_id)

# Now you have access to the session without altering its metadata
# You can perform your desired operations here, such as logging out other sessions

Using get() should give you the behavior you are looking for without affecting the _creation_time of the session. This way, you can implement your 'show all of my sessions' functionality in your app. Remember to handle any necessary locking or synchronization to avoid race conditions when manipulating sessions.

ljluestc avatar Aug 05 '23 19:08 ljluestc