Skip to content

Caching results from common API calls

Jim Allman edited this page Feb 8, 2015 · 6 revisions

The Open Tree webapps make frequent calls to a few API methods, often requesting the same data (e.g. arguson views of major clades). For performance reasons, and to relieve stress on the API servers, we've opted to cache these results using web2py.

Initially, cached values are stored in RAM and set to never expire. To clear all cached values (after each synthesis release or other change in source data), simply restart web2py.

Web2py uses a @cache decorator to designate controller actions whose responses will be cached. Arguments to this decorator are evaluated on each request, include one which lets us define a unique cache key for each method call and its arguments, for example:

  /cached/taxomachine/v1/getContextsJSON
  /cached/treemachine/v1/getSyntheticTree?format=arguson&maxDepth=3&subtreeNodeID=170042&treeID=otol.draft.22

The "query string" portion of this key is reconstructed from request.vars, so it captures all arguments, whether originally sent via GET or POST. For example, this is needed to distinguish calls to getSyntheticTree, which would otherwise all return a single response (a single arguson view).

Special considerations for disk-based caching

If caching these variables in RAM creates problems, we can change a single line of code to switch to a filesystem-based "disk cache". These values would survive a web2py restart, so the cache would need to be cleared explicitly using either of these web2py cache methods:

cache.ram(key, None)          # clear a single value using its unique key
cache.ram.clear(regex='...')  # clear all values with keys matching this regex
Clone this wiki locally