本文介绍了带有memcached的金字塔:如何使其工作?错误-MissingCacheParameter:必须输入网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Pyramid框架上有网站,并希望使用memcached进行缓存.出于测试原因,我使用了内存类型缓存,一切正常.我正在使用pyramid_beaker软件包.这是我以前的代码(工作版本).

I have site on Pyramid framework and want to cache with memcached. For testing reasons I've used memory type caching and everything was OK. I'm using pyramid_beaker package.Here is my previous code (working version).

.ini文件中

cache.regions = day, hour, minute, second
cache.type = memory
cache.second.expire = 1
cache.minute.expire = 60
cache.hour.expire = 3600
cache.day.expire = 86400

在views.py中:

In views.py:

from beaker.cache import cache_region

@cache_region('hour')
def get_popular_users():
    #some code to work with db
return some_dict

我在文档中发现的唯一.ini设置是关于使用内存和缓存文件类型的.但是我需要使用memcached.

The only .ini settings I've found in docs were about working with memory and file types of caching. But I need to work with memcached.

首先,我从Ubuntu官方存储库安装了软件包memcached,还从我的virtualenv安装了python-memcached.

First of all I've installed package memcached from Ubuntu official repository and also python-memcached to my virtualenv.

.ini文件中,我替换了cache.type = memory-> cache.type = memcached.而且我还有下一个错误:

In .ini file I've replaced cache.type = memory -> cache.type = memcached. And I've got next error:

MissingCacheParameter:网址为必填

MissingCacheParameter: url is required

我做错了什么?

提前谢谢!

推荐答案

因此,使用作为TurboGears文档的指南,您对URL进行了哪些设置?

So, using the TurboGears documentation as a guide, what settings do you have for the url?

[app:main]
beaker.cache.type = ext:memcached
beaker.cache.url = 127.0.0.1:11211
# you can also store sessions in memcached, should you wish
# beaker.session.type = ext:memcached
# beaker.session.url = 127.0.0.1:11211

在我看来, memcached需要网址正确初始化:

It looks to me as if memcached requires a url to initialize correctly:

def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, **params):
    NamespaceManager.__init__(self, namespace)

    if not url:
        raise MissingCacheParameter("url is required") 

我不太确定为什么代码允许url为可选(默认为None)然后要求它.我认为,仅以url作为参数会更简单.

I am not really sure why the code allows url to be optional (defaulting to None) and then requires it. I think it would have been simpler just to require the url as an argument.

稍后:针对您的下一个问题:

Later: in response to your next question:

我想说的是我阅读下面代码的方式,您必须提供lock_dirdata_dir来初始化self.lock_dir:

I'd say that the way I read the code below, you have to provide either lock_dir or data_dir to initialize self.lock_dir:

    if lock_dir:
        self.lock_dir = lock_dir
    elif data_dir:
        self.lock_dir = data_dir + "/container_mcd_lock"
    if self.lock_dir:
        verify_directory(self.lock_dir)

您可以使用以下测试代码来复制确切的错误:

You can replicate that exact error using this test code:

class Foo(object):
    def __init__(self, lock_dir=None, data_dir=None):
        if lock_dir:
            self.lock_dir = lock_dir
        elif data_dir:
            self.lock_dir = data_dir + "/container_mcd_lock"
        if self.lock_dir:
            verify_directory(self.lock_dir)

f = Foo()

结果是这样的:

>>> f = Foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in __init__
AttributeError: 'Foo' object has no attribute 'lock_dir'

这篇关于带有memcached的金字塔:如何使其工作?错误-MissingCacheParameter:必须输入网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 20:09