Memoization data structure in db

What is the best data structure for caching (save / save / remember), so many functions lead to a database. Assume calc_regress function with current definition in python:

def calc_regress(ind_key, dep_key, count=30):
    independent_list = sql_select_recent_values(count, ind_key)
    dependant_list = sql_select_recent_values(count, dep_key)
    import scipy.stats as st
    return st.linregress(independent_list, dependant_list)

I see answers to What table structure should be used to store parameters of memoized functions and results in a relational database? but, it seems, it solves the problem of only one function, while I have about 500 functions.

+4
source share
2 answers

Option A

, = 500 . .

SELECT * FROM expensive_func_results WHERE func_name = 'calc_regress' AND arg1 = ind_key AND arg2 = dep_key and arg3 = count ..

, . , , /; .

B

/ func_name, arguments, result, "arguments" kwargs , . kwargs dict, , - > /, . . , SELECT * FROM expensive_func_results WHERE func_name = 'calc_regress' AND arguments = 'str(kwargs_dict)', str(kwargs_dict) - , . inspect.getargspec, ( inspect.getcallargs), .

, LIKE.

C

: func_calls func_name, args_combo_id, arg_name_idx, arg_value. , args. func_results func_name, args_combo_id, result. func_name func_id.

args , . , , , . 3 , 10. arg_name_idx - , kwargs + args. , calc_regress(ind_key=1, dep_key=2, count=30) calc_regress(1, 2, 30) ( calc_regress(1, 2) count < - , ); args_combo_id , , , . , .


[] PS: , func_name , . ; deco.__name__ = func.__name__ ..

PPS: , , , __str__ - / arg.

arg db, __str__ __repr__ , __repr__ ( ):

Python, ( ).

+1

, id ( ) , .

, calc_regress(1, 5, 30) 139694472779248_1_5_30, id(calc_regress). :

>>> def produce_cache_key(fun, *args, **kwargs):
...     args_key = '_'.join(str(a) for a in args)
...     kwargs_key = '_'.join('%s%s' % (k, v) for k, v in kwargs.items())
...     return '%s_%s_%s' % (id(fun), args_key, kwargs_key)

:

>>> def cache_result(cache):
...     def decorator(fun):
...         def wrapper(*args, **kwargs):
...             key = produce_cache_key(fun, *args, **kwargs)
...             if key not in cache:
...                 cache[key] = fun(*args, **kwargs)
...             return cache[key]
...         return wrapper
...     return decorator
... 
>>> 
>>> @cache_result(cache_dict)
... def fx(x, y, z=0):
...     print 'Doing some expensive job...'
... 
>>> cache = {}
>>> fx(1, 2, z=1)
Doing some expensive job...
>>> fx(1, 2, z=1)
>>> 
+1

Source: https://habr.com/ru/post/1613944/


All Articles