def fetch_all(self): ''' Fetches all impressions from the cache. It returns a dictionary with the impressions grouped by feature name. :return: All cached impressions so far grouped by feature name :rtype: dict ''' impressions_list = list() impressions_keys = self._redis.keys(self._get_impressions_key('*')) for impression_key in impressions_keys: impression_key = bytes_to_string(impression_key) if (impression_key.replace(self._get_impressions_key(''), '') == 'impressions'): continue feature_name = impression_key.replace( self._get_impressions_key(''), '') for impression in self._redis.smembers(impression_key): impression = bytes_to_string(impression) impression_decoded = decode(impression) impression_tuple = Impression( key=impression_decoded['keyName'], feature_name=feature_name, treatment=impression_decoded['treatment'], time=impression_decoded['time']) impressions_list.append(impression_tuple) if not impressions_list: return dict() return self._build_impressions_dict(impressions_list)
def fetch_all_and_clear(self): ''' Fetches all impressions from the cache and clears it. It returns a dictionary with the impressions grouped by feature name. :return: All cached impressions so far grouped by feature name :rtype: dict ''' impressions_list = list() impressions_keys = self._redis.keys(self._get_impressions_key('*')) for impression_key in impressions_keys: impression_key = bytes_to_string(impression_key) if (impression_key.replace(self._get_impressions_key(''), '') == 'impressions'): continue feature_name = impression_key.replace( self._get_impressions_key(''), '') to_remove = list() for impression in self._redis.smembers(impression_key): to_remove.append(impression) impression = bytes_to_string(impression) impression_decoded = decode(impression) label = '' if 'label' in impression_decoded: label = impression_decoded['label'] change_number = -1 if 'changeNumber' in impression_decoded: change_number = impression_decoded['changeNumber'] bucketing_key = '' if 'bucketingKey' in impression_decoded: bucketing_key = impression_decoded['bucketingKey'] impression_tuple = Impression( matching_key=impression_decoded['keyName'], feature_name=feature_name, treatment=impression_decoded['treatment'], label=label, change_number=change_number, bucketing_key=bucketing_key, time=impression_decoded['time']) impressions_list.append(impression_tuple) self._redis.srem(impression_key, *set(to_remove)) if not impressions_list: return dict() return self._build_impressions_dict(impressions_list)
def get_split(self, split_name): """ Retrieves a Split from the cache. :param split_name: Name of the split (feature) :type split_name: str :return: The split under the name if it exists, None otherwise :rtype: Split """ to_decode = self._adapter.cache_get( self._KEY_TEMPLATE.format(suffix=split_name), _SPLITIO_SPLITS_CACHE_NAMESPACE) if to_decode is None: return None to_decode = bytes_to_string(to_decode) split_dump = decode(to_decode) if split_dump is not None: segment_cache = UWSGISegmentCache(self._adapter) split_parser = UWSGISplitParser(segment_cache) split = split_parser.parse(split_dump) return split return None
def _get_segment_key_set_key(self, segment_name): ''' Build cache key for a given segment key set. :param segment_name: The name of the segment :type segment_name: str :return: The cache key for the segment key set :rtype: str ''' segment_name = bytes_to_string(segment_name) return RedisSegmentCache._SEGMENT_KEY_SET_KEY_TEMPLATE.format( segment_name=segment_name)
def _get_segment_change_number_key(self, segment_name): ''' Build cache key for a given segment change_number. :param segment_name: The name of the segment :type segment_name: str :return: The cache key for the segment change number :rtype: str ''' segment_name = bytes_to_string(segment_name) return RedisSegmentCache._SEGMENT_CHANGE_NUMBER_KEY_TEMPLATE.format( segment_name=segment_name)
def split_names(self): """Get the name of fetched splits. :return: A list of str :rtype: list """ splits = self._split_cache.get_splits_keys() split_names = [] for split_name in splits: split_name = bytes_to_string(split_name) split_names.append(split_name) return split_names
def split_names(self): """Get the name of fetched splits. :return: A list of str :rtype: list """ splits = self._split_cache.get_splits_keys() split_names = [] for split_name in splits: split_name = bytes_to_string(split_name) split_names.append(split_name.replace(RedisSplitCache._KEY_TEMPLATE.format(suffix=''), '')) return split_names
def get_split(self, split_name): to_decode = self._redis.get(self._get_split_key(split_name)) if to_decode is None: return None to_decode = bytes_to_string(to_decode) split_dump = decode(to_decode) if split_dump is not None: segment_cache = RedisSegmentCache(self._redis) split_parser = RedisSplitParser(segment_cache) split = split_parser.parse(split_dump) return split return None
def get_splits(self): keys = self.get_splits_keys() splits = self._redis.mget(keys) to_return = [] segment_cache = RedisSegmentCache(self._redis) split_parser = RedisSplitParser(segment_cache) for split in splits: try: split = bytes_to_string(split) split_dump = decode(split) if split_dump is not None: to_return.append(split_parser.parse(split_dump)) except: self._logger.error( 'Error decoding/parsing fetched split or invalid split' ' format: %s' % split) return to_return