.ini 文件
cache.regions = default_term, second, short_term, long_term
cache.type = memcached
cache.url = 127.0.0.1:11211
cache.second.expire = 1
cache.short_term.expire = 60
cache.default_term.expire = 300
cache.long_term.expire = 3600
__init__.py
文件
from pyramid_beaker import set_cache_regions_from_settings
def main(global_config, **settings):
set_cache_regions_from_settings(settings)
...
测试.py文件
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
cache_opts = {
'cache.data_dir': '/tmp/cache/data',
'cache.lock_dir': '/tmp/cache/lock',
'cache.regions': 'short_term, long_term',
'cache.short_term.type': 'ext:memcached',
'cache.short_term.url': '127.0.0.1.11211',
'cache.short_term.expire': '3600',
'cache.long_term.type': 'file',
'cache.long_term.expire': '86400',
}
cache = CacheManager(**parse_cache_config_options(cache_opts))
@cache.region('short_term', 'test')
def test_method(*args, **kwargs):
执行上面的代码会出现错误:
...
File "c:\python27\lib\site-packages\python_memcached-1.48-py2.7.egg\memcache.py", line 1058, in __init__
self.port = int(hostData.get('port', 11211))
TypeError: int() argument must be a string or a number, not 'NoneType'
知道什么可能导致错误/或者我遗漏了任何东西吗?
最佳答案
看看你的测试配置,url
设置有错误:
'cache.short_term.url': '127.0.0.1.11211',
请注意,其中没有 :
冒号。您使用的 memcached 模块使用正则表达式来尝试解析该值,并且当您将该值指定为主机时,该方法将 port
设置为 None:
>>> host = '127.0.0.1.11211'
>>> re.match(r'^(?P<host>[^:]+)(:(?P<port>[0-9]+))?$', host).groupdict()
{'host': '127.0.0.1.11211', 'port': None}
这是你的回溯的来源。将 cache_opts
字典更改为:
'cache.short_term.url': '127.0.0.1:11211',
一切都会正常进行:
>>> host = '127.0.0.1:11211'
>>> re.match(r'^(?P<host>[^:]+)(:(?P<port>[0-9]+))?$', host).groupdict()
{'host': '127.0.0.1', 'port': '11211'}
关于python - Pyramid 烧杯缓存问题 - TypeError : int() argument must be a string or a number, 而不是 'NoneType',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12177541/