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
Expand Up @@ -19,3 +19,4 @@ bookenv/
jyenv/ jyenv/
pypyenv/ pypyenv/
env*/ env*/
tests/test.db
58 changes: 32 additions & 26 deletions beaker/cache.py
@@ -1,7 +1,7 @@
"""This package contains the "front end" classes and functions """This package contains the "front end" classes and functions
for Beaker caching. 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`, as well as the function decorators :func:`.region_decorate`,
:func:`.region_invalidate`. :func:`.region_invalidate`.
Expand All @@ -23,7 +23,7 @@
cache_regions = {} cache_regions = {}
"""Dictionary of 'region' arguments. """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 configuration arguments. An application may have multiple
"regions" - one which stores things in a memory cache, one "regions" - one which stores things in a memory cache, one
which writes data to files, etc. which writes data to files, etc.
Expand All @@ -48,6 +48,7 @@


cache_managers = {} cache_managers = {}



class _backends(object): class _backends(object):
initialized = False initialized = False


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


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



def region_invalidate(namespace, region, *args): def region_invalidate(namespace, region, *args):
"""Invalidate a cache region corresponding to a function """Invalidate a cache region corresponding to a function
decorated with :func:`.cache_region`. decorated with :func:`.cache_region`.
Expand All @@ -209,7 +211,7 @@ def region_invalidate(namespace, region, *args):
Example:: Example::
from beaker.cache import cache_regions, cache_region, region_invalidate from beaker.cache import cache_regions, cache_region, region_invalidate
# configure regions # configure regions
cache_regions.update({ cache_regions.update({
'short_term':{ 'short_term':{
Expand All @@ -226,11 +228,11 @@ def load(search_term, limit, offset):
def invalidate_search(search_term, limit, offset): def invalidate_search(search_term, limit, offset):
'''Invalidate the cached storage for a given 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) 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`` 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 or ``self`` is not included in the cache key. This means you don't send
it to :func:`.region_invalidate`:: it to :func:`.region_invalidate`::
class MyThing(object): class MyThing(object):
@cache_region('short_term', 'some_data') @cache_region('short_term', 'some_data')
def load(self, search_term, limit, offset): 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): def invalidate_search(self, search_term, limit, offset):
'''Invalidate the cached storage for a given 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) region_invalidate(self.load, 'short_term', 'some_data', search_term, limit, offset)
""" """
if callable(namespace): if callable(namespace):
if not region: if not region:
Expand Down Expand Up @@ -331,7 +333,7 @@ def _legacy_get_value(self, key, type, **kw):
kwargs = self.nsargs.copy() kwargs = self.nsargs.copy()
kwargs.update(kw) kwargs.update(kw)
c = Cache(self.namespace.namespace, type=type, **kwargs) 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) starttime=starttime)


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



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


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

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

def go(): def go():
return func(*args) return func(*args)


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



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


Expand Down

0 comments on commit abf9ebf

Please sign in to comment.