Skip to content
This repository

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

Open
wants to merge 2 commits into from

2 participants

MartinBorg Don't Add Me To Your Organization a.k.a The Travis Bot
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.

Don't Add Me To Your Organization a.k.a The Travis Bot

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

Showing 2 unique commits by 1 author.

Aug 15, 2012
MartinBorg MartinBorg Added MemoryStore for sessions by Michele Petrazzo. cfd4340
May 01, 2013
MartinBorg MartinBorg fix class name. 5327459
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 46 additions and 0 deletions. Show diff stats Hide diff stats

  1. +46 0 web/session.py
46 web/session.py
@@ -353,6 +353,52 @@ def cleanup(self, timeout):
353 353 if now - atime > timeout :
354 354 del self[k]
355 355
  356 +
  357 +# From: Michele Petrazzo <michele.petra...@gmail.com>
  358 +# Date: Sat, 26 Apr 2008 09:55:20 -0700 (PDT)
  359 +# Local: Sat, Apr 26 2008 6:55 pm
  360 +# Subject: memory session
  361 +class MemoryStore(Store):
  362 + """Store for saving a session in memory.
  363 + Usefull where there is limited fs writes on the disk, like
  364 + flash memories
  365 +
  366 + I save the data into a dict:
  367 + k: (time, pydata)
  368 + """
  369 + def __init__(self, d_store=None):
  370 + if d_store is None:
  371 + d_store = {}
  372 + self.d_store = d_store
  373 +
  374 + def __contains__(self, key):
  375 + return key in self.d_store
  376 +
  377 + def __getitem__(self, key):
  378 + """ Return the value and update the last seen value
  379 + """
  380 + t, value = self.d_store[key]
  381 + self.d_store[key] = (time.time(), value)
  382 + return value
  383 +
  384 + def __setitem__(self, key, value):
  385 + self.d_store[key] = (time.time(), value)
  386 +
  387 + def __delitem__(self, key):
  388 + del self.d_store[key]
  389 +
  390 + def cleanup(self, timeout):
  391 + now = time.time()
  392 + to_del = []
  393 + for k, (atime, value) in self.d_store.iteritems():
  394 + if now - atime > timeout :
  395 + to_del.append(k)
  396 +
  397 + #to avoid exception on "dict change during iterations"
  398 + for k in to_del:
  399 + del self.d_store[k]
  400 +
  401 +
356 402 if __name__ == '__main__' :
357 403 import doctest
358 404 doctest.testmod()

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.