S
S
Spoon in the brain2019-07-12 20:49:44
memcached
Spoon in the brain, 2019-07-12 20:49:44

Flask caching not working, what should I do?

Good afternoon, the problem is that when I save a variable in the cache, and try to get it, the site says that there is nothing in the cache for my request.
Here is the code:

from flask import Flask
from werkzeug.contrib.cache import MemcachedCache

app = Flask(__name__)

cache = MemcachedCache(['localhost:11211'])


@app.route('/test')
def test():
  hw = 'Hello World!'
  cache.set('f', hw, timeout = None)
  get = cache.get('f')
  return render_template('pages/test.html', get=get)



if __name__ == '__main__':
  app.run()

I use Cachelib.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2019-07-12
@dimonchik2013

good code, keep
watching

P
pcdesign, 2019-07-13
@pcdesign

https://pythonhosted.org/Flask-Caching/
Try this module

from flask_caching import Cache
cache = Cache(app, config={'CACHE_TYPE': 'memcached',
                           "CACHE_MEMCACHED_SERVERS": ['127.0.0.1:11211']})

cache.set('f', "aaaa")
cache.get('f')

S
Spoon in the brain, 2019-07-14
@vessels

Thanks, problem solved, I just forgot to install memcache server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question