[Python-ideas] Unified style of cache management API

Brett Cannon brett at python.org
Wed Mar 27 13:09:57 EDT 2019


On Wed, Mar 27, 2019 at 6:47 AM Ma Lin <malincns at 163.com> wrote:

> 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?
>

Need? No. Nice to have? Maybe.


> 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
>

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 ;) .


>
> 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()
>

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


>
> [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-L2126
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190327/f4feee9e/attachment.html>


More information about the Python-ideas mailing list