-
-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] expose the cache, key and lock arguments of @cachetools.cached as attributes on the decorated function #176
Comments
IMHO, if you're trying to do something more complex so you really need access to these, you should properly not use the decorators at all, but better use the cache classes directly. The decorators are there for the most simple use cases, and the fact that you can do tricky stuff with them doesn't necessarily mean you always should. So the awkwardness of manipulating caches outside of the decorator is somewhat intended. |
Maybe I'll change my mind about this, due to the apparent popularity of the decorators... |
This feature is more just for convenience, so I understand if its far down in the list of desirable features. But I think it would allow users to write cleaner code when they need to interact with the underlying cache object if we expose
cache
,key
andlock
arguments of@cachetools.cached
as attributes on the decorated function.The documentation currently recommends storing the cache in a variable if you need to access it directly. Simplified example from docs:
While that's really easy to do, it forms an implicit relationship between the function and and the cache object which isn't ideal in the long run. If you have multiple functions that are cached, you'll probably prepend the cache variable's name with the function name like so:
But wait! To properly access the cache, you also need access to the
key
andlock
arguments that the cache was created with! So,If instead, we exposed these arguments as attributes (like we already do with
__wrapped__
) the code becomes much simpler, cleaner and the implicit "prepended" relations become explicit attribute accesses. Like so,This also has the added advantage that users don't need worry about whether the default key was used during cache creation (like in #173) and they can always rely on
fib.cache[fib.key(2)]
working.To summarize the advantages:
The text was updated successfully, but these errors were encountered: