Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

New Store mechanism to use with Google App Engine #223

Open
wants to merge 1 commit into from

1 participant

@eduardohenriquearnold

Creates a Store mechanism to use with sessions when developing to Google App Engine. This is based on their own data storage system. Other methods based on file storage don't work because GAE doesn't allow file writing.
Credits to yejun (https://github.com/yejun)

An example code is:

session = web.session.Session(app, web.session.GoogleStore())
eduardohenriquearnold Enable sessions on Google App Engine
Creates a Store mechanism to use with sessions when developing to Google App Engine. This is based on their own data storage system. Other methods based on file storage don't work because GAE doesn't allow file writing.
Credits to yejun (https://github.com/yejun)
5807e22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 15, 2013
  1. Enable sessions on Google App Engine

    eduardohenriquearnold authored
    Creates a Store mechanism to use with sessions when developing to Google App Engine. This is based on their own data storage system. Other methods based on file storage don't work because GAE doesn't allow file writing.
    Credits to yejun (https://github.com/yejun)
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 0 deletions.
  1. +39 −0 web/session.py
View
39 web/session.py
@@ -352,6 +352,45 @@ def cleanup(self, timeout):
atime, v = self.shelf[k]
if now - atime > timeout :
del self[k]
+
+try:
+ from google.appengine.ext import db
+
+ CLEANUP_BATCH_SIZE = 20
+
+ class WebpySession(db.Model):
+ """Model used to save session data to Google App Engine DB"""
+ data = db.BlobProperty()
+ atime = db.DateTimeProperty(auto_now=True)
+
+ class GoogleStore(Store):
+ """Google Datastore"""
+ def __init__(self, prefix="p"):
+ self.prefix=prefix
+
+ def __contains__(self, key):
+ return WebpySession.get_by_key_name(self.prefix+key) is not None
+
+ def __getitem__(self, key):
+ try:
+ return pickle.loads(WebpySession.get_by_key_name(self.prefix+key).data)
+ except AttributeError:
+ raise KeyError, key
+
+ def __setitem__(self, key, value):
+ r = WebpySession.get_or_insert(self.prefix+key)
+ r.data = pickle.dumps(value)
+ r.put()
+
+ def __delitem__(self, key):
+ try:
+ WebpySession.get_by_key_name(self.prefix+key).delete()
+ except AttributeError:
+ pass
+ def cleanup(self, timeout):
+ db.delete(WebpySession.all().filter('atime <', datetime.datetime.now()-datetime.timedelta(seconds=timeout)).fetch(CLEANUP_BATCH_SIZE))
+except ImportError:
+ pass
if __name__ == '__main__' :
import doctest
Something went wrong with that request. Please try again.