beaker / beaker / cache.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
"""This package contains the "front end" classes and functions
for Beaker caching.

Included are the :class:`.Cache` and :class:`.CacheManager` classes, 
as well as the function decorators :func:`.region_decorate`,
:func:`.region_invalidate`.

"""

import warnings

import beaker.container as container
import beaker.util as util
from beaker.exceptions import BeakerException, InvalidCacheBackendError
from beaker.synchronization import _threading

import beaker.ext.memcached as memcached
import beaker.ext.database as database
import beaker.ext.sqla as sqla
import beaker.ext.google as google

# Initialize the cache region dict
cache_regions = {}
"""Dictionary of 'region' arguments.

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.

The dictionary stores string key names mapped to dictionaries
of configuration arguments.  Example::

    from beaker.cache import cache_regions
    cache_regions.update({
        'short_term':{
            'expire':'60',
            'type':'memory'
        },
        'long_term':{
            'expire':'1800',
            'type':'dbm',
            'data_dir':'/tmp',
        }
    })
"""


cache_managers = {}

class _backends(object):
    initialized = False

    def __init__(self, clsmap):
        self._clsmap = clsmap
        self._mutex = _threading.Lock()

    def __getitem__(self, key):
        try:
            return self._clsmap[key]
        except KeyError, e:
            if not self.initialized:
                self._mutex.acquire()
                try:
                    if not self.initialized:
                        self._init()
                        self.initialized = True

                    return self._clsmap[key]
                finally:
                    self._mutex.release()

            raise e

    def _init(self):
        try:
            import pkg_resources

            # Load up the additional entry point defined backends
            for entry_point in pkg_resources.iter_entry_points('beaker.backends'):
                try:
                    namespace_manager = entry_point.load()
                    name = entry_point.name
                    if name in self._clsmap:
                        raise BeakerException("NamespaceManager name conflict,'%s' "
                                              "already loaded" % name)
                    self._clsmap[name] = namespace_manager
                except (InvalidCacheBackendError, SyntaxError):
                    # Ignore invalid backends
                    pass
                except:
                    import sys
                    from pkg_resources import DistributionNotFound
                    # Warn when there's a problem loading a NamespaceManager
                    if not isinstance(sys.exc_info()[1], DistributionNotFound):
                        import traceback
                        from StringIO import StringIO
                        tb = StringIO()
                        traceback.print_exc(file=tb)
                        warnings.warn(
                            "Unable to load NamespaceManager "
                            "entry point: '%s': %s" % (
                                        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,
          'ext:sqla': sqla.SqlaNamespaceManager,
          'ext:google': google.GoogleNamespaceManager,
          })


