Welcome to pycached’s documentation!

Installing

  • pip install pycached
  • pip install pycached[redis]

Usage

Using a cache is as simple as

>>> from pycached import Cache
>>> cache = Cache()
>>> cache.set('key', 'value')
True
>>> cache.get('key')
'value'

Here we are using the SimpleMemoryCache but you can use any other listed in Caches. All caches contain the same minimum interface which consists on the following functions:

  • add: Only adds key/value if key does not exist. Otherwise raises ValueError.
  • get: Retrieve value identified by key.
  • set: Sets key/value.
  • multi_get: Retrieves multiple key/values.
  • multi_set: Sets multiple key/values.
  • exists: Returns True if key exists False otherwise.
  • increment: Increment the value stored in the given key.
  • delete: Deletes key and returns number of deleted items.
  • clear: Clears the items stored.
  • raw: Executes the specified command using the underlying client.

You can also setup cache aliases like in Django settings:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from pycached import caches, SimpleMemoryCache, RedisCache
from pycached.serializers import StringSerializer, PickleSerializer

caches.set_config({
    'default': {
        'cache': "pycached.SimpleMemoryCache",
        'serializer': {
            'class': "pycached.serializers.StringSerializer"
        }
    },
    'redis_alt': {
        'cache': "pycached.RedisCache",
        'endpoint': "127.0.0.1",
        'port': 6379,
        'timeout': 1,
        'serializer': {
            'class': "pycached.serializers.PickleSerializer"
        },
        'plugins': [
            {'class': "pycached.plugins.HitMissRatioPlugin"},
            {'class': "pycached.plugins.TimingPlugin"}
        ]
    }
})


def default_cache():
    cache = caches.get('default')  # This always returns the same instance
    cache.set("key", "value")

    assert cache.get("key") == "value"
    assert isinstance(cache, SimpleMemoryCache)
    assert isinstance(cache.serializer, StringSerializer)


def alt_cache():
    # This generates a new instance every time! You can also use `caches.create('alt')`
    # or even `caches.create('alt', namespace="test", etc...)` to override extra args
    cache = caches.create(**caches.get_alias_config('redis_alt'))
    cache.set("key", "value")

    assert cache.get("key") == "value"
    assert isinstance(cache, RedisCache)
    assert isinstance(cache.serializer, PickleSerializer)
    assert len(cache.plugins) == 2
    assert cache.endpoint == "127.0.0.1"
    assert cache.timeout == 1
    assert cache.port == 6379
    cache.close()


def test_alias():
    default_cache()
    alt_cache()

    cache = RedisCache()
    cache.delete("key")
    cache.close()

    caches.get('default').close()


if __name__ == "__main__":
    test_alias()

In examples folder you can check different use cases:

Indices and tables