flask cache

测试代码:

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)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。