Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Please consider the MemoryStore class by Michele Petrazzo, thanks #174

Open
wants to merge 2 commits into from

2 participants

@MartinBorg

Store sessions in memory instead of database or file.
I'm using this class since some years in production without any problems in an embedded system.

@travisbot

This pull request fails (merged cfd4340 into 3861897).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 15, 2012
  1. @MartinBorg
Commits on May 1, 2013
  1. @MartinBorg

    fix class name.

    MartinBorg authored
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 0 deletions.
  1. +46 −0 web/session.py
View
46 web/session.py
@@ -353,6 +353,52 @@ def cleanup(self, timeout):
if now - atime > timeout :
del self[k]
+
+# From: Michele Petrazzo <michele.petra...@gmail.com>
+# Date: Sat, 26 Apr 2008 09:55:20 -0700 (PDT)
+# Local: Sat, Apr 26 2008 6:55 pm
+# Subject: memory session
+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]
+
+
if __name__ == '__main__' :
import doctest
doctest.testmod()
Something went wrong with that request. Please try again.