Configuration

Cachy provides a unified API for various caching systems.

All you need to get you started is the configuration describing the various cache stores and passing it to a CacheManager instance.

from cachy import CacheManager

config = {
    'stores': {
        'redis': {
            'driver': 'redis',
            'host': 'localhost',
            'port': 6379,
            'db': 0
        }
    }
}

cache = CacheManager(config)

If you have multiple stores configured you can specify which one is the default:

from cachy import CacheManager

config = {
    'stores': {
        'redis': {
            'driver': 'redis',
            'host': 'localhost',
            'port': 6379,
            'db': 0
        },
        'memcached': {
            'driver' 'memcached',
            'servers': [
                '127.0.0.1:11211'
            ]
        }
    }
}

An example cache configuration is located at examples/config.py.

The cache configuration file contains various options, which are documented within the file, so make sure to read over these options.

Cache Prerequisites

Database

When using the database cache driver, you will need the Orator ORM. You will also need to setup a table to contain the cache items. You will find an example SchemaBuilder declaration for the table below:

with schema.create('cache') as table:
    table.string('key').unique()
    table.text('value')
    table.integer('expiration')

Memcached

When using the memcached driver, you will need either the pure-python python-memcached (python3-memcached) or the libmemcached wrapper, pylibmc.

{
    'memcached': {
        'driver': 'memcached',
        'servers': [
            '127.0.0.1:11211'
        ]
    }
}

Redis

You will need the redis library in order to use the redis driver.

{
    'redis': {
        'driver': 'memcached',
        'host': 'localhost',
        'port': 6379,
        'db': 0
    }
}

File

You do not need any extra package to use the file driver.

{
    'file': {
        'driver': 'file',
        'path': '/my/cache/directory'
    }
}

Dict

You do not need any extra package to use the dict driver.

{
    'dict': {
        'driver': 'dict'
    }
}

Serialization

By default, Cachy will serialize objects using the pickle library. However, this can be changed in the configuration, either globally or at driver level.

The possible values are pickle, json, msgpack.

config = {
    'default': 'redis',
    'serializer': 'pickle',
    'stores': {
        'redis': {
            'driver': 'redis',
            'serializer': 'json',
            'host': 'localhost',
            'port': 6379,
            'db': 0
        },
        'memcached': {
            'driver' 'memcached',
            'servers': [
                '127.0.0.1:11211'
            ]
        }
    }
}

Warning

The serializer you choose will determine which types of objects you can serialize, the pickle serializer being the more permissive.