Hi,

on 2008-04-26, Michele Petrazzo posted code on this list
that implemented a MemoryStore. This is very useful for
embedded systems, where you want to minimise disk writes.
I use Micheles implementation since two years and it
works for me. Thanks to Michele (cc) for the enhancemnt!

Thanks for considering!

PS Micheles code is here:

class MemoryStore(web.session.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]

--
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to