killerxf 发表于 2015-11-18 09:55:43

flask cache with memcache

  一开始 flask的cache type是 simple,然后在生产环境 可不能这么用吧,然后开始研究用memcached,然后一直出错。出错的原因是 libmemcached的版本和pylibmc的版本不对,然后,我结局了他就好了,这里就不标明如何搭建环境了,值得提醒的是,在pip 安装包的时候一定要指定好版本。
  代码如下:
  config.py
  (pythonenv)$ cat config.py
import os
class Config(object):
DEBUG=True
CACHE_TYPE = 'memcached'
SECRET_KEY = "d73b04b0e696b0945283defa3eee4538"
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(BASE_DIR, 'app.db')
CACHE_MEMCACHED_SERVERS=["10.210.71.145:11211"]
class MysqlConfig(Config):
SQLALCHEMY_DATABASE_URI = 'mysql://hello:hello@10.210.71.145:3306/sqlalchemy'
class MongoDBConfig(Config):
MONGOALCHEMY_DATABASE="mydb"
MONGOALCHEMY_SERVER="10.210.71.145"
MONGOALCHEMY_PORT=8888
POST_PER_PAGE = 3

run.py
  (pythonenv)$ cat run.py
from flask import Flask
import random
# import the flask extension
from flask.ext.cache import Cache   
app = Flask(__name__)
#import config setting
app.config.from_object("config.Config")
# register the cache instance and binds it on to your app
app.cache = Cache(app)   
@app.route("/")
@app.cache.cached(timeout=500,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')

github ,stackoverflow 是一对好基友,好工具啊。
  cache 验证的代码:
  <?php
$mem = new Memcache;
$mem->connect(&quot;127.0.0.1&quot;,11211);
#add
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo &quot;Get key1 value: &quot; . $val .&quot;\n&quot;;
#modify
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo &quot;Get key1 value: &quot; . $val . &quot;\n&quot;;
#insert array
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo &quot;Get key2 value: &quot;;
print_r($val2);
echo &quot;\n&quot;;
#delete
$mem->delete('key1');
$val = $mem->get('key1');
echo &quot;Get key1 value: &quot; . $val . &quot;\n&quot;;
#add
$mem->set('key1', 'This is k1 value', 0, 60);
$val = $mem->get('key1');
echo &quot;Get key1 value: &quot; . $val .&quot;\n&quot;;
#add
$mem->set('key2', 'This is k2 value', 0, 60);
$val = $mem->get('key2');
echo &quot;Get key2 value: &quot; . $val .&quot;\n&quot;;

#add
$mem->set('key3', 'This is k3 value', 0, 60);
$val = $mem->get('key3');
echo &quot;Get key3 value: &quot; . $val .&quot;\n&quot;;
$items=$mem->getExtendedStats(&quot;items&quot;);
$host=&quot;127.0.0.1&quot;;
$port=11211;
$items=$items[&quot;$host:$port&quot;]['items'];
print &quot;###############&quot;;
print_r($items);
print &quot;###############&quot;;
foreach($items as $key=>$values)
{
$number=$key;
$str=$mem->getExtendedStats (&quot;cachedump&quot;,$number,0);
$line=$str[&quot;$host:$port&quot;];
if( is_array($line) && count($line)>0)
{
foreach($line as $key=>$value)
{
echo $key.'=>';
print_r($mem->get($key));
echo &quot;\r\n&quot;;
}
}
}
$mem->flush();
$val2 = $mem->get('key2');
echo &quot;Get key2 value: &quot;;
print_r($val2);
echo &quot;\n&quot;;
$mem->close();
?>验证的时候,页面会有直观的体现,同时,通过代码访问memcached的,得到的效果更佳真实




  太爽了。

版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: flask cache with memcache