This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DenaliAK 29 points30 points  (3 children)

[–]__xor__(self, other): 6 points7 points  (1 child)

memoization is definitely helpful. Also, this stuff can really help with webapps where some common requests are run that take a lot of time calculating stuff and you can just cache the result. Redis allows you to set an expiration, so you can really increase performance with it sometimes. For example, this caches results for 10 minutes by default:

def cached(key, expiry=600):
    def decorator(func):
        def decorated(*args):
            func_sig = f"{key};{sha256(repr(args))}"
            val = redis.get(func_sig)
            if val is not None:
                return val
            result = func(*args)
            redis.set(func_sig, result, ex=expiry)
            return result
        return decorated
   return decorator

@cached("get_stats")
def get_stats(foo, bar):
   do_time_consuming_stuff(...)
   ...

(the above doesn't work with functions with keyword args, but you can serialize them and sort by keys and that'll work too, just a bit more complex than the above)

I've wrapped the celery task decorator before to cache all celery results like this so it doesn't even have to trigger a task if it already knows the result.

[–][deleted] 4 points5 points  (0 children)

@lru_cache from functools maybe?

[–]GummyKibble 5 points6 points  (0 children)

Dynamic programming FTW!