pallets-eco / flask-caching

A caching extension for Flask
https://flask-caching.readthedocs.io
Other
887 stars 192 forks source link

Documentation issues re: memoizing instance methods #554

Open andreyfedoseev opened 7 months ago

andreyfedoseev commented 7 months ago

I noticed two issues with documentation related to memoizing instance methods.

Consider the following case:

class MyClass:

    @cache.memoize
    def func(self, n):
        return n

According to the documentation, I should provide a class object to the delete_memoized function when a classmethod is memoized. The documentation does not mention instance methods there, so this implies that it isn't necessary in such case.

However, this does not work as expected (the cache is not invalidated):

my_instance = MyClass()
cache.delete_memoized(my_instance.func, 1)

Instead, one should do:

my_instance = MyClass()
cache.delete_memoized(my_instance.func, my_instance, 1)

This isn't documented, however.

Another issue related to that is that it is not possible to invalidate all cached calls of a method by calling delete_memoized without any arguments. It works with regular functions, but it won't work with class/instance methods since they require at least one argument when calling delete_memoized. This behavior isn't documented either.