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 = cache_utils.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
def __init__(self, ie_job): self.new_objects = defaultdict(structures.CaseInsensitiveDict) self.shared_state = {} self.response_data = [] self.cache_manager = cache_utils.get_cache_manager() self.ie_job = ie_job self.exportable = get_exportables()
def test_memcache_flushing(self): """Test if memcache is properly cleaned on object creation Procedure to test functionality: 1) load and permissions for specific user and store them in memcahe 2) emulate new object creation, which cleans permissions in memcache 3) make request which tries to get cache for permissions from memcache Also, it's assumed that 2 or more GGRC workers are running """ client = cache_utils.get_cache_manager().cache_object.memcache_client client.flush_all() # load perms and store them in memcache self.load_perms(11, {"11": "a"}) # emulate situation when a new object is created # this procedure cleans memcache in the end cache_utils.clear_permission_cache() # emulate work of worker #1 - get permissions for our user # the first step - check permissions in memcache ggrc_basic_permissions.query_memcache(client, "permissions:11") # step 2 - load permissions from DB and save then into memcahe # this step is omitted # load permission on behalf of worker #2, before step 2 of worker #1 result = self.load_perms(11, {"11": "b"}) # ensure that new permissions were returned instead of old ones self.assertEquals(result, {"11": "b"})
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 = cache_utils.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_data = cache.get(key) if permissions_data: # permissions_cache is stored in compressed state, # need to decompress it before using permissions_cache = cPickle.loads(zlib.decompress(permissions_data)) # 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
def test_memcache_flushing(self): """Test if memcache is properly cleaned on object creation Procedure to test functionality: 1) load and permissions for specific user and store them in memcahe 2) emulate new object creation, which cleans permissions in memcache 3) make request which tries to get cache for permissions from memcache Also, it's assumed that 2 or more GGRC workers are running """ client = cache_utils.get_cache_manager().cache_object.memcache_client client.flush_all() # load perms and store them in memcache self.load_perms(11, {"11": "a"}) # emulate situation when a new object is created # this procedure cleans memcache in the end cache_utils.clear_permission_cache() # emulate work of worker #1 - get permissions for our user # the first step - check permissions in memcache ggrc_basic_permissions.query_memcache(client, "permissions:11") # step 2 - load permissions from DB and save then into memcahe # this step is omitted # load permission on behalf of worker #2, before step 2 of worker #1 result = self.load_perms(11, {"11": "b"}) # ensure that new permissions were returned instead of old ones self.assertEquals(result, {"11": "b"})
def __init__(self, ids_by_type, exportable_queries=None, ie_job=None): super(ExportConverter, self).__init__() self.dry_run = True # TODO: fix ColumnHandler to not use it for exports self.block_converters = [] self.ids_by_type = ids_by_type self.exportable_queries = exportable_queries or [] self.ie_job = ie_job self.cache_manager = cache_utils.get_cache_manager()
def expire_ie_cache(ie_job): """Expire export status cache to force DB request.""" cache_manager = cache_utils.get_cache_manager() cache_key = cache_utils.get_ie_cache_key(ie_job) cache_manager.cache_object.memcache_client.delete(cache_key)
def _get_memcache_client(): """Return memcache client if it's enabled, otherwise return None""" if not cache_utils.has_memcache(): return None return cache_utils.get_cache_manager().cache_object.memcache_client
def _get_memcache_client(): """Return memcache client if it's enabled, otherwise return None""" if not cache_utils.has_memcache(): return None return cache_utils.get_cache_manager().cache_object.memcache_client