oslo.memcache如何支持SASL详解

backend = oslo_cache.memcache_pool
the backend is <oslo_cache.backends.memcache_pool.PooledMemcachedBackend object at 0x7f46bb3b2d68>
		

前面可以看到,如果.conf文件中的cache 组中的backend = oslo_cache.memcache_pool

那么得到的backend就是oslo_cache.backends.memcache_pool.PooledMemcachedBackend

然后,我又在代码中将backend.set这个方法打印出来:

the backend.set is 
<bound method GenericMemcachedBackend.set of 
<oslo_cache.backends.memcache_pool.PooledMemcachedBackend object at 0x7fad77e497b8>>

可以看到backend.set是GenericMemcachedBackend.set中的方法,在dogpile/cache/backends/memcached.py文件中定义的

GenericMemcachedBackend.set中调用的是self.client.set(key, value, **self.set_arguments),而self.client又是一个属性method

oslo_cache.backends.memcache_pool.PooledMemcachedBackend在oslo_cache/backends/memcache_pool.py中定义的,继承的是GenericMemcachedBackend,所有可以调用其中的set方法。这个类又对client属性方法进行二次重载

the backend.client is 
<oslo_cache.backends.memcache_pool.ClientProxy object at 0x7fce5f48a710>
backend.client返回的是ClientProxy类对象

在ClientProxy类中,传入的是oslo_cache._memcache_pool.MemcacheClientPool参数

class ClientProxy(object):
    def __init__(self, client_pool):
        self.client_pool = client_pool

    def _run_method(self, __name, *args, **kwargs):
        with self.client_pool.acquire() as client:
            return getattr(client, __name)(*args, **kwargs)

    def __getattr__(self, name):
        return functools.partial(self._run_method, name)

这个函数暂时没有看懂,应该在调用ClientProxy(self.client_pool)的时候,直接调用__getattr__(self, name)函数吧,然后把client_pool中的conn对象直接返回了,这个时候,PooledMemcachedBackend.client就直接是client_pool中的conn的了,就可以直接调用其中的set、get等方法了!

现在的应用场景是想打开memcached服务的SALA安全认证,前面已经说了如何开启这个SALA安全认证的!

现在的想法是:

由于oslo_cache/_memcache_pool.py文件中的_MemcacheClient类继承的是memcache类
想着能不能创建一个_bmemcacge_pool.py文件,在这个文件中建立一个_bMemcacheClient类,让这个类继承bmemcache类
然后在memcache_pool.py文件中,用conf.sala_enable来判断调用哪个类!

现在在centos环境下进行验证:

把原来的demo代码copy到centos环境下进行验证,由于该环境没有安装openstack,因此并没有对应的库,需要执行:

pip install oslo_cache

执行:pip install python-memcached

安装memcache

执行demo之前,需要确认一下memcached服务是否已经开启了

netstat -an |grep 11211

如果没有开启的话,则执行命令

cd /opt/system/portaluser/memcached/bin
bash start

然后执行 python app.py

执行结果如下:

[jorh@localhost cache]$ python app.py 
the caching is True and the cache_time is 7200
_get_default_cache_region
<dogpile.cache.region.CacheRegion object at 0x7fd9f951fd50>
_CACHE is <cacheutils.CacheClient instance at 0x7fda07
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值