def query_memcache(key): """Check if cached permissions are available Args: key (string): key of the stored permissions Returns: cache (memcache_client): memcache client or None if caching is not available permissions_cache (dict): dict with all permissions or None if there was a cache miss """ if not getattr(settings, 'MEMCACHE_MECHANISM', False): return None, None cache = _get_cache_manager().cache_object.memcache_client cached_keys_set = cache.get('permissions:list') or set() if key not in cached_keys_set: # We set the permissions:list variable so that we are able to batch # remove all permissions related keys from memcache cached_keys_set.add(key) cache.set('permissions:list', cached_keys_set, PERMISSION_CACHE_TIMEOUT) return cache, None permissions_cache = cache.get(key) if permissions_cache: # If the key is both in permissions:list and in memcache itself # it is safe to return the cached permissions return cache, permissions_cache return cache, None