OLD | NEW |
(Empty) | |
| 1 """ |
| 2 Proxy Backends |
| 3 ------------------ |
| 4 |
| 5 Provides a utility and a decorator class that allow for modifying the behavior |
| 6 of different backends without altering the class itself or having to extend the |
| 7 base backend. |
| 8 |
| 9 .. versionadded:: 0.5.0 Added support for the :class:`.ProxyBackend` class. |
| 10 |
| 11 """ |
| 12 |
| 13 from .api import CacheBackend |
| 14 |
| 15 |
| 16 class ProxyBackend(CacheBackend): |
| 17 """A decorator class for altering the functionality of backends. |
| 18 |
| 19 Basic usage:: |
| 20 |
| 21 from dogpile.cache import make_region |
| 22 from dogpile.cache.proxy import ProxyBackend |
| 23 |
| 24 class MyFirstProxy(ProxyBackend): |
| 25 def get(self, key): |
| 26 # ... custom code goes here ... |
| 27 return self.proxied.get(key) |
| 28 |
| 29 def set(self, key, value): |
| 30 # ... custom code goes here ... |
| 31 self.proxied.set(key) |
| 32 |
| 33 class MySecondProxy(ProxyBackend): |
| 34 def get(self, key): |
| 35 # ... custom code goes here ... |
| 36 return self.proxied.get(key) |
| 37 |
| 38 |
| 39 region = make_region().configure( |
| 40 'dogpile.cache.dbm', |
| 41 expiration_time = 3600, |
| 42 arguments = { |
| 43 "filename":"/path/to/cachefile.dbm" |
| 44 }, |
| 45 wrap = [ MyFirstProxy, MySecondProxy ] |
| 46 ) |
| 47 |
| 48 Classes that extend :class:`.ProxyBackend` can be stacked |
| 49 together. The ``.proxied`` property will always |
| 50 point to either the concrete backend instance or |
| 51 the next proxy in the chain that a method can be |
| 52 delegated towards. |
| 53 |
| 54 .. versionadded:: 0.5.0 |
| 55 |
| 56 """ |
| 57 |
| 58 def __init__(self, *args, **kwargs): |
| 59 self.proxied = None |
| 60 |
| 61 def wrap(self, backend): |
| 62 ''' Take a backend as an argument and setup the self.proxied property. |
| 63 Return an object that be used as a backend by a :class:`.CacheRegion` |
| 64 object. |
| 65 ''' |
| 66 assert( |
| 67 isinstance(backend, CacheBackend) or |
| 68 isinstance(backend, ProxyBackend)) |
| 69 self.proxied = backend |
| 70 return self |
| 71 |
| 72 # |
| 73 # Delegate any functions that are not already overridden to |
| 74 # the proxies backend |
| 75 # |
| 76 def get(self, key): |
| 77 return self.proxied.get(key) |
| 78 |
| 79 def set(self, key, value): |
| 80 self.proxied.set(key, value) |
| 81 |
| 82 def delete(self, key): |
| 83 self.proxied.delete(key) |
| 84 |
| 85 def get_multi(self, keys): |
| 86 return self.proxied.get_multi(keys) |
| 87 |
| 88 def set_multi(self, mapping): |
| 89 self.proxied.set_multi(mapping) |
| 90 |
| 91 def delete_multi(self, keys): |
| 92 self.proxied.delete_multi(keys) |
| 93 |
| 94 def get_mutex(self, key): |
| 95 return self.proxied.get_mutex(key) |
OLD | NEW |