PyCache, a Python port of PHPCache

If you write web pages in Python and find your complex database queries are slowing down you page load times then you might want to consider a caching solution. While most advanced frameworks (like Django) provide their own internal caching solution, basic frameworks and barebone sites are left to dry.

PyCache allows you to store and retrieve values as well as set expiration times to invalidate that data. PHPCache, the inspiration for PyCache, provides a good example:

Suppose that your nasty script gets 100 hits per second, and you cache your script using [PyCache] for just 1 minute at a time, the result is, in that one minute your server will run that piece of code only once.

[PyCache] will only query the database for the result that was stored in the database 100 times in that one minute.
This will be a very quick process, since it’s only a primary key lookup (piece a cake) for MySQL and SQLite. These databases can handle 1000s of simple queries like these per second! But your server might only be able to process your code 5 times per second…

In fact I tested this and SQLite could retrieve the cache 2,300 per second, MySQL around 2,500 times!

PyCache provides a set of methods for data manipulation:

  • store(key, value, expires) – This adds or updates a value in the cache.
  • get(key) – Retrieves a value from the cache or throws an exception if it has expired.
  • expire(key) – Manually expires a key/value pair in the cache.
  • gc() – Garbage collection: removes all values from the cache that have expired.

You can also use an instance of PyCache like a dictionary to set and retrieve values. If the variable cache is an instance then following three statements are valid.

  • cache['keyname'] – Functions like get('keyname').
  • cache['keyname'] = 'test' – Functions like set('keyname', 'test', time() + cache.default_length).
  • del cache['keyname'] – Functions like expire('keyname').

PyCache uses a storage interface like PHPCache which allows you to create new storage backends in addition to the Sqlite and MySQL ones included already. There are only 5 methods which you must implement for a backend to be usable by PyCache and they can be seen here.

While PyCache is still in its infancy in terms of development, it is currently functional and could be used with dramatic results. The methods and storage interface has not yet been finalized so if you choose to adopt you might have to modify your code to be compatible with later upgrades but those changes would be extremely minimal and clearly described in a wiki page.

You can view all the details of the project here in the mine.

Written on: 02-05-09 · No Comments » · Permalink

Leave a Reply