Midware beaker using Google App Engine Memcache API as backend

What

Beaker is a library for caching and sessions for use with web applications and stand-alone Python scripts and applications. — Beaker Offcial Document

Why

I recently writting a small app running on Google App Engine, Beaker is the ideal midware working together with light weight python web frameworks, eg bottle, web.py, to handle session things.

The GAE provides a bunch of external services, which are extremely convinent for building apps. Beaker supports using google’s Datastore api as backend, but operations over the Datastore is expensive (actually still free to me, hah), it would be more delightful to use the (currently) unlimited memcache service.

Beaker supports memcached backend, too, but only to those ordinary APIs, like pylibmc, cmemcache which connect to specified addresses as a client. The GAE memcache however, with exactly same interfaces, but no need to connect to somewhere before operating data. So, it needs some work over Beaker.

Patch

  1. Download this googlememcache.py ext module and save as beaker/ext/googlememcache.py
  2. Apply the following patch in the beaker directory. (Minor modification over cache.py)
diff -r 0bf25d5b4287 beaker/cache.py
--- a/beaker/cache.py   Mon Mar 12 14:19:57 2012 -0700
+++ b/beaker/cache.py   Wed Apr 11 17:29:30 2012 +0800
@@ -18,6 +18,7 @@
 import beaker.ext.database as database
 import beaker.ext.sqla as sqla
 import beaker.ext.google as google
+import beaker.ext.googlememcache as googlememcache
 
 # Initialize the cache region dict
 cache_regions = {}
@@ -115,6 +116,7 @@
           'ext:database':database.DatabaseNamespaceManager,
           'ext:sqla': sqla.SqlaNamespaceManager,
           'ext:google': google.GoogleNamespaceManager,
+          'ext:googlememcache': googlememcache.GoogleMemcacheNamespaceManager,
           })

Usage

Nothing much different from using the original ext:google

1
2
3
4
5
6
session_opts = {
    'session.cookie_expires': True,
    'session.type': 'ext:googlememcache',
}
 
app = SessionMiddleware(orig_app, session_opts)

Upstream

I submitted this patch to the Beaker offcial, not sure if they will adopt it as a new feature in the later releases.

文章分类 web技术 标签: , , , ,
One comment on “Midware beaker using Google App Engine Memcache API as backend
  1. Hernán说道:

    What about using ndb? It has memcache included.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*