金字塔烧杯缓存问题 - TypeError:int()参数必须是字符串或数字,而不是'NoneType'

时间:2012-08-29 12:07:23

标签: python pyramid beaker

.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)
...

test.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'

知道可能导致错误的原因/或者我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

查看您的测试配置,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 dict更改为:

'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'}