Skip to main content

A Session and Caching library with WSGI Middleware

Project description

About

Beaker is a web session and general caching library that includes WSGI middleware for use in web applications.

As a general caching library, Beaker can handle storing for various times any Python object that can be pickled with optional back-ends on a fine-grained basis.

Beaker was built largely on the code from MyghtyUtils, then refactored and extended with database support.

Beaker includes Cache and Session WSGI middleware to ease integration with WSGI capable frameworks, and is automatically used by Pylons.

Features

  • Fast, robust performance

  • Multiple reader/single writer lock system to avoid duplicate simultaneous cache creation

  • Cache back-ends include dbm, file, memory, memcached, and database (Using SQLAlchemy for multiple-db vendor support)

  • Signed cookie’s to prevent session hijacking/spoofing

  • Cookie-only sessions to remove the need for a db or file backend (ideal for clustered systems)

  • Extensible Container object to support new back-ends

  • Cache’s can be divided into namespaces (to represent templates, objects, etc.) then keyed for different copies

  • Create functions for automatic call-backs to create new cache copies after expiration

  • Fine-grained toggling of back-ends, keys, and expiration per Cache object

Documentation

Documentation can be found on the Official Beaker Docs site.

Examples

Caching

Basic Example:

from beaker.cache import CacheManager
cm = CacheManager(type='dbm', data_dir='./cache')

cache = cm.get_cache('mytemplate')

def somethingslow():
    # slow stuff
    db_lookups()

# Get the value, this will create the cache copy the first time
# and any time it expires (in seconds, so 3600 = one hour)
result = mycache.get_value(day, createfunc=somethingslow, expiretime=3600)

Using WSGI:

from beaker.middleware import CacheMiddleware

def simple_app(environ, start_response):
    cache = environ['beaker.cache'].get_cache('testcache')
    try:
        value = cache.get_value('value')
    except KeyError:
        value = 0
    cache.set_value('value', value+1)
    start_response('200 OK', [('Content-type', 'text/plain')])
    return ['The current value is: %s' % cache.get_value('value')]

app = CacheMiddleware(simple_app, type='dbm', data_dir='./cache')

Sessions

Using WSGI:

from beaker.middleware import SessionMiddleware

def simple_app(environ, start_response):
    session = environ['beaker.session']
    if not session.has_key('value'):
        session['value'] = 0
    session['value'] += 1
    session.save()
    start_response('200 OK', [('Content-type', 'text/plain')])
    return ['The current value is: %d' % session['value']]

wsgi_app = SessionMiddleware(simple_app, type='dbm', data_dir='./cache')

Source

The latest developer version is available in a Mercurial repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Beaker-1.1.2.tar.gz (36.9 kB view hashes)

Uploaded Source

Built Distribution

Beaker-1.1.2-py2.5.egg (74.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page