Permalink
Browse files

added cache support

  • Loading branch information...
1 parent 68407df commit 72b6d8536dfe62553ca1eda773b071d63cb2c70c ergo committed Nov 11, 2010
Showing with 144 additions and 1 deletion.
  1. +2 −0 docs/api.rst
  2. +57 −1 docs/index.rst
  3. +29 −0 pyramid_beaker/__init__.py
  4. +56 −0 pyramid_beaker/tests.py
View
@@ -3,6 +3,8 @@ API Documentation
.. automodule:: pyramid_beaker
+.. autofunction:: set_cache_regions_from_settings
+
.. autofunction:: session_factory_from_settings
.. autofunction:: BeakerSessionFactoryConfig
View
@@ -1,11 +1,15 @@
pyramid_beaker
==============
-A :term:`Beaker` session factory backend for :term:`Pyramid`.
+A :term:`Beaker` session factory backend for :term:`Pyramid`, also cache
+configurator.
Usage
-----
+Session management
+````````
+
In the configuration portion of your :term:`Pyramid` app, use the
:func:`pyramid_beaker.BeakerSessionFactoryConfig` function or the
:func:`pyramid_beaker.session_factory_from_settings` function to
@@ -57,6 +61,58 @@ in the ``__init__.py`` of your Pyramid application that uses an
config.end()
return config.make_wsgi_app()
+
+Beaker cache region support
+```````````````````````````
+
+In the configuration portion of your :term:`Pyramid` app, use the
+:func:`pyramid_beaker.set_cache_regions_from_settings` function to
+set Beaker's cache regions. At that point, you can use Beaker's `cache_region`
+functionality to enable caching for Your application.
+
+:func:`pyramid_beaker.set_cache_regions_from_settings` obtains region
+settings from the ``**settings`` dictionary passed to the
+Configurator. It assumes that you've placed cache configuration
+parameters prefixed with ``cache.`` in your Pyramid application's
+``.ini`` file. For example:
+
+.. code-block:: ini
+
+ [app:myapp]
+ .. other settings ..
+ cache.regions = default_term, second, short_term, long_term
+ cache.type = memory
+ cache.second.expire = 1
+ cache.short_term.expire = 60
+ cache.default_term.expire = 300
+ cache.long_term.expire = 3600
+
+If your ``.ini`` file has such settings, you can use
+:func:`pyramid_beaker.set_cache_regions_from_settings` in your
+application's configuration. For example, let's assume this code is
+in the ``__init__.py`` of your Pyramid application that uses an
+``.ini`` file with the ``cache.`` settings above to obtain its
+``**settings`` dictionary.
+
+.. code-block:: python
+
+ from pyramid_beaker import set_cache_regions_from_settings
+ from pyramid.configuration import configurator
+
+ def app(global_config, **settings):
+ """ This function returns a WSGI application.
+
+ It is usually called by the PasteDeploy framework during
+ ``paster serve``.
+ """
+ zcml_file = settings.get('configure_zcml', 'configure.zcml')
+ set_cache_regions_from_settings(settings)
+ config = Configurator(root_factory=get_root, settings=settings)
+ config.begin()
+ # ... other configuration stuff...
+ config.end()
+ return config.make_wsgi_app()
+
API
---
View
@@ -1,3 +1,4 @@
+from beaker import cache
from beaker.session import SessionObject
from beaker.util import coerce_session_params
@@ -88,3 +89,31 @@ def session_factory_from_settings(settings):
options = coerce_session_params(options)
return BeakerSessionFactoryConfig(**options)
+
+def set_cache_regions_from_settings(settings):
+ """ Add cache support to the Pylons application.
+
+ The ``settings`` passed to the configurator are used to setup
+ the cache options. Cache options in the settings should start
+ with either 'beaker.cache.' or 'cache.'.
+
+ """
+ cache_settings = {'regions':None}
+ for key in settings.keys():
+ for prefix in ['beaker.cache.', 'cache.']:
+ if key.startswith(prefix):
+ name = key.split(prefix)[1].strip()
+ cache_settings[name] = settings[key].strip()
+ if cache_settings['regions']:
+ for region in cache_settings['regions'].split(','):
+ region = region.strip()
+ region_settings = {}
+ for key, value in cache_settings.items():
+ if key.startswith(region):
+ region_settings[key.split('.')[1]] = value
+ region_settings['expire'] = int(region_settings.get('expire', 60))
+ region_settings.setdefault('lock_dir',
+ cache_settings.get('lock_dir'))
+ if 'type' not in region_settings:
+ region_settings['type'] = cache_settings.get('type', 'memory')
+ cache.cache_regions[region] = region_settings
View
@@ -112,3 +112,59 @@ def add_response_callback(self, callback):
class DummyResponse:
def __init__(self):
self.headerlist = []
+
+class TestCacheConfiguration(unittest.TestCase):
+ def _set_settings(self):
+ return {'cache.regions':'default_term, second, short_term, long_term',
+ 'cache.type':'memory',
+ 'cache.second.expire':'1',
+ 'cache.short_term.expire':'60',
+ 'cache.default_term.expire':'300',
+ 'cache.long_term.expire':'3600',
+ }
+
+ def test_add_cache_no_regions(self):
+ from pyramid_beaker import set_cache_regions_from_settings
+ import beaker
+ settings = self._set_settings()
+ beaker.cache.cache_regions = {}
+ settings['cache.regions'] = ''
+ set_cache_regions_from_settings(settings)
+ self.assertEqual(beaker.cache.cache_regions, {})
+
+ def test_add_cache_single_region_no_expire(self):
+ from pyramid_beaker import set_cache_regions_from_settings
+ import beaker
+ settings = self._set_settings()
+ beaker.cache.cache_regions = {}
+ settings['cache.regions'] = 'default_term'
+ del settings['cache.default_term.expire']
+ set_cache_regions_from_settings(settings)
+ default_term = beaker.cache.cache_regions.get('default_term')
+ self.assertEqual(default_term, {'expire': 60, 'type': 'memory',
+ 'lock_dir': None})
+
+ def test_add_cache_multiple_region(self):
+ from pyramid_beaker import set_cache_regions_from_settings
+ import beaker
+ settings = self._set_settings()
+ beaker.cache.cache_regions = {}
+ settings['cache.regions'] = 'default_term, short_term'
+ settings['cache.lock_dir'] = 'foo'
+ settings['cache.short_term.expire'] = '60'
+ settings['cache.default_term.type'] = 'file'
+ settings['cache.default_term.expire'] = '300'
+ set_cache_regions_from_settings(settings)
+ default_term = beaker.cache.cache_regions.get('default_term')
+ short_term = beaker.cache.cache_regions.get('short_term')
+ self.assertEqual(short_term.get('expire'),
+ int(settings['cache.short_term.expire']))
+ self.assertEqual(short_term.get('lock_dir'), settings['cache.lock_dir'])
+ self.assertEqual(short_term.get('type'), 'memory')
+
+ self.assertEqual(default_term.get('expire'),
+ int(settings['cache.default_term.expire']))
+ self.assertEqual(default_term.get('lock_dir'),
+ settings['cache.lock_dir'])
+ self.assertEqual(default_term.get('type'),
+ settings['cache.default_term.type'])

0 comments on commit 72b6d85

Please sign in to comment.