# Werkzeug

# Cache

The main problem with dynamic Web sites is, well, they’re dynamic. Each time a user requests a page, the webserver executes a lot of code, queries the database, renders templates until the visitor gets the page he sees.

This is a lot more expensive than just loading a file from the file system and sending it to the visitor.

For most Web applications, this overhead isn’t a big deal but once it becomes, you will be glad to have a cache system in place.

## How Caching Works

Caching is pretty simple. Basically you have a cache object lurking around somewhere that is connected to a remote cache or the file system or something else. When the request comes in you check if the current page is already in the cache and if, you’re returning it. Otherwise you generate the page and put it into the cache. (Or a fragment of the page, you don’t have to cache the full thing)

Here a simple example of how to cache a sidebar for a template:

    def get_sidebar(user):
        identifier = 'sidebar_for/user%d' % user.id
        value = cache.get(identifier)
        if value is not None:
            return value
        value = generate_sidebar_for(user=user)
        cache.set(identifier, value, timeout=60 * 5)
        return value

## Creating a Cache Object

To create a cache object you just import the cache system of your choice from the cache module and instanciate it. Then you can start working with that object:

    >>> from werkzeug.contrib.cache import SimpleCache
    >>> c = SimpleCache()
    >>> c.set("foo", "value")
    >>> c.get("foo")
    'value'
    >>> c.get("missing") is None
    True

Please keep in mind that you have to create the cache and put it somewhere you have access to it (either as a module global you can import or if you put it onto your WSGI application).

## Cache System API

class `werkzeug.contrib.cache.``BaseCache`(default_timeout=300)

  Baseclass for the cache systems. All the cache systems implement this API or a superset of it.

  Parameter: | default_timeout – the default timeout that is used if no timeout is specified on `set`.

  `add`(key, value, timeout=None)

    Works like `set` but does not override already existing values.

    Parameters: | key – the key to set value – the value for the key timeout – the cache timeout for the key or the default timeout if not specified.

  `clear`()
    Clears the cache. Keep in mind that not all caches support clearning of the full cache.
  `dec`(key, delta=1)

    Decrements the value of a key by delta. If the key does not yet exist it is initialized with -delta.

    For supporting caches this is an atomic operation.

    Parameters: | key – the key to increment. delta – the delta to subtract.

  `delete`(key)

    Deletes key from the cache. If it does not exist in the cache nothing happens.

    Parameter: | key – the key to delete.

  `delete_many`(*keys)

    Deletes multiple keys at once.

    Parameter: | keys – The function accepts multiple keys as positional arguments.

  `get`(key)

    Looks up key in the cache and returns it. If the key does not exist None is returned instead.

    Parameter: | key – the key to be looked up.

  `get_dict`(*keys)

    Works like `get_many` but returns a dict:

        d = cache.get_dict("foo", "bar")
        foo = d["foo"]
        bar = d["bar"]

    Parameter: | keys – The function accepts multiple keys as positional arguments.

  `get_many`(*keys)

    Returns a list of keys. For each key a item in the list is created. Example:

        foo, bar = cache.get_many("foo", "bar")

    If a key can’t be looked up None is returned for that key instead.

    Parameter: | keys – The function accepts multiple keys as positional arguments.

  `inc`(key, delta=1)

    Increments the value of a key by delta. If the key does not yet exist it is initialized with delta.

    For supporting caches this is an atomic operation.

    Parameters: | key – the key to increment. delta – the delta to add.

  `set`(key, value, timeout=None)

    Adds or overrides a key in the cache.

    Parameters: | key – the key to set value – the value for the key timeout – the cache timeout for the key or the default timeout if not specified.

  `set_many`(mapping, timeout=None)

    Sets multiple keys and values from a dict.

    Parameters: | mapping – a dict with the values to set. timeout – the cache timeout for the key or the default timeout if not specified.

## Cache Systems

class `werkzeug.contrib.cache.``NullCache`(default_timeout=300)

  A cache that doesn’t cache. This can be useful for unit testing.

  Parameter: | default_timeout – a dummy parameter that is ignored but exists for API compatibility with other caches.

class `werkzeug.contrib.cache.``SimpleCache`(threshold=500, default_timeout=300)

  Simple memory cache for single process environments. This class exists mainly for the development server and is not 100% thread safe. It tries to use as many atomic operations as possible and no locks for simplicity but it could happen under heavy load that keys are added multiple times.

  Parameters: | threshold – the maximum number of items the cache stores before it starts deleting some. default_timeout – the default timeout that is used if no timeout is specified on `set`.

class `werkzeug.contrib.cache.``MemcachedCache`(servers, default_timeout=300, key_prefix=None)

  A cache that uses memcached as backend.

  The first argument can either be a list or tuple of server addresses in which case Werkzeug tries to import the memcache module and connect to it, or an object that resembles the API of a `memcache.Client`.

  Implementation notes: This cache backend works around some limitations in memcached to simplify the interface. For example unicode keys are encoded to utf-8 on the fly. Methods such as `get_dict` return the keys in the same format as passed. Furthermore all get methods silently ignore key errors to not cause problems when untrusted user data is passed to the get methods which is often the case in web applications.

  Parameters: | servers – a list or tuple of server addresses or alternatively a `memcache.Client` or a compatible client. default_timeout – the default timeout that is used if no timeout is specified on `set`. key_prefix – a prefix that is added before all keys. This makes it possible to use the same memcached server for different applications. Keep in mind that `clear` will also clear keys with a different prefix.

class `werkzeug.contrib.cache.``GAEMemcachedCache`(default_timeout=300, key_prefix=None)

  Connects to the Google appengine memcached Cache.

  Parameters: | default_timeout – the default timeout that is used if no timeout is specified on `set`. key_prefix – a prefix that is added before all keys. This makes it possible to use the same memcached server for different applications. Keep in mind that `clear` will also clear keys with a different prefix.

class `werkzeug.contrib.cache.``FileSystemCache`(cache_dir, threshold=500, default_timeout=300)

  A cache that stores the items on the file system. This cache depends on being the only user of the cache_dir. Make absolutely sure that nobody but this cache stores files there or otherwise the chace will randomely delete files therein.

  Parameters: | cache_dir – the directory where cached files are stored. threshold – the maximum number of items the cache stores before it starts deleting some. default_timeout – the default timeout that is used if no timeout is specified on `set`.