def cache_region(region, *args):
    """Decorate a function such that its return result is cached,
    using a "region" to indicate the cache arguments.

    Example::

        from beaker.cache import cache_regions, cache_region
        
        # configure regions
        cache_regions.update({
            'short_term':{
                'expire':'60',
                'type':'memory'
            }
        })

        @cache_region('short_term', 'load_things')
        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 
    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
        positional arguments, and the arguments must support
        being stringified with ``str()``.  The concatenation
        of the ``str()`` version of each argument, combined
        with that of the ``*args`` sent to the decorator,
        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`.

    :param namespace: The namespace of the cache to invalidate.  This is typically
      a reference to the original function (as returned by the :func:`.cache_region`
      decorator), where the :func:`.cache_region` decorator applies a "memo" to
      the function in order to locate the string name of the namespace.

    :param region: String name of the region used with the decorator.  This can be
     ``None`` in the usual case that the decorated function itself is passed,
     not the string name of the namespace.

    :param args: Stringifyable arguments that are used to locate the correct
     key.  This consists of the ``*args`` sent to the :func:`.cache_region`
     decorator itself, plus the ``*args`` sent to the function itself
     at runtime.

    Example::

        from beaker.cache import cache_regions, cache_region, region_invalidate
        
        # configure regions
        cache_regions.update({
            'short_term':{
                'expire':'60',
                'type':'memory'
            }
        })

        @cache_region('short_term', 'load_data')
        def load(search_term, limit, offset):
            '''Load from a database given a search term, limit, offset.'''
            return database.query(search_term)[offset:offset + limit]

        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):
                '''Load from a database given a search term, limit, offset.'''
                return database.query(search_term)[offset:offset + limit]

            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:
            region = namespace._arg_region
        namespace = namespace._arg_namespace

    if not region:
        raise BeakerException("Region or callable function "
                                    "namespace is required")
    else:
        region = cache_regions[region]

    cache = Cache._get_cache(namespace, region)
    _cache_decorator_invalidate(cache, args)


class Cache(object):
    """Front-end to the containment API implementing a data cache.

    :param namespace: the namespace of this Cache

    :param type: type of cache to use

    :param expire: seconds to keep cached data

    :param expiretime: seconds to keep cached data (legacy support)

    :param starttime: time when cache was cache was

    """
    def __init__(self, namespace, type='memory', expiretime=None,
                 starttime=None, expire=None, **nsargs):
        try:
            cls = clsmap[type]
            if isinstance(cls, InvalidCacheBackendError):
                raise cls
        except KeyError:
            raise TypeError("Unknown cache implementation %r" % type)

        self.namespace = cls(namespace, **nsargs)
        self.expiretime = expiretime or expire
        self.starttime = starttime
        self.nsargs = nsargs

    @classmethod
    def _get_cache(cls, namespace, kw):
        key = namespace + str(kw)
        try:
            return cache_managers[key]
        except KeyError:
            cache_managers[key] = cache = cls(namespace, **kw)
            return cache

    def put(self, key, value, **kw):
        self._get_value(key, **kw).set_value(value)
    set_value = put

    def get(self, key, **kw):
        """Retrieve a cached value from the container"""
        return self._get_value(key, **kw).get_value()
    get_value = get

    def remove_value(self, key, **kw):
        mycontainer = self._get_value(key, **kw)
        mycontainer.clear_value()
    remove = remove_value

    def _get_value(self, key, **kw):
        if isinstance(key, unicode):
            key = key.encode('ascii', 'backslashreplace')

        if 'type' in kw:
            return self._legacy_get_value(key, **kw)

        kw.setdefault('expiretime', self.expiretime)
        kw.setdefault('starttime', self.starttime)

        return container.Value(key, self.namespace, **kw)

    @util.deprecated("Specifying a "
            "'type' and other namespace configuration with cache.get()/put()/etc. "
            "is deprecated. Specify 'type' and other namespace configuration to "
            "cache_manager.get_cache() and/or the Cache constructor instead.")
    def _legacy_get_value(self, key, type, **kw):
        expiretime = kw.pop('expiretime', self.expiretime)
        starttime = kw.pop('starttime', None)
        createfunc = kw.pop('createfunc', None)
        kwargs = self.nsargs.copy()
        kwargs.update(kw)
        c = Cache(self.namespace.namespace, type=type, **kwargs)
        return c._get_value(key, expiretime=expiretime, createfunc=createfunc, 
                            starttime=starttime)

    def clear(self):
        """Clear all the values from the namespace"""
        self.namespace.remove()

    # dict interface
    def __getitem__(self, key):
        return self.get(key)

    def __contains__(self, key):
        return self._get_value(key).has_current_value()

    def has_key(self, key):
        return key in self

    def __delitem__(self, key):
        self.remove_value(key)

    def __setitem__(self, key, value):
        self.put(key, value)


class CacheManager(object):
    def __init__(self, **kwargs):
        """Initialize a CacheManager object with a set of options

        Options should be parsed with the
        :func:`~beaker.util.parse_cache_config_options` function to
        ensure only valid options are used.

        """
        self.kwargs = kwargs
        self.regions = kwargs.pop('cache_regions', {})

        # Add these regions to the module global
        cache_regions.update(self.regions)

    def get_cache(self, name, **kwargs):
        kw = self.kwargs.copy()
        kw.update(kwargs)
        return Cache._get_cache(name, kw)

    def get_cache_region(self, name, region):
        if region not in self.regions:
            raise BeakerException('Cache region not configured: %s' % region)
        kw = self.regions[region]
        return Cache._get_cache(name, kw)

    def region(self, region, *args):
        """Decorate a function to cache itself using a cache region

        The region decorator requires arguments if there are more than
        two of the same named function, in the same module. This is
        because the namespace used for the functions cache is based on
        the functions name and the module.


        Example::

            # Assuming a cache object is available like:
            cache = CacheManager(dict_of_config_options)


            def populate_things():

                @cache.region('short_term', 'some_data')
                def load(search_term, limit, offset):
                    return load_the_data(search_term, limit, offset)

                return load('rabbits', 20, 0)

        .. note::

            The function being decorated must only be called with
            positional arguments.

        """
        return cache_region(region, *args)

    def region_invalidate(self, namespace, region, *args):
        """Invalidate a cache region namespace or decorated function

        This function only invalidates cache spaces created with the
        cache_region decorator.

        :param namespace: Either the namespace of the result to invalidate, or the
           name of the cached function

        :param region: The region the function was cached to. If the function was
            cached to a single region then this argument can be None

        :param args: Arguments that were used to differentiate the cached
            function as well as the arguments passed to the decorated
            function

        Example::

            # Assuming a cache object is available like:
            cache = CacheManager(dict_of_config_options)

            def populate_things(invalidate=False):

                @cache.region('short_term', 'some_data')
                def load(search_term, limit, offset):
                    return load_the_data(search_term, limit, offset)

                # If the results should be invalidated first
                if invalidate:
                    cache.region_invalidate(load, None, 'some_data',
                                            'rabbits', 20, 0)
                return load('rabbits', 20, 0)


        """
        return region_invalidate(namespace, region, *args)

    def cache(self, *args, **kwargs):
        """Decorate a function to cache itself with supplied parameters

        :param args: Used to make the key unique for this function, as in region()
            above.

        :param kwargs: Parameters to be passed to get_cache(), will override defaults

        Example::

            # Assuming a cache object is available like:
            cache = CacheManager(dict_of_config_options)


            def populate_things():

                @cache.cache('mycache', expire=15)
                def load(search_term, limit, offset):
                    return load_the_data(search_term, limit, offset)

                return load('rabbits', 20, 0)

        .. note::

            The function being decorated must only be called with
            positional arguments. 

        """
        return _cache_decorate(args, self, kwargs, None)

    def invalidate(self, func, *args, **kwargs):
        """Invalidate a cache decorated function

        This function only invalidates cache spaces created with the
        cache decorator.

        :param func: Decorated function to invalidate

        :param args: Used to make the key unique for this function, as in region()
            above.

        :param kwargs: Parameters that were passed for use by get_cache(), note that
            this is only required if a ``type`` was specified for the
            function

        Example::

            # Assuming a cache object is available like:
            cache = CacheManager(dict_of_config_options)


            def populate_things(invalidate=False):

                @cache.cache('mycache', type="file", expire=15)
                def load(search_term, limit, offset):
                    return load_the_data(search_term, limit, offset)

                # If the results should be invalidated first
                if invalidate:
                    cache.invalidate(load, 'mycache', 'rabbits', 20, 0, type="file")
                return load('rabbits', 20, 0)

        """
        namespace = func._arg_namespace

        cache = self.get_cache(namespace, **kwargs)
        _cache_decorator_invalidate(cache, args)

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

    cache = [None]

    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:
                    if region not in cache_regions:
                        raise BeakerException(
                            'Cache region not configured: %s' % region)
                    reg = cache_regions[region]
                    if not reg.get('enabled', True):
                        return func(*args)
                    cache[0] = Cache._get_cache(namespace, reg)
                elif manager:
                    cache[0] = manager.get_cache(namespace, **kwargs)
                else:
                    raise Exception("'manager + kwargs' or 'region' "
                                    "argument is required")

            if skip_self:
                cache_key = " ".join(map(str, deco_args + args[1:]))
            else:
                cache_key = " ".join(map(str, deco_args + args))
            def go():
                return func(*args)

            return cache[0].get_value(cache_key, createfunc=go)
        cached._arg_namespace = namespace
        if region is not None:
            cached._arg_region = region
        return cached
    return decorate

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

    cache_key = " ".join(map(str, args))
    cache.remove_value(cache_key)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.