Esempio n. 1
0
    def set_list(self, key, value, ttl=None, key_extractor=None):
        # locking it to prevent concurrency violation
        lock = self.lock(key)

        # check if item loaded
        v = self.get_list(key)
        if v[1]:
            self.unlock(lock)
            return v  # packed: (value_metadata, value)

        if key_extractor is None:

            def key_extractor(x):
                return x["id"]

        try:
            value_metadata, value = value() if callable(value) else value
            item_keys = []
            assert isinstance(value, collections.Iterable), "Value must be iterable"

            for item_value in value:
                item_key = create_cache_key(key.split(":")[0], key_extractor(item_value))
                item_keys.append(item_key)
                self.set_item(item_key, item_value, ttl=ttl)

            self.redis.set(key, serialize((value_metadata, item_keys)), ex=ttl)

            return value_metadata, value
        finally:
            if lock:
                self.unlock(lock)
Esempio n. 2
0
 def wrapper(*args, **kwargs):
     cache_key = create_cache_key(namespace, kwargs)
     cache_params = dict(ttl=ttl, recover=func, arguments=(args, kwargs))
     if list_:
         cache_method = self.get_list
         cache_params["key_extractor"] = key_extractor
     else:
         cache_method = self.get_item
     return cache_method(cache_key, **cache_params)