[Python-Dev] new LRU cache API in Py3.2 (original) (raw)

Jason R. Coombs jaraco at jaraco.com
Wed Nov 17 21:58:10 CET 2010


I see now that my previous reply went only to Stefan, so I'm re-submitting, this time to the list.

-----Original Message----- From: Stefan Behnel Sent: Saturday, 04 September, 2010 04:29

What about adding an intermediate namespace called "cache", so that the new operations are available like this: print getphonenumber.cache.hits getphonenumber.cache.clear()

I agree. While the function-based implementation is highly efficient, the pure use of functions has the counter-Pythonic effect of obfuscating the internal state (the same way the 'private' keyword does in Java). A class-based implementation would be capable of having its state introspected and could easily be extended. While the functional implementation is a powerful construct, it fails to generalize well. IMHO, a stdlib implementation should err on the side of transparency and extensibility over performance.

That said, I've adapted Hettinger's Python 2.5 implementation to a class-based implementation. I've tried to keep the performance optimizations in place, but instead of instrumenting the wrapped method with lots of cache_* functions, I simply attach the cache object itself, which then provides the interface suggested by Stefan. This technique allows access to the cache object and all of its internal state, so it's also possible to do things like:

get_phone_number.cache.maxsize += 100

or

if get_phone_number.cache.store:
    do_something_interesting()

These techniques are nearly impossible in the functional implementation, as the state is buried in the locals() of the nested functions.

I'm most grateful to Raymond for contributing this to Python; On many occasions, I've used the ActiveState recipes for simple caches, but in almost every case, I've had to adapt the implementation to provide more transparency. I'd prefer to not have to do the same with the stdlib.

Regards, Jason R. Coombs

-------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: cache.py URL: <http://mail.python.org/pipermail/python-dev/attachments/20101117/dc2870f9/attachment.ksh> -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 6448 bytes Desc: not available URL: <http://mail.python.org/pipermail/python-dev/attachments/20101117/dc2870f9/attachment.bin>



More information about the Python-Dev mailing list