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 Test0(object): @app.cache.memoize() def add(self, b): import random return b + random.random() @app.route("/test0") def test0(): adder1 = Test0() adder2 = Test0() 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 '' @app.route("/test01") def test01(): adder = Test0()print 'adder 3', adder.add(3) print 'adder 2', adder.add(2) app.cache.delete_memoized(adder.add, adder, 3) print 'adder 3', adder1.add(3)print 'adder 2', adder1.add(2)
class Test1(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 = Test1(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')