I need to write a sql database column getter, that takes in a column name and time, and returns the entire column of values for that column corresponding to the input time. This may be a frequent function call with the same arguments, so I would like to use an lru cache. However, I'm not sure if the frequency of the column names is uniformly distributed, so ideally, I would have a separate lru cache for each column name.
I previously had it like below, but I would like to separate the lru for each col_name.
@lru_cache(...)
def get_col(self, col_name, time)
# do stuff to get the column and return it
How can I achieve this? Also, unfortunately, I have to support py2.
Since
functools#lru_cache()was introduced only with Python 3.x, you will need to manage separate cache dictionaries for each column name, meaning:get_colfunction, check if the column name is in the dictionary. If it is, use the associated LRU cache to get the data. If it is not, create a new LRU cache for that column name.Something like this
LRUCacheclass:Output: (demo
tio.run)