Hi there,
I’m adding decorator support to my cache library Theine. Because Theine only support string keys, user must specify how this key is generated from decorated function arguments.
Here is my solution for this, use another function to generate key:
The key function could certainly be improved (for example, adding type(arg) to the string) to avoid false hits. Combine with a mechanism to override the key function and to apply it to a inputs, and you get all of your pros for option 1 without hitting the con unless the user has a specific-enough case to warrant it.
My consideration is if I add this default function, users will blindly think this will work and never use the key decorator way. But str or similar approaches may lead to wrong result if some one override it. If there is a perfect way to generate key from function arguments, maybe I can use that as default.
Actually I have an idea of near perfect function. Python’s lru cache use a fancy HashedSeq to build key automatically. I can create a Dict[HashedSeq, UUID], use the uuid as my string cache key, and find the uuid based on function arguments on get. Cons of this is firstly performance cost, secondly HashedSeq will inclule function arguments, may consume much memory.