This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

It’s like lru_caches but can be cleared automatically or via context managers.

Why?

There are only two hard things in Computer Science: cache invalidation and naming things.

– Phil Karlton

So, caches are fairly easy, but their invalidation is kind of hard to get right.

What type of cache is used?

The default implementation is the lru_cache of the stdlib but you can drop in your own.

How?

Python features garbage collection aka. automatic memory management. Let’s make use of it:

from xcache import cache_gen

request_cache = cache_gen(lambda: request)  # default cache_impl=lru_cache

Here, we defined our own cache that needs the global request for invalidating the cache. As soon as request is garbage-collected, the cached data is freed.

@request_cache()
def fib(n):
    return fib(n-1) + fib(n-2) if n > 1 else 1

Here, we see how to decorate a function to work like a lru_cache.

Now, let’s see how this works:

class Request(): pass

request = Request()

print(fib(10))
print(fib(20))
print(fib.cache_info())  # fib cache contains 2 items

request = Request()      # invalidates all caches

print(fib.cache_info())  # fib cache contains 0 items
print(fib(10))
print(fib(20))

Context Manager for more more control over cache invalidation

If you need more control, the context manager clean_caches is what you need:

from xcache import cached, clean_caches

@cached()
def fib(n):
    return fib(n-1) + fib(n-2) if n > 1 else 1

with clean_caches():
    print(fib(10))
    print(fib(20))
    print(fib.cache_info())  # fib cache contains 2 items
print(fib.cache_info())      # fib cache contains 0 items

You can even specify what object the caches should be attached to:

@cached()
def fib(n):
    return fib(n-1) + fib(n-2) if n > 1 else 1

with clean_caches(Request()) as request:
    print(fib(10))
    print(fib(20))
    print(fib.cache_info())  # fib cache contains 2 items
print(fib.cache_info())      # fib cache contains 0 items

Can these context managers be nested?

Sure. At each entrance and and exit of each context, all associated caches are emptied.

Conclusion

Good

  • cache invalidation done easy
  • works via garbage collection
  • works via context managers
  • works with Python2 and Python3

Bad

  • unkown ;-)

Ideas are always welcome. :-)

Release History

Release History

0.2

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
xcache-0.2-py2.py3-none-any.whl (2.6 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Mar 22, 2016
xcache-0.2.tar.gz (3.2 kB) Copy SHA256 Checksum SHA256 Source Mar 22, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting