oslo_cache主要依赖于dogpile.cache库
oslo.cache缓存机制的核心实现都定义在oslo_cache.core模块中,而缓存机制的实现主要依赖于以下几个方法:
- create_region(function=function_key_generator):创建缓存区,该方法主要调用了dogpile.cache模块的make_region(function_key_generator=function)方法创建了一个CacheRegion对象。
- configure_cache_region(conf, region):该方法通过配置文件中缓存的相关配置以及CacheRegion对象提供的配置方法配置缓存后端。
- get_memoization_decorator(conf, region, group, expiration_group=None):这是一个根据CacheRegion对象中cache_on_arguments()装饰器定义的oslo.cache的一个装饰器,其会根据group或expiration_group确定是否允许缓存以及缓存的时间。
create_region返回CacheRegion对象
configure_cache_region通过conf和创建好的region对象配置缓存后端backend
def configure_from_config(self, config_dict, prefix):
"""Configure from a configuration dictionary
and a prefix.
Example::
local_region = make_region()
memcached_region = make_region()
# regions are ready to use for function
# decorators, but not yet for actual caching
# later, when config is available
myconfig = {
"cache.local.backend":"dogpile.cache.dbm",
"cache.local.arguments.filename":"/path/to/dbmfile.dbm",
"cache.memcached.backend":"dogpile.cache.pylibmc",
"cache.memcached.arguments.url":"127.0.0.1, 10.0.0.1",
}
local_region.configure_from_config(myconfig, "cache.local.")
memcached_region.configure_from_config(myconfig,
"cache.memcached.")
"""
config_dict = coerce_string_conf(config_dict)
return self.configure(
config_dict["%sbackend" % prefix],
expiration_time=config_dict.get(
"%sexpiration_time" % prefix, None
),
_config_argument_dict=config_dict,
_config_prefix="%sarguments." % prefix,
wrap=config_dict.get("%swrap" % prefix, None),
replace_existing_backend=config_dict.get(
"%sreplace_existing_backend" % prefix, False
),
)
config_dict就是根据oslo_cache配置的conf文件将其转化为字典形式参数
查看configure函数(代码做了删减),实际上是根据conf加载了所需要缓存后端,实例化了self.backend
def configure(
self,
backend,
expiration_time=None,
arguments=None,
_config_argument_dict=None,
_config_prefix=None,
wrap=None,
replace_existing_backend=False,
region_invalidator=None,
):
if "backend" in self.__dict__ and not replace_existing_backend:
raise exception.RegionAlreadyConfigured(
"This region is already "
"configured with backend: %s. "
"Specify replace_existing_backend=True to replace."
% self.backend
)
try:
backend_cls = _backend_loader.load(backend)
except PluginLoader.NotFound:
raise exception.PluginNotFound(
"Couldn't find cache plugin to load: %s" % backend
)
if _config_argument_dict:
self.backend = backend_cls.from_config_dict(
_config_argument_dict, _config_prefix
)
else:
self.backend = backend_cls(arguments or {})