0

I am using Flask- Restful for a Python API, which is working well. Now, there are few DB operations which I want to cache, how do I go about that? I have searched online and there were a couple of options like flask cache and CacheTools and I am not able to decide.

Flask cache was mainly about caching the requests rather than the data used inside, correct me if I am wrong.

Cachetools has useful methods like lru_cache etc. which could be of use to me?

PS: I am primarily a Java guy and used to use guava with spring boot in my previous services, so looking for something like that in python.

Tejas Thakar
  • 585
  • 5
  • 19
xmen
  • 9
  • 1
  • 6
  • 1
    Flask Cache caches responses to requests. lru_cache caches returnvalues of functions, CacheTools offers you different cache strategies and what you do with them is up to you. I've no idea what spring boot does, but it depends on your needs where and what you want to cache and there is no single solution. – syntonym Jul 07 '17 at 11:15
  • hi, I basically want to cache data rather than requests, because my requests are almost all the time going to be unique. – xmen Jul 07 '17 at 11:18

1 Answers1

0

Earlier, I had this problem, too. In the end, I use Redis.

And in werkeug, there is a cache lib, which makes Redis easy to use.

from werkzeug.contrib.cache import RedisCache

for more information, see the doc

By the way, if your app runs in single process(multithread is also OK), you can just use the codes below.

class CachedItem:
    def __init__(self, item, duration):
        self.item = item
        self.duration = duration
        self.time_stamp = time.time()

    def __repr__(self):
        return '<CachedItem {%s} expires at: %s>' % (self.item, time.time() + self.duration)


class CachedDict(dict):
    def __init__(self, *args, **kwargs):
        super(CachedDict, self).__init__(*args, **kwargs)
        self.lock = threading.Lock()

    def get_cache(self, key, default=None, duration=300):
        with self.lock:
            self._clean()
            if key in self:
                return self[key].item
            else:
                self[key] = CachedItem(default, duration)
            return self[key].item

    def set_cache(self, key, value, duration=300):
        with self.lock:
            self[key] = CachedItem(value, duration)

    def _clean(self):
        for key in list(self.keys()): # [self.keys()] error, we get dict_keys type
            if self[key].time_stamp + self[key].duration <= time.time():
                self.pop(key)
xinnjie
  • 672
  • 5
  • 12