2012-02-04 37 views
3

我有Pyramid框架的網站,並希望緩存與memcached。出於測試的原因,我使用了內存類型緩存,一切正常。我正在使用pyramid_beaker包。 這是我以前的代碼(工作版本)。金字塔與memcached:如何使其工作?錯誤 - MissingCacheParameter:url是必需的

.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,並且還在我的virtualenv上安裝了python-memcached

.ini文件我已經替換cache.type = memory - >cache.type = memcached。我有下一個錯誤:

beaker.exceptions.MissingCacheParameter

MissingCacheParameter: url is required

我在做什麼錯?

在此先感謝!

回答

4

因此,使用TurboGears documentation作爲指南,你有什麼設置的網址?

[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 requires a 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是可選的(默認爲無),然後需要它。我認爲只要將url作爲參數就簡單了。


後來:響應您的下一個問題:

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

您可以使用此測試代碼複製,準確的錯誤0

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' 
+0

當我用cache.url我有一個錯誤:'AttributeError的:「MemcachedNamespaceManager」對象有沒有屬性「lock_dir'' – 2012-02-04 21:06:31

+0

而且在我的代碼的網址需要def _ _init __(self,namespace,url,memcache_module ='auto',data_dir = None,lock_dir = None,** kw): – 2012-02-04 21:10:13

+2

當我添加'lock_dir'時,一切都開始正常工作。但爲什麼memcached需要該目錄?它是內存緩存,而不是文件緩存... – 2012-02-05 06:43:40