From da72f73efb97bc1a8d32dae9646b00ed0446b785 Mon Sep 17 00:00:00 2001 From: Martin Bornhold Date: Thu, 14 Jul 2016 09:33:12 +0200 Subject: [PATCH] caches: Add an argument to make the cache context thread scoped. --- rhodecode/lib/caches.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/rhodecode/lib/caches.py b/rhodecode/lib/caches.py index 05de576f..46d0e1b4 100644 --- a/rhodecode/lib/caches.py +++ b/rhodecode/lib/caches.py @@ -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)