Skip to content

Commit

Permalink
Merge pull request #17 from wgen/master
Browse files Browse the repository at this point in the history
A few pep-8 code cleanup fixes
  • Loading branch information
bbangert committed May 10, 2012
2 parents bdd66a4 + af7456b commit abf9ebf
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ bookenv/
jyenv/
pypyenv/
env*/
tests/test.db
58 changes: 32 additions & 26 deletions beaker/cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This package contains the "front end" classes and functions
for Beaker caching.
Included are the :class:`.Cache` and :class:`.CacheManager` classes,
Included are the :class:`.Cache` and :class:`.CacheManager` classes,
as well as the function decorators :func:`.region_decorate`,
:func:`.region_invalidate`.
Expand All @@ -23,7 +23,7 @@
cache_regions = {}
"""Dictionary of 'region' arguments.
A "region" is a string name that refers to a series of cache
A "region" is a string name that refers to a series of cache
configuration arguments. An application may have multiple
"regions" - one which stores things in a memory cache, one
which writes data to files, etc.
Expand All @@ -48,6 +48,7 @@

cache_managers = {}


class _backends(object):
initialized = False

Expand Down Expand Up @@ -100,19 +101,19 @@ def _init(self):
warnings.warn(
"Unable to load NamespaceManager "
"entry point: '%s': %s" % (
entry_point,
tb.getvalue()),
entry_point,
tb.getvalue()),
RuntimeWarning, 2)
except ImportError:
pass

# Initialize the basic available backends
clsmap = _backends({
'memory':container.MemoryNamespaceManager,
'dbm':container.DBMNamespaceManager,
'file':container.FileNamespaceManager,
'ext:memcached':memcached.MemcachedNamespaceManager,
'ext:database':database.DatabaseNamespaceManager,
'memory': container.MemoryNamespaceManager,
'dbm': container.DBMNamespaceManager,
'file': container.FileNamespaceManager,
'ext:memcached': memcached.MemcachedNamespaceManager,
'ext:database': database.DatabaseNamespaceManager,
'ext:sqla': sqla.SqlaNamespaceManager,
'ext:google': google.GoogleNamespaceManager,
})
Expand All @@ -125,7 +126,7 @@ def cache_region(region, *args):
Example::
from beaker.cache import cache_regions, cache_region
# configure regions
cache_regions.update({
'short_term':{
Expand All @@ -138,38 +139,38 @@ def cache_region(region, *args):
def load(search_term, limit, offset):
'''Load from a database given a search term, limit, offset.'''
return database.query(search_term)[offset:offset + limit]
The decorator can also be used with object methods. The ``self``
argument is not part of the cache key. This is based on the
actual string name ``self`` being in the first argument
argument is not part of the cache key. This is based on the
actual string name ``self`` being in the first argument
position (new in 1.6)::
class MyThing(object):
@cache_region('short_term', 'load_things')
def load(self, search_term, limit, offset):
'''Load from a database given a search term, limit, offset.'''
return database.query(search_term)[offset:offset + limit]
Classmethods work as well - use ``cls`` as the name of the class argument,
and place the decorator around the function underneath ``@classmethod``
(new in 1.6)::
class MyThing(object):
@classmethod
@cache_region('short_term', 'load_things')
def load(cls, search_term, limit, offset):
'''Load from a database given a search term, limit, offset.'''
return database.query(search_term)[offset:offset + limit]
:param region: String name of the region corresponding to the desired
caching arguments, established in :attr:`.cache_regions`.
:param \*args: Optional ``str()``-compatible arguments which will uniquely
identify the key used by this decorated function, in addition
to the positional arguments passed to the function itself at call time.
This is recommended as it is needed to distinguish between any two functions
or methods that have the same name (regardless of parent class or not).
.. note::
The function being decorated must only be called with
Expand All @@ -180,14 +181,15 @@ def load(cls, search_term, limit, offset):
forms the unique cache key.
.. note::
When a method on a class is decorated, the ``self`` or ``cls``
argument in the first position is
not included in the "key" used for caching. New in 1.6.
"""
return _cache_decorate(args, None, None, region)


def region_invalidate(namespace, region, *args):
"""Invalidate a cache region corresponding to a function
decorated with :func:`.cache_region`.
Expand All @@ -209,7 +211,7 @@ def region_invalidate(namespace, region, *args):
Example::
from beaker.cache import cache_regions, cache_region, region_invalidate
# configure regions
cache_regions.update({
'short_term':{
Expand All @@ -226,11 +228,11 @@ def load(search_term, limit, offset):
def invalidate_search(search_term, limit, offset):
'''Invalidate the cached storage for a given search term, limit, offset.'''
region_invalidate(load, 'short_term', 'load_data', search_term, limit, offset)
Note that when a method on a class is decorated, the first argument ``cls``
or ``self`` is not included in the cache key. This means you don't send
it to :func:`.region_invalidate`::
class MyThing(object):
@cache_region('short_term', 'some_data')
def load(self, search_term, limit, offset):
Expand All @@ -240,7 +242,7 @@ def load(self, search_term, limit, offset):
def invalidate_search(self, search_term, limit, offset):
'''Invalidate the cached storage for a given search term, limit, offset.'''
region_invalidate(self.load, 'short_term', 'some_data', search_term, limit, offset)
"""
if callable(namespace):
if not region:
Expand Down Expand Up @@ -331,7 +333,7 @@ def _legacy_get_value(self, key, type, **kw):
kwargs = self.nsargs.copy()
kwargs.update(kw)
c = Cache(self.namespace.namespace, type=type, **kwargs)
return c._get_value(key, expiretime=expiretime, createfunc=createfunc,
return c._get_value(key, expiretime=expiretime, createfunc=createfunc,
starttime=starttime)

def clear(self):
Expand Down Expand Up @@ -474,7 +476,7 @@ def load(search_term, limit, offset):
.. note::
The function being decorated must only be called with
positional arguments.
positional arguments.
"""
return _cache_decorate(args, self, kwargs, None)
Expand Down Expand Up @@ -521,6 +523,7 @@ def load(search_term, limit, offset):
key_length = kwargs.pop('key_length', 250)
_cache_decorator_invalidate(cache, key_length, args)


def _cache_decorate(deco_args, manager, kwargs, region):
"""Return a caching function decorator."""

Expand All @@ -529,6 +532,7 @@ def _cache_decorate(deco_args, manager, kwargs, region):
def decorate(func):
namespace = util.func_namespace(func)
skip_self = util.has_self_arg(func)

def cached(*args):
if not cache[0]:
if region is not None:
Expand Down Expand Up @@ -561,6 +565,7 @@ def cached(*args):
key_length = kwargs.pop('key_length', 250)
if len(cache_key) + len(namespace) > key_length:
cache_key = sha1(cache_key).hexdigest()

def go():
return func(*args)

Expand All @@ -571,6 +576,7 @@ def go():
return cached
return decorate


def _cache_decorator_invalidate(cache, key_length, args):
"""Invalidate a cache key based on function arguments."""

Expand Down
Loading

0 comments on commit abf9ebf

Please sign in to comment.