Unified style of cache management API

re module [1] and struct module [2] have module-level cache for compiled stuffs. Other third-party modules may also need cache for something. Do we need an unified cache management API like this? I suppose it's not mandatory, but welcome each module to use this API. module.cache_get_capacity() # return current capacity module.cache_set_capacity(100) # set capacity module.cache_clear() # clear cache Moreover, add these API to sys module, then the users can manage system wide cache easily: sys.cache_register(f) # register a .cache_clear() function sys.cache_clear() # call all registered .cache_clear() [1] re module policy: FIFO capacity: 512 (default), changeable implementation: Python https://github.com/python/cpython/blob/v3.8.0a3/Lib/re.py#L268-L295 [2] struct module clear entire cache when full capacity: 100, unchangeable implementation: C https://github.com/python/cpython/blob/v3.8.0a3/Modules/_struct.c#L2071-L212...

On Wed, Mar 27, 2019 at 6:47 AM Ma Lin <malincns@163.com> wrote:
Need? No. Nice to have? Maybe.
The only thing might be a cache-clearing function and I would name that module._clear_cache() or something like importlib.invalidate_cache() (which isn't a module-level cache, but it's still a cache ;) .
That's not necessary if you standardize on the name as all you're asking to do is: for module in sys.modules.values(): if hasattr(module, '_clear_cache'): module._clear_cache() -Brett

27.03.19 15:46, Ma Lin пише:
I proposed similar idea at 2015. https://mail.python.org/pipermail/python-ideas/2015-April/032836.html

On Wed, Mar 27, 2019 at 10:34 AM Serhiy Storchaka <storchaka@gmail.com> wrote:
So I would say that a cache-clearing function convention would be a reasonable starting point. If that turns out to not be enough for folks we can talk about expanding it, but I think we should start small and grow from there as needed. So what name would people want. clear_cache() or _clear_cache()? I personally prefer the latter since clearing the cache shouldn't be something people should typically need to do and thus the function is an implementation detail.

29.03.19 20:52, Brett Cannon пише:
Then I understood you correctly. Only now I saw that your words could have been understood differently. Opened https://bugs.python.org/issue36485 for implementing this idea.

On Wed, Mar 27, 2019 at 6:47 AM Ma Lin <malincns@163.com> wrote:
Need? No. Nice to have? Maybe.
The only thing might be a cache-clearing function and I would name that module._clear_cache() or something like importlib.invalidate_cache() (which isn't a module-level cache, but it's still a cache ;) .
That's not necessary if you standardize on the name as all you're asking to do is: for module in sys.modules.values(): if hasattr(module, '_clear_cache'): module._clear_cache() -Brett

27.03.19 15:46, Ma Lin пише:
I proposed similar idea at 2015. https://mail.python.org/pipermail/python-ideas/2015-April/032836.html

On Wed, Mar 27, 2019 at 10:34 AM Serhiy Storchaka <storchaka@gmail.com> wrote:
So I would say that a cache-clearing function convention would be a reasonable starting point. If that turns out to not be enough for folks we can talk about expanding it, but I think we should start small and grow from there as needed. So what name would people want. clear_cache() or _clear_cache()? I personally prefer the latter since clearing the cache shouldn't be something people should typically need to do and thus the function is an implementation detail.

29.03.19 20:52, Brett Cannon пише:
Then I understood you correctly. Only now I saw that your words could have been understood differently. Opened https://bugs.python.org/issue36485 for implementing this idea.
participants (3)
-
Brett Cannon
-
Ma Lin
-
Serhiy Storchaka