Plugins¶
Plugins can be used to enrich the behavior of the cache. By default all caches are configured without any plugin but can add new ones in the constructor or after initializing the cache class:
>>> from aiocache import Cache
>>> from aiocache.plugins import TimingPlugin
cache = Cache(plugins=[HitMissRatioPlugin()])
cache.plugins += [TimingPlugin()]
You can define your custom plugin by inheriting from BasePlugin and overriding the needed methods (the overrides NEED to be async). All commands have pre_<command_name>
and post_<command_name>
hooks.
Warning
Both pre and post hooks are executed awaiting the coroutine. If you perform expensive operations with the hooks, you will add more latency to the command being executed and thus, there are more probabilities of raising a timeout error. If a timeout error is raised, be aware that previous actions won’t be rolled back.
A complete example of using plugins:
1import asyncio
2import random
3import logging
4
5from aiocache import Cache
6from aiocache.plugins import HitMissRatioPlugin, TimingPlugin, BasePlugin
7
8
9logger = logging.getLogger(__name__)
10
11
12class MyCustomPlugin(BasePlugin):
13
14 async def pre_set(self, *args, **kwargs):
15 logger.info("I'm the pre_set hook being called with %s %s" % (args, kwargs))
16
17 async def post_set(self, *args, **kwargs):
18 logger.info("I'm the post_set hook being called with %s %s" % (args, kwargs))
19
20
21cache = Cache(
22 plugins=[HitMissRatioPlugin(), TimingPlugin(), MyCustomPlugin()],
23 namespace="main")
24
25
26async def run():
27 await cache.set("a", "1")
28 await cache.set("b", "2")
29 await cache.set("c", "3")
30 await cache.set("d", "4")
31
32 possible_keys = ["a", "b", "c", "d", "e", "f"]
33
34 for t in range(1000):
35 await cache.get(random.choice(possible_keys))
36
37 assert cache.hit_miss_ratio["hit_ratio"] > 0.5
38 assert cache.hit_miss_ratio["total"] == 1000
39
40 assert cache.profiling["get_min"] > 0
41 assert cache.profiling["set_min"] > 0
42 assert cache.profiling["get_max"] > 0
43 assert cache.profiling["set_max"] > 0
44
45 print(cache.hit_miss_ratio)
46 print(cache.profiling)
47
48
49async def test_run():
50 await run()
51 await cache.delete("a")
52 await cache.delete("b")
53 await cache.delete("c")
54 await cache.delete("d")
55
56
57if __name__ == "__main__":
58 asyncio.run(test_run())
BasePlugin¶
- class aiocache.plugins.BasePlugin[source]¶
-
- async post_add(*args, **kwargs)¶
- async post_clear(*args, **kwargs)¶
- async post_delete(*args, **kwargs)¶
- async post_exists(*args, **kwargs)¶
- async post_expire(*args, **kwargs)¶
- async post_get(*args, **kwargs)¶
- async post_increment(*args, **kwargs)¶
- async post_multi_get(*args, **kwargs)¶
- async post_multi_set(*args, **kwargs)¶
- async post_raw(*args, **kwargs)¶
- async post_set(*args, **kwargs)¶
- async pre_add(*args, **kwargs)¶
- async pre_clear(*args, **kwargs)¶
- async pre_delete(*args, **kwargs)¶
- async pre_exists(*args, **kwargs)¶
- async pre_expire(*args, **kwargs)¶
- async pre_get(*args, **kwargs)¶
- async pre_increment(*args, **kwargs)¶
- async pre_multi_get(*args, **kwargs)¶
- async pre_multi_set(*args, **kwargs)¶
- async pre_raw(*args, **kwargs)¶
- async pre_set(*args, **kwargs)¶
TimingPlugin¶
- class aiocache.plugins.TimingPlugin[source]¶
Calculates average, min and max times each command takes. The data is saved in the cache class as a dict attribute called
profiling
. For example, to access the average time of the operation get, you can docache.profiling['get_avg']
- async post_add(client, *args, took=0, **kwargs)¶
- async post_clear(client, *args, took=0, **kwargs)¶
- async post_delete(client, *args, took=0, **kwargs)¶
- async post_exists(client, *args, took=0, **kwargs)¶
- async post_expire(client, *args, took=0, **kwargs)¶
- async post_get(client, *args, took=0, **kwargs)¶
- async post_increment(client, *args, took=0, **kwargs)¶
- async post_multi_get(client, *args, took=0, **kwargs)¶
- async post_multi_set(client, *args, took=0, **kwargs)¶
- async post_raw(client, *args, took=0, **kwargs)¶
- async post_set(client, *args, took=0, **kwargs)¶
HitMissRatioPlugin¶
- class aiocache.plugins.HitMissRatioPlugin[source]¶
Calculates the ratio of hits the cache has. The data is saved in the cache class as a dict attribute called
hit_miss_ratio
. For example, to access the hit ratio of the cache, you can docache.hit_miss_ratio['hit_ratio']
. It also provides the “total” and “hits” keys.