What is the best way of caching functions?

Hi all testerossa!

Straight to the point: there is, for example, N number of functions:

def main_function():
 # Request and receive data

def function1():
 data = main_function()

def function2():
 data = main_function()

def function3():
 data = main_function()


Functions are called every 10 seconds, so the speed should be optimized as quickly as possible. Function main_function()making the request is a precious 3 seconds and I don't want every time to re-trigger it.

What is the most optimal way to store such data?

1) writing to a file and reading it - just not a quick procedure.
2) Connect some Redis for this - it will be too.
3) Use a class instead of functions is also inconvenient because between the functions is the definition of the variables.
July 9th 19 at 10:11
2 answers
July 9th 19 at 10:13
Solution
July 9th 19 at 10:15
If anyone is interested, here's a class decorator to cache responses functions for some time. May rewrite later to specify the maximum lifetime of the response via a decorator:

https://gist.github.com/gitex/2e0a078bf382aed17200...

Find more questions by tags Python