码迷,mamicode.com
首页 > 系统相关 > 详细

flask cache

时间:2015-03-03 20:17:15      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

测试代码:

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

app = Flask(__name__)
app.config[CACHE_TYPE] = simple
app.cache = Cache(app)

class Adder(object):
    @app.cache.memoize()
    def add(self, b):
        import random
        return b + random.random()

@app.route("/add/<name>")
def add(name):
    adder1 = Adder()
    adder2 = Adder()
    print adder1, adder1.add(3)
    print adder2, adder2.add(3)
    app.cache.delete_memoized(adder1.add)
    # app.cache.delete_memoized(adder2.add)
    print adder1, adder1.add(3)
    print adder2, adder2.add(3)
    app.cache.delete_memoized(Adder.add)
    print adder1, adder1.add(3)
    print adder2, adder2.add(3)
    return ‘‘

class Test(object):
    def __init__(self, name):
        self.name = name

    @app.cache.memoize()
    def test(self):
        import random
        return random.random()
    foo_prop = property(test)

@app.route("/test/<name>")
def test(name):
    t = Test(name)
    print t1, t.foo_prop
    print t1, t.foo_prop
    app.cache.delete_memoized(t.test)
    # app.cache.delete_memoized(adder2.add)
    print t2, t.foo_prop
    return ‘‘

class Test2(object):
    def __init__(self, name):
        self.name = name

    @classmethod
    @app.cache.memoize()
    def add(cls, a):
        import random
        return a + random.random()
    foo_prop = property(test)

@app.route("/test2/<name>")
def test2(name):
    x = Test2.add(1)
    y = Test2.add(2)
    print x, x
    print y, y
    x = Test2.add(1)
    y = Test2.add(2)
    print x, x
    print y, y
    app.cache.delete_memoized(Test2.add, Test2, 1)
    x = Test2.add(1)
    y = Test2.add(2)
    print x, x
    print y, y
    return ‘‘

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

 

flask cache

标签:

原文地址:http://www.cnblogs.com/bluewhale/p/4311697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!