Changeset - 2bf25d54baed
[Not reviewed]
0 2 0
Lance Edgar (lance) - 2 years ago 2022-08-06 20:20:57
lance@edbob.org
Add support for memcached as beaker config cache backend

technically this is for other backends too, i just tried memcached
2 files changed with 32 insertions and 15 deletions:
0 comments (0 inline, 0 general)
rattail/config.py
Show inline comments
 
@@ -149,28 +149,37 @@ class RattailConfig(object):
 
            from beaker.cache import CacheManager
 
            from beaker.util import parse_cache_config_options
 

	
 
            # default cache settings
 
            options = {
 
                'cache.type': self.get(
 
                    'rattail.config', 'beaker_cache.type',
 
                    usedb=False,
 
                    default='file'),
 
                'cache.data_dir': self.get(
 
                    'rattail.config', 'beaker_cache.data_dir',
 
                    usedb=False,
 
                    default=os.path.join(self.appdir(), 'cache', 'config', 'data')),
 
                'cache.lock_dir': self.get(
 
                    'rattail.config', 'beaker_cache.lock_dir',
 
                    usedb=False,
 
                    default=os.path.join(self.appdir(), 'cache', 'config', 'lock')),
 
                'cache.type': 'file',
 
                'cache.data_dir': os.path.join(self.appdir(), 'cache',
 
                                               'config', 'data'),
 
                'cache.lock_dir': os.path.join(self.appdir(), 'cache',
 
                                               'config', 'lock'),
 
            }
 

	
 
            # but let config override and supplement
 
            if self.parser.has_section('rattail.config'):
 
                for option in self.parser.options('rattail.config'):
 
                    prefix = 'beaker_cache.'
 
                    if option.startswith(prefix):
 
                        key = 'cache.{}'.format(option[len(prefix):])
 
                        options[key] = self.parser.get('rattail.config', option)
 

	
 
            # and with those options, make a cache manager
 
            self.beaker_cache_manager = CacheManager(
 
                **parse_cache_config_options(options))
 

	
 
            # our namespace is pretty normal...but on python2, and
 
            # using memcached as backend, there is an encoding gotcha,
 
            # which we must fix here
 
            namespace = 'rattail.config'
 
            if six.PY2:
 
                namespace = namespace.encode('utf_8')
 

	
 
            # and finally, make our cache
 
            self.beaker_config_cache = self.beaker_cache_manager.get_cache(
 
                'rattail.config',
 
                type='file',    # TODO: (why) must we declare this?
 
                expire=3600)
 
                namespace)
 

	
 
    def read_file(self, path, recurse=True, require=False):
 
        """
 
@@ -355,6 +364,7 @@ class RattailConfig(object):
 
            session = app.make_session(continuum_user=None)
 
            value = app.get_setting(session, name)
 
            session.close()
 
            log.debug("had to (re-)fetch setting '%s' from db: %s", name, value)
 
            return value
 

	
 
        return self.beaker_config_cache.get(key=name, createfunc=get_value)
setup.py
Show inline comments
 
@@ -129,6 +129,13 @@ extras = {
 
        'SQLAlchemy-Continuum',         # 1.1.5
 
    ],
 

	
 
    'memcached': [
 
        #
 
        # package                       # low                   high
 

	
 
        'pylibmc',                      # 1.6.1
 
    ],
 

	
 
    'docs': [
 
        #
 
        # package                       # low                   high
0 comments (0 inline, 0 general)