Permalink
Browse files

Merge pull request #8 from Pylons/feature.beaker-1.6-compat

Beaker 1.6+ compat
  • Loading branch information...
2 parents be1b686 + 6c6e4bf commit 7a18850ab24d9ac21974c6593c1f6032a376bed7 @mcdonc mcdonc committed Nov 14, 2011
Showing with 58 additions and 17 deletions.
  1. +4 −0 CONTRIBUTORS.txt
  2. +9 −0 docs/index.rst
  3. +25 −14 pyramid_beaker/__init__.py
  4. +20 −3 pyramid_beaker/tests.py
View
@@ -116,4 +116,8 @@ Contributors
- Wichert Akkerman, 2011/04/29
+- Carson Lam, 2011/07/16
+
- Blaise Laflamme, 2011/10/22
+
+- Michael Merickel, 2011/11/13
View
@@ -151,9 +151,18 @@ Inherited region settings
The following optional region settings inherit from the main cache
configuration or default as specified:
+``data_dir``
+ Inherits if specified.
+
+``enabled``
+ Inherits or defaults to True.
+
``expire``
Inherits or defaults to 60 seconds.
+``key_length``
+ Inherits or defaults to 250 characters.
+
``lock_dir``
Inherits if specified.
View
@@ -2,6 +2,7 @@
from beaker import cache
from beaker.session import SessionObject
+from beaker.util import coerce_cache_params
from beaker.util import coerce_session_params
from pyramid.interfaces import ISession
@@ -136,20 +137,30 @@ def set_cache_regions_from_settings(settings):
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))
- if 'lock_dir' not in region_settings:
- region_settings['lock_dir'] = cache_settings.get('lock_dir')
- if 'type' not in region_settings:
- region_settings['type'] = cache_settings.get('type', 'memory')
- if 'url' not in region_settings:
- region_settings['url'] = cache_settings.get('url')
+ coerce_cache_params(cache_settings)
+
+ if 'enabled' not in cache_settings:
+ cache_settings['enabled'] = True
+
+ regions = cache_settings['regions']
+ if regions:
+ for region in regions:
+ if not region: continue
+ region_settings = {
+ 'data_dir': cache_settings.get('data_dir'),
+ 'lock_dir': cache_settings.get('lock_dir'),
+ 'expire': cache_settings.get('expire', 60),
+ 'enabled': cache_settings['enabled'],
+ 'key_length': cache_settings.get('key_length', 250),
+ 'type': cache_settings.get('type'),
+ 'url': cache_settings.get('url'),
+ }
+ region_prefix = '%s.' % region
+ region_len = len(region_prefix)
+ for key in cache_settings.keys():
+ if key.startswith(region_prefix):
+ region_settings[key[region_len:]] = cache_settings.pop(key)
+ coerce_cache_params(region_settings)
cache.cache_regions[region] = region_settings
def includeme(config):
View
@@ -260,8 +260,10 @@ def test_add_cache_single_region_no_expire(self):
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, {'url': None, 'expire': 60,
- 'type': 'memory', 'lock_dir': None})
+ self.assertEqual(default_term,
+ {'url': None, 'expire': 60, 'type': 'memory',
+ 'lock_dir': None, 'data_dir': None, 'enabled': True,
+ 'key_length': 250,})
def test_add_cache_multiple_region(self):
from pyramid_beaker import set_cache_regions_from_settings
@@ -273,20 +275,23 @@ def test_add_cache_multiple_region(self):
settings['cache.short_term.expire'] = '60'
settings['cache.default_term.type'] = 'file'
settings['cache.default_term.expire'] = '300'
+ settings['cache.default_term.enabled'] = 'false'
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.assertTrue(short_term.get('enabled'))
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'])
+ self.assertFalse(default_term.get('enabled'))
def test_region_inherit_url(self):
from pyramid_beaker import set_cache_regions_from_settings
@@ -304,6 +309,18 @@ def test_region_inherit_url(self):
short_term = beaker.cache.cache_regions.get('short_term')
self.assertEqual(short_term.get('url'), settings['cache.url'])
self.assertEqual(default_term.get('url'), settings['cache.url'])
+
+ def test_region_inherit_enabled(self):
+ from pyramid_beaker import set_cache_regions_from_settings
+ import beaker
+ settings = self._set_settings()
+ settings['cache.enabled'] = 'false'
+ beaker.cache.cache_regions = {}
+ 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.assertFalse(short_term.get('enabled'))
+ self.assertFalse(default_term.get('enabled'))
class TestIncludeMe(unittest.TestCase):
def test_includeme(self):
@@ -314,4 +331,4 @@ def test_includeme(self):
includeme(config)
session_factory = config.registry.queryUtility(ISessionFactory)
self.assertEqual(str(session_factory),
- "<class 'pyramid_beaker.PyramidBeakerSessionObject'>")
+ "<class 'pyramid_beaker.PyramidBeakerSessionObject'>")

0 comments on commit 7a18850

Please sign in to comment.