Utils Cache
inyoka.utils.cache
The caching infrastructure of Inyoka.
On top of the django cache client that speaks directly to either redis
or caches in-memory we have a RequestCache
that caches
redis-commands in a thread-local dictionary. This saves a lot of
redis-commands in some scenarios.
- copyright:
2007-2024 by the Inyoka Team, see AUTHORS for more details.
- license:
BSD, see LICENSE for more details.
- class inyoka.utils.cache.QueryCounter(cache_key, query, use_task=False, timeout=None)
Calls .count() for a query and saves this value into redis.
- db_count(write_cache=False)
Executes the query with .count() and returns the value.
If write_cache is True, then the value is also written to the cache.
- decr(count=1)
Decrease the counter by count.
Does nothing if the counter is not in the cache.
- delete_cache()
Deletes the counter from the cache.
This should only be used for debugging.
- incr(count=1)
Adds count to the counter.
Does nothing if the counter is not in the cache.
- value(default=None, calculate=True)
Returns the value from the cache.
If the value is not in the cache and this object was initialized with task, then the task is executed with celery and default is returned.
In other case cache.get_or_set() is used to create the value. This blocks all requests until the value is created, so this should only be done for fast queries.
If the value is not in the cache, the counter was initialized with use_task and calculate is False, then the counter is not calculated.
- class inyoka.utils.cache.RedisCache(server: str, params: Dict[str, Any])
Wrapper to redis cache that creates status keys for the time a value is created.
Idea from https://github.com/funkybob/puppy
- get_or_set(key, callback, timeout=None, update_time=6)
Get a key if it exists. Creates it if other case.
Sets a status key for the time the value is created, so other workers do not created the same content in the meantime.