1

I have setup memcache using the AWS Elastic cache service. I have verified that I can telnet to the endpoint and store and retrieve items.

Now, I am trying to use memcache for Flask-cache. I have the following code.

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

I have been referring to this.

I have also install pylibmc. When I run the above code and hit the url, I get the following error:

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)

I am not sure what the error means.

Also, when I use a local file system, it works as expected.

kosta
  • 4,302
  • 10
  • 50
  • 104

1 Answers1

0

Use this.

'CACHE_MEMCACHED_SERVERS' : ("My endpoint",),

  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – lokusking Jul 07 '16 at 09:40