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

Introduction

The horae.cache package provides simple decorators for caching. There are decorators available for caching vocabularies (contextual or global) and for caching method and function output on the request.

Usage

Vocabulary

There are three different ways to cache a vocabulary:

Global cache
Caches the vocabulary globally
Contextual cache
Caches the vocabulary in the given context
Contextual parent cache
Caches the vocabulary in the defined parent of the given context

Every type of cache has a corresponding invalidation function to clear the cache for the given vocabulary. This is especially usable when using dynamic vocabularies which change over time for example a vocabulary of content objects of a given type:

import grok

from zope.schema import vocabulary
from some.module.interfaces import ISampleContent
from horae.cache import vocabulary

@vocabulary.cache_global
def all_sample_contents(context):
    # find all content objects of type ISampleContent
    return vocab
vocabulary.getVocabularyRegistry().register(
    'allsamplecontents',
    all_sample_contents)

@grok.subscribe(ISampleContent, grok.IObjectModifiedEvent)
@grok.subscribe(ISampleContent, grok.IObjectMovedEvent)
def invalidate_all_sample_contents_cache(obj, event):
    vocabulary.invalidate_global(all_sample_contents)

The contextual cache is used for context specific vocabularies and are used the same way as the global cache:

@vocabulary.cache_contextual
def sample_contents_in_context(context):
    # find all content objects of type ISampleContent
    # in the given context
    return vocab
vocabulary.getVocabularyRegistry().register(
    'samplecontentsincontext',
    sample_contents_in_context)

@grok.subscribe(ISampleContent, grok.IObjectModifiedEvent)
@grok.subscribe(ISampleContent, grok.IObjectMovedEvent)
def invalidate_sample_contents_in_context_cache(obj, event):
    # iterate over all the parents of the object and call:
    vocabulary.invalidate_contextual(parent,
                                     sample_contents_in_context)

The contextual parent cache is used slightly different. It takes an optional interface as a parameter to find the parent to cache the vocabulary on. The cache steps up the object hierarchy until it finds a parent implementing the given interface. If no interface is given the immediate parent of the vocabulary context is taken as the cache context. An example usage may look like this:

from some.module.interfaces import ISampleContainer

@vocabulary.cache_contextual_parent(ISampleContainer)
def sample_contents_in_parent_of_context(context):
    # find all content objects of type ISampleContent in
    # the first found parent implementing ISampleContainer
    # of the given context
    return vocab
vocabulary.getVocabularyRegistry().register(
    'samplecontentsinparentofcontext',
    sample_contents_in_parent_of_context)

@grok.subscribe(ISampleContent, grok.IObjectModifiedEvent)
@grok.subscribe(ISampleContent, grok.IObjectMovedEvent)
def invalidate_sample_contents_in_context_cache(obj, event):
    vocabulary.invalidate_contextual_parent(
        obj,
        ISampleContainer,
        sample_contents_in_parent_of_context)

Request

To cache the output of a function for the current request simply add the horae.cache.request.cache decorator:

from horae.cache import request

@request.cache()
def some_heavy_computation(arg1, arg2, kwarg1=True, kwarg2=True):
    # do the heavy computation
    return result_of_the_heavy_computation

The horae.cache.request.cache takes two optional arguments args and kwargs using which one may define what arguments and/or keyword arguments are relevant for the output. If in the example above the output would be the same if arg2 and kwarg2 was set differently the decorator could look like this to improve the caching:

@request.cache(args=(0,), kwargs=('kwarg1',))
def some_heavy_computation(arg1, arg2, kwarg1=True, kwarg2=True):
    # do the heavy computation
    return result_of_the_heavy_computation

Dependencies

Changelog

1.0a1 (2012-01-16)

  • Initial release
Release History

Release History

1.0a1

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

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
horae.cache-1.0a1.tar.gz (5.0 kB) Copy SHA256 Checksum SHA256 Source Jan 16, 2012

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS 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