J
J
JRazor2016-05-31 19:53:53
Python
JRazor, 2016-05-31 19:53:53

What is the best way to cache a function?

Hello to all toasters!
Straight to the point: there are, for example, N number of functions:

def main_function():
    # Запрос и получение данных

def function1():
    data = main_function()
    
def function2():
    data = main_function()
    
def function3():
    data = main_function()

The functions are called every 10 seconds, so the speed should be optimized as quickly as possible. The main_function() function that makes the request takes a precious 3 seconds and I don't want to call it again every time.
What is the best way to store such data?
1) Writing to a file and reading it is also a slow procedure.
2) Connecting some kind of Redis for the sake of this will be overkill.
3) Using a class instead of functions is also inconvenient, because between the functions there is a definition of variables.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AtomKrieg, 2016-06-01
@JRazor

stackoverflow.com/questions/815110/is-there-a-deco...

J
JRazor, 2016-06-01
@JRazor

If anyone is interested, here is a decorator class for caching function responses for some time. Perhaps I will rewrite later to set the maximum response time through the decorator:
https://gist.github.com/gitex/2e0a078bf382aed17200...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question