Cache objects for a session?

112 views
Skip to first unread message

Beau

unread,
Apr 2, 2012, 2:02:05 AM4/2/12
to web.py
Howdy.

Been playing around with web.py and mimerender to create a simple site
that translates other connections into RESTful urls (with json/html/
xml responses).

Some of this URLs currently open a TCP connection, and close it upon
completion. This works fine, however it's probably not the best going
forward. What I would like to do is cache the connection object for a
'session'. So if the user continually polls my service for data, it's
not creating a new connection, getting the data and then closing it.
I'd rather it lookup the connection and use an existing one.

I've thought about using sessions, then using a dictionary for the
sessionid to contain the object info etc. This could work, but I would
like to close the connection if the session expires.

Is there a method/interface that gets called when a session expires?.
So I don't leave open TCP connections around the place.

Is it possible to store the session info in memory only?. The session
information does not need to be persistent, or even last longer than
one minute (perfectly acceptable to open a new connection if the
requests are one minute apart, but some may be 100ms apart). So
writing to the disk seems odd :S.

Beau

unread,
Apr 19, 2012, 3:21:46 AM4/19/12
to web.py

I'm surprised no one has attempted to do anything similar ever
Surely you'd want to be caching non permanent data in a session
somehow. Isn't that half the point of sessions?

Andrey Kuzmin

unread,
Apr 19, 2012, 4:29:55 AM4/19/12
to we...@googlegroups.com
I don't know what you mean, webpy's session module has various backend storages (eg. File of Database) and its persistent at its lifetime. And you can specify session expiration time. 

NSC

unread,
Apr 19, 2012, 11:45:23 AM4/19/12
to we...@googlegroups.com
I recently had a similar question, and I'm not sure "memory" sessions are possible.  Unless someone corrects me, I do not believe web.py Sessions have a "memory" option, like Apache or IIS.  You have to tell the Session storage object to either use disk or db, where the storage pickle evidently gets written.

But, I'm testing a workaround, that's not specifically a web.py thing, just python.

In my main startup routine, I import a global.py file, which simply has a couple of vars defined.  Then, *before* app.run(), I'm setting those values.  (things like a debug level, etc).  Thanks to the magic of python imports, any other code file that imports that module can access those variables.

Requests to web.py are ephemeral, but the main process runs until you kill it.  So, essentially I'm sticking stuff in the main process, and accessing it from the request threads.

Unless I'm completely retarded, it seems to be working.

Ben Corneau

unread,
Apr 22, 2012, 10:51:41 PM4/22/12
to we...@googlegroups.com
The two built in Session Storage objects are DiskStore and DBStore, however web.py makes it easy to use a custom session store.  Writing a Memcache backed session store class is very straight forward.  If you want to keep your sessions in memory I would highly recommend this approach.


--
You received this message because you are subscribed to the Google Groups "web.py" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webpy/-/lCwqGm8P4_EJ.

To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.

W. Martin Borgert

unread,
Apr 23, 2012, 2:22:56 AM4/23/12
to we...@googlegroups.com
On 2012-04-22 22:51, Ben Corneau wrote:
> The two built in Session Storage objects are DiskStore and DBStore, however
> web.py makes it easy to use a custom session store. Writing a Memcache
> backed session store class is very straight forward. If you want to keep
> your sessions in memory I would highly recommend this approach.

A memory session object (however, not memcache) has been posted
to this list by Michele Petrazzo in 2008:

http://groups.google.com/group/webpy/browse_thread/thread/5e1c36fd6bd43900

I use it since some years and it works well for me. I would like
to see this code in web.py.

Reply all
Reply to author
Forward
0 new messages