memory session

52 views
Skip to first unread message

Michele Petrazzo

unread,
Apr 26, 2008, 12:55:20 PM4/26/08
to web.py
Hi all,
looking into the list, I see that was already asked a in memeory
session store, but someone said that it's not so useful (for a lot of
reasons). In some cases, I think, it can be very useful. I'm talking
about systems where there is no db and the filesystem are over a flash
memory, so thereis the problem on limited re-write and since sessions
can be a lot and change very offen, it can degrede the memory.
So I create (and attach) a simple MemoryStore that do the work


class MemoryStore(Store):
"""Store for saving a session in memory.
Usefull where there is limited fs writes on the disk, like
flash memories

I save the data into a dict:
k: (time, pydata)
"""
def __init__(self, d_store=None):
if d_store is None:
d_store = {}
self.d_store = d_store

def __contains__(self, key):
return key in self.d_store

def __getitem__(self, key):
""" Return the value and update the last seen value
"""
t, value = self.d_store[key]
self.d_store[key] = (time.time(), value)
return value

def __setitem__(self, key, value):
self.d_store[key] = (time.time(), value)

def __delitem__(self, key):
del self.d_store[key]

def cleanup(self, timeout):
now = time.time()
to_del = []
for k, (atime, value) in self.d_store.iteritems():
if now - atime > timeout :
to_del.append(k)

#to avoid exception on "dict change during iterations"
for k in to_del:
del self.d_store[k]

Bjorn Tipling

unread,
Apr 26, 2008, 1:00:11 PM4/26/08
to we...@googlegroups.com
Wouldn't each request end up with a new instance of this class?

Bjorn Tipling

unread,
Apr 26, 2008, 1:49:20 PM4/26/08
to we...@googlegroups.com
I think a good in memory session store would use a separate process and
unix sockets. I've been actually thinking of using memcache, just to
give it a try and learn how to use it.

Michele Petrazzo

unread,
Apr 27, 2008, 7:18:28 AM4/27/08
to web.py
On 26 Apr, 19:00, Bjorn Tipling <bjorn.tipl...@gmail.com> wrote:
> Wouldn't each request end up with a new instance of this class?
>

On my tests, no. This class has the same DiskStore behavior, so one
instances for all requests.

Michele Petrazzo

unread,
Apr 27, 2008, 7:23:01 AM4/27/08
to web.py

On 26 Apr, 19:49, Bjorn Tipling <bjorn.tipl...@gmail.com> wrote:
> I think a good in memory session store would use a separate process and
> unix sockets. I've been actually thinking of using memcache, just to
> give it a try and learn how to use it.
>

It's a nice idea!
If someone (the developers) wants, I can make some test code that use:
- memcache if exists on the machine
- pure unix-socket communication in the other case.

Bjorn Tipling

unread,
Apr 27, 2008, 11:20:59 AM4/27/08
to we...@googlegroups.com
How did you implement it? If the server is set up as cgi, I think it
would create a new instance for each request, wouldn't it?

> -------- Original Message --------
> Subject: [webpy] Re: memory session
> From: Michele Petrazzo <michele....@gmail.com>

Michele Petrazzo

unread,
Apr 28, 2008, 5:40:50 AM4/28/08
to web.py
On 27 Apr, 17:20, Bjorn Tipling <bjorn.tipl...@gmail.com> wrote:
> How did you implement it? If the server is set up as cgi, I think it
> would create a new instance for each request, wouldn't it?
>

My tests was only as stand alone server. I think that on cgi setup,
it cannot work.
Before start: normally I don't use cgi, so can be that I make some
technical mistakes in my thinks.

The hard way itìs to create a new process that talk with web.py
(instances) trought a single fs fifo (or python xmlrpc) and simplest
it's to use memcache (daemon) and the python client (python-
memcached). Just tried the tests on my etch and work like a charm.

The latter cost about in 40 lines of code, more or less my
MemoryStore, but has external dipendeces. The first it's the better
for webpy, but have to pay attention for the synchronous talks.

jlist9

unread,
Dec 30, 2009, 2:31:34 PM12/30/09
to we...@googlegroups.com
Thanks Michele! I tried it and it worked well.

I think this is a useful addition to the choices of session stores.
Is it possible to add it to web.py code base?

Jack

Reply all
Reply to author
Forward
0 new messages