Google App Engine (python) persistent cache: memcache and cloud storage
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
.gitignore
README.md
__init__.py
app.yaml
main.py

README.md

Google App Engine Cache with real persistence

With this simple cache you can truly persist in GAE cache items: in memcache and in cloud storage. Memcache is volatile, so, with persistence in cloudstorage you can guarantee items are available always.

How to use

	from lib.gae_cache import cache
  • cache.set(key, value[, ttl=0][, maxsize=1000000])

    • ttl: in seconds, default cache ttl is 0, unlimited

    • maxsize: GAE has a limit for each cache entry. Default is 1000000 of bytes. You can setup other size: gae_cache splits content in blocks of 1000000 of bytes and set cache keys with "_N" (key_1,key_2, ..., key_N)

  • cache.get(key)

  • cache.remove(key)

Requirements and dependencies

This new version requires to activate the cloud storage default bucket in your application: https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/activate

You have to put the client library in you applicacion/lib directory: https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/download

Sample code

		import webapp2
		from google.appengine.api import urlfetch

		from lib.gae_cache import cache

		class MainHandler(webapp2.RequestHandler):
			def get(self):
				url = "http://loripsum.net/api/9000/short/headers"; #1.5MB aprox request html, gae_cache.cache will split it in 2 blocks
				c = cache.get("content") 
				
				if c is None:

					try:
						c = urlfetch.fetch(url, deadline=60).content
						cache.set("content", c, 15) #ttl = 15 seconds
						c = "from live <br /><br /><br />" + c
					except:
						c = "Unexpected error"		
				else:
					c = "from cache <br /><br /><br />" + c 

				self.response.out.write(c[0:3000] + "[...]")

		app = webapp2.WSGIApplication([('/', MainHandler)],debug=True)

Sample app

Sample app is running here: http://gae-cache-testing.appspot.com/