本文整理汇总了Python中beaker.cache.CacheManager.get_cache_region方法的典型用法代码示例。如果您正苦于以下问题:Python CacheManager.get_cache_region方法的具体用法?Python CacheManager.get_cache_region怎么用?Python CacheManager.get_cache_region使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类beaker.cache.CacheManager
的用法示例。
在下文中一共展示了CacheManager.get_cache_region方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestBeakerBackend
# 需要导入模块: from beaker.cache import CacheManager [as 别名]
# 或者: from beaker.cache.CacheManager import get_cache_region [as 别名]
class TestBeakerBackend(unittest.TestCase):
def setUp(self):
reg = {}
reg['cache.expire'] = 10
reg['cache.regions'] = 'session'
reg['cache.type'] = 'memory'
reg['batch.user.session.timeout'] = 1
self.cachemgr = CacheManager(**parse_cache_config_options(reg))
self.backend = BeakerBackend(reg)
def __get_region(self, key):
return self.cachemgr.get_cache_region('edware_session_' + key, 'session')
def test_create_new_session(self):
session = Session()
session.set_session_id('123')
self.backend.create_new_session(session)
self.assertIsNotNone(self.__get_region('123').get('123'))
def test_get_session_from_persistence_with_existing_session(self):
session = Session()
session.set_session_id('456')
session.set_uid('abc')
self.backend.create_new_session(session)
lookup = self.backend.get_session('456')
self.assertEqual(lookup.get_uid(), 'abc')
def test_get_session_invalid_session(self):
lookup = self.backend.get_session('idontexist')
self.assertIsNone(lookup)
def test_delete_session(self):
session = Session()
session.set_session_id('456')
session.set_uid('abc')
self.backend.create_new_session(session)
self.backend.delete_session('456')
self.assertFalse('456' in self.__get_region('456'))
def test_update_session(self):
session = Session()
session.set_session_id('456')
session.set_uid('abc')
self.backend.create_new_session(session)
session.set_uid('def')
self.backend.update_session(session)
lookup = self.__get_region('456').get('456')
self.assertEquals(lookup.get_uid(), 'def')
def test_overwrite_timeout(self):
session = Session()
session.set_session_id('1456')
session.set_uid('abc')
self.backend.create_new_session(session, overwrite_timeout=True)
time.sleep(1)
self.assertIsNone(self.backend.get_session('1456'))
示例2: BeakerBackend
# 需要导入模块: from beaker.cache import CacheManager [as 别名]
# 或者: from beaker.cache.CacheManager import get_cache_region [as 别名]
class BeakerBackend(Backend):
'''
Manipulates session that resides in persistent storage (memory, memcached)
'''
def __init__(self, settings):
self.cache_mgr = CacheManager(**parse_cache_config_options(settings))
self.batch_timeout = int(settings.get('batch.user.session.timeout'))
def create_new_session(self, session, overwrite_timeout=False):
'''
Creates a new session
'''
self.update_session(session, overwrite_timeout=overwrite_timeout)
def update_session(self, session, overwrite_timeout=False):
'''
Given a session, persist it
'''
_id = session.get_session_id()
region = self.__get_cache_region(_id)
# Overwrite the timeout for batch user sessions
if overwrite_timeout:
region.expiretime = self.batch_timeout
region.put(_id, session)
def get_session(self, session_id):
'''
Return session from persistent storage
'''
region = self.__get_cache_region(session_id)
if session_id not in region:
logger.info('Session is not found in cache. It may have expired or connection to memcached is down')
return None
return region.get(session_id)
def delete_session(self, session_id):
'''
Delete session from persistent storage
'''
# delete from db doesn't work
region = self.__get_cache_region(session_id)
if session_id in region:
# works for memcached
region.remove_value(session_id)
def __get_cache_region(self, key):
return self.cache_mgr.get_cache_region('edware_session_' + key, 'session')
def clear(self):
'''
clear cache
'''
self.cache_region.clear()
示例3: CacheManager
# 需要导入模块: from beaker.cache import CacheManager [as 别名]
# 或者: from beaker.cache.CacheManager import get_cache_region [as 别名]
class CacheManager(grok.GlobalUtility):
""" a cache manager that wraps beaker
"""
grok.implements(ICacheManager)
grok.provides(ICacheManager)
default_region_options = {
'lock_dir': '/tmp/beaker',
'type': 'memorylru',
'max_items': '1000',
}
def __init__(self):
self._parse_config()
self.bcm = BCM(**self._parse_config())
self.regions = self.bcm.regions
def get_cache_from_region(self, namespace, region):
try:
return self.bcm.get_cache_region(namespace, region)
except BeakerException as e:
logger.warn('no specific configuration for region %s'
' using defaults (%s) : %s',
region, repr(self.default_region_options), e)
self._create_region_from_default(region)
return self.bcm.get_cache_region(namespace, region)
def get_cache(self, namespace, **options):
# Doesn't rely on region for configuration.
return self.bcm.get_cache(namespace, **options)
def _parse_config(self):
zconf = getattr(getConfiguration(), 'product_config', {})
cache_config = zconf.get('silva.core.cache', {})
regions = {}
for key, value in cache_config.iteritems():
if '.' in key:
try:
region, param = key.split('.', 1)
except ValueError:
continue
if region not in regions:
regions[region] = {}
regions[region][param] = value
if regions.has_key('default'):
self.default_region_options = regions['default']
del regions['default']
options = self.default_region_options.copy()
options['cache_regions'] = regions
return options
def _create_region_from_default(self, region):
""" Create the region with default
"""
options = self.default_region_options.copy()
self.regions[region] = options
self.bcm.regions.update({region: options})
beaker.cache.cache_regions.update({region: options})
示例4: Application
# 需要导入模块: from beaker.cache import CacheManager [as 别名]
# 或者: from beaker.cache.CacheManager import get_cache_region [as 别名]
#.........这里部分代码省略.........
log.info('Starting %s in PID %i.', __name__, os.getpid())
else:
log.info('Starting %s.', __name__)
if SERVER_RUNNING_FILE:
log.info('PID file written as %s', SERVER_RUNNING_FILE)
else:
log.info('No PID file written by default.')
when_ready(self)
try:
self._pyrodaemon.requestLoop(
loopCondition=lambda: not self._vcsserver._shutdown)
finally:
self._pyrodaemon.shutdown()
def _configure_locale(self):
if self.locale:
log.info('Settings locale: `LC_ALL` to %s' % self.locale)
else:
log.info(
'Configuring locale subsystem based on environment variables')
try:
# If self.locale is the empty string, then the locale
# module will use the environment variables. See the
# documentation of the package `locale`.
locale.setlocale(locale.LC_ALL, self.locale)
language_code, encoding = locale.getlocale()
log.info(
'Locale set to language code "%s" with encoding "%s".',
language_code, encoding)
except locale.Error:
log.exception(
'Cannot set locale, not configuring the locale system')
def _configure_pyro(self):
if self.threadpool_size is not None:
log.info("Threadpool size set to %s", self.threadpool_size)
Pyro4.config.THREADPOOL_SIZE = self.threadpool_size
if self.timeout not in (None, 0, 0.0, '0'):
log.info("Timeout for RPC calls set to %s seconds", self.timeout)
Pyro4.config.COMMTIMEOUT = float(self.timeout)
Pyro4.config.SERIALIZER = 'pickle'
Pyro4.config.SERIALIZERS_ACCEPTED.add('pickle')
Pyro4.config.SOCK_REUSE = True
# Uncomment the next line when you need to debug remote errors
# Pyro4.config.DETAILED_TRACEBACK = True
def _initialize_cache(self):
cache_config = parse_cache_config_options(self.cache_config)
log.info('Initializing beaker cache: %s' % cache_config)
self.cache = CacheManager(**cache_config)
def _create_daemon_and_remote_objects(self, host='localhost',
port=settings.PYRO_PORT):
daemon = Pyro4.Daemon(host=host, port=port)
self._vcsserver = VcsServer()
uri = daemon.register(
self._vcsserver, objectId=settings.PYRO_VCSSERVER)
log.info("Object registered = %s", uri)
if GitFactory and GitRemote:
git_repo_cache = self.cache.get_cache_region('git', region='repo_object')
git_factory = GitFactory(git_repo_cache)
self._git_remote = GitRemote(git_factory)
uri = daemon.register(self._git_remote, objectId=settings.PYRO_GIT)
log.info("Object registered = %s", uri)
else:
log.info("Git client import failed")
if MercurialFactory and HgRemote:
hg_repo_cache = self.cache.get_cache_region('hg', region='repo_object')
hg_factory = MercurialFactory(hg_repo_cache)
self._hg_remote = HgRemote(hg_factory)
uri = daemon.register(self._hg_remote, objectId=settings.PYRO_HG)
log.info("Object registered = %s", uri)
else:
log.info("Mercurial client import failed")
if SubversionFactory and SvnRemote:
svn_repo_cache = self.cache.get_cache_region('svn', region='repo_object')
svn_factory = SubversionFactory(svn_repo_cache)
self._svn_remote = SvnRemote(svn_factory, hg_factory=hg_factory)
uri = daemon.register(self._svn_remote, objectId=settings.PYRO_SVN)
log.info("Object registered = %s", uri)
else:
log.info("Subversion client import failed")
self._git_remote_wsgi = self.remote_wsgi.GitRemoteWsgi()
uri = daemon.register(self._git_remote_wsgi,
objectId=settings.PYRO_GIT_REMOTE_WSGI)
log.info("Object registered = %s", uri)
self._hg_remote_wsgi = self.remote_wsgi.HgRemoteWsgi()
uri = daemon.register(self._hg_remote_wsgi,
objectId=settings.PYRO_HG_REMOTE_WSGI)
log.info("Object registered = %s", uri)
self._pyrodaemon = daemon