1

我已经使用 AWS 弹性缓存服务设置了内存缓存。我已经验证我可以远程登录到端点并存储和检索项目。

现在,我正在尝试将 memcache 用于 Flask-cache。我有以下代码。

from this import s, d
from string import translate, maketrans

from flask import Flask
from flask.ext.cache import Cache

app =  Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'memcached',
                                   'CACHE_MEMCACHED_SERVERS' : "My endpoint",
                                   'CACHE_KEY_PREFIX' : "optimization"})
#cache = Cache(app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR': '/tmp'})


@cache.cached(timeout=10, key_prefix="current_time")
def get_current_time():
    return time.ctime()

def random_zen_quote():
    """Pick a random quote from the Zen of Python"""
    transtable = maketrans("".join(d.keys()), "".join(d.values()))
    return random.choice(translate(s, transtable).split("\n")[2:])

@app.route("/")
def zen():
    return """
    <ul>
        <li><strong>It is cached:</strong> {cached}</li>
        <li><strong>It is not cached:</strong> {not_cached}</li>
    </ul>
    """.format(
            cached=get_current_time(),
            not_cached=random_zen_quote()
    )

if __name__ == "__main__":
    app.run(debug=True, port=5000, host='0.0.0.0')

我一直提到这个

我也安装了pylibmc。当我运行上面的代码并点击 url 时,我收到以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/projects/mv2/test.py", line 34, in zen
    cached=get_current_time(),
  File "/usr/local/lib/python2.7/site-packages/Flask_Cache-0.13.1-py2.7.egg/flask_cache/__init__.py", line 289, in decorated_function
    rv = self.cache.get(cache_key)
  File "/usr/local/lib/python2.7/site-packages/werkzeug/contrib/cache.py", line 406, in get
    return self._client.get(key)

我不确定错误是什么意思。

此外,当我使用本地文件系统时,它按预期工作。

4

1 回答 1

0

用这个。

'CACHE_MEMCACHED_SERVERS' : ("我的端点",),

于 2016-07-07T09:21:12.597 回答