I am trying to use lru_cache
in Python3 to speed up common queries into our Salesforce database. Below is the relevant code that is supposed to
When I try this code, the cache works for calling the functions with no arguments, but it doesn't seem to cache the function calls with arguments. Also, I am not sure of how to order the decorators for the decorated functions.
Note, I am using a class here with class and static methods so I can override the get
and get_all
methods for different subclasses of Resource
.
Please explain what I am doing wrong or could be doing better.
from functools import lru_cache
from functools import wraps
class Resource(object):
def hash_dict(func):
"""Transform mutable dictionnary
Into immutable
Useful to be compatible with cache
"""
class HDict(dict):
def __hash__(self):
return hash(frozenset(self.items()))
@wraps(func)
def wrapped(*args, **kwargs):
args = tuple([HDict(arg) if isinstance(arg, dict) else arg for arg in args])
kwargs = {}
for k, v in kwargs.items():
if isinstance(v, dict):
kwargs[k] = HDict(v)
elif isinstance(v, list):
kwargs[k] = tuple(v)
else:
kwargs[k] = v
return func(*args, **kwargs)
return wrapped
@staticmethod
@hash_dict
@lru_cache
def get(cls, resource_id, lang='en', fields=None):
pass
@classmethod
@hash_dict
@lru_cache
def get_all(cls, lang='en', filters=None, fields=None):
pass
Python's functools module comes with the @lru_cache decorator, which gives you the ability to cache the result of your functions using the Least Recently Used (LRU) strategy. This is a simple yet powerful technique that you can use to leverage the power of caching in your code.
lru_cache decorator. lru_cache isn't ... It looks like a fantastic library that provides great functionality. But note that those classes are not thread-safe - you have to manually ......
To make a method a static method, add @staticmethod decorator before the method definition. The @staticmethod decorator is a built-in function decorator in Python to declare a method as a static method.
Clearing LRU Cache After the use of the cache, cache_clear() can be used for clearing or invalidating the cache.
No need for an additional package. The following work as expected:
import functools
class A:
@staticmethod
@functools.lru_cache(maxsize=None)
def build(value):
print('Creating', value)
return A()
assert A.build('a') is A.build('a')
assert A.build('b') is A.build('b')
assert A.build('b') is not A.build('a')
Starting python 3.9, you can replace @functools.lru_cache(maxsize=None)
by @functools.cache
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With