如何使用flask-cache和memcached? [英] how to use flask-cache and memcached?

查看:414
本文介绍了如何使用flask-cache和memcached?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


下面的类型为simple的flask-cache的一个例子,但是我怎样才能在memcache中使用flask-cache呢?我需要你的帮助,谢谢你:)

  from flask import Flask 
import random

#从flask.ext.cache导入缓存
导入缓存

app = Flask(__ name__)

#import配置设置
app.config [CACHE_TYPE] =简单

#注册缓存实例并绑定到您的应用
app.cache =缓存(应用)

@ #缓存这个视图30秒
def cached_view():
a = $ appcache.cached(timeout = 50,key_prefix =hello random.randint(0,100)
return str(a)

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


解决方案

问题

Q :如何使用memcached与flask-cache

A :只需将缓存类型从简单修改为 memcached ,那么很容易

注意:

您应该注意的最重要的事情是您应该安装libmemcached,pylibmc和python-dev。此外,最重要的是libmemcached的版本应该匹配pylibmc的版本,否则当 pip install pylibmc

an example for flask-cache with type "simple" below but how can i use flask-cache with memcache ? I need ur help thank you :)

from flask import Flask
import random

# import the flask extension
from flask.ext.cache import Cache

app = Flask(__name__)

#import config setting
app.config["CACHE_TYPE"]="simple"

# register the cache instance and binds it on to your app 
app.cache = Cache(app)

@app.route("/")
@app.cache.cached(timeout=50,key_prefix="hello")  # cache this view for 30 seconds
def cached_view():
    a=random.randint(0,100)
    return str(a)

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

解决方案

:) I have solved this problem

Q:How to use memcached with flask-cache

A: just modify the cache type from "simple" to "memcached",so easy

attention:

the most important thing you should notice is that you should install libmemcached, pylibmc and python-dev. Also, the most important is that the version of libmemcached should match the version of pylibmc,otherwise it would be wrong when pip install pylibmc

这篇关于如何使用flask-cache和memcached?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