金字塔与 memcached:如何使其工作?错误 - MissingCacheParameter: url 为必填项

发布于 2025-01-02 16:40:10 字数 959 浏览 1 评论 0原文

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

.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 中:

from beaker.cache import cache_region

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

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

首先,我从 Ubuntu 官方存储库安装了 memcached 包,并将 python-memcached 安装到了我的 virtualenv 中。

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

beaker.exceptions.MissingCacheParameter

MissingCacheParameter:url 为必填项

我做错了什么?

提前致谢!

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

In .ini file

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

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

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.

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

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

beaker.exceptions.MissingCacheParameter

MissingCacheParameter: url is required

What am I doing wrong?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2025-01-09 16:40:10

因此,使用 TurboGears 文档 作为指南,您对网址?

[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 需要一个 url 来正确初始化:

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 作为参数会更简单。


后来:回答你的下一个问题:

当我使用cache.url时,我遇到了下一个错误:AttributeError:
“MemcachedNamespaceManager”对象没有属性“lock_dir”

我想说的是,按照我阅读下面代码的方式,您必须提供 lock_dirdata_dir 来初始化 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)

您可以使用此测试代码复制该确切错误:

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'

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

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

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:

when I used cache.url I've got next error: AttributeError:
'MemcachedNamespaceManager' object has no attribute '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()

It turns out like this:

>>> 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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文