caches: Add an argument to make the cache context thread scoped.

This commit is contained in:
Martin Bornhold 2016-07-14 09:33:12 +02:00
parent 6fce04f6d1
commit da72f73efb

View file

@ -21,6 +21,7 @@
import beaker
import logging
import threading
from beaker.cache import _cache_decorate, cache_regions, region_invalidate
@ -170,7 +171,7 @@ class InvalidationContext(object):
safe_str(self.repo_name), safe_str(self.cache_type))
def __init__(self, compute_func, repo_name, cache_type,
raise_exception=False):
raise_exception=False, thread_scoped=False):
self.compute_func = compute_func
self.repo_name = repo_name
self.cache_type = cache_type
@ -178,6 +179,13 @@ class InvalidationContext(object):
repo_name, cache_type)
self.raise_exception = raise_exception
# Append the thread id to the cache key if this invalidation context
# should be scoped to the current thread.
if thread_scoped:
thread_id = threading.current_thread().ident
self.cache_key = '{cache_key}_{thread_id}'.format(
cache_key=self.cache_key, thread_id=thread_id)
def get_cache_obj(self):
cache_key = CacheKey.get_cache_key(
self.repo_name, self.cache_type)