1 year ago

#77608

test-img

Anum Sheraz

Python @lru_cache() how to store key with different value

I understand that following caches an item in dict with key as 1 and value as "this is item 1 value"

@lru_cache()
def get_item(item_id):
    return {item_id: f"this is item {item_id} value"}

item = Item(id=1, name="foo")
get_item(item.id)

What if I want to store value as entire item object ?

So that later in the code, I can fetch the item object from cache by its id.

python-3.x

caching

lru

0 Answers

Your Answer

Accepted video resources