Uncategorized

I use the (excellent) Python library dogpile.cache quite extensively. One web project can make hundreds of calls to the cache per page, so it’s often one of the first places I look to maximize performance.

I have one bit of code that uses the key_mangler functionality manipulate the keys used for a Redis data store.

An overly simplified example of using this is generating the key_mangler to “prefix” keys, and looks something like this:

The result was this had quite the speedup – twice the speed of the original version. But… there was a huge caveat – all my unit tests broke. Digging into things, the following is the output of iterating over the same info:

I was lazy when i rewrote the function, and did not expect Python to scope tmpl like that. It makes perfect sense, I know Python behaves like this, and I’ve taken this exact implementation detail into account before… I just didn’t expect it at the time.

Since I knew how/why this happened, the fix was simple — hinting the def with scope I want…