1

I have the following three files.

app.py

from flask_restful import Api
from lib import globals
from flask import Flask
from flask.ext.cache import Cache

globals.algos_app =  Flask(__name__)

#cache in file system
globals.cache = Cache(globals.algos_app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR': '/tmp'})

api = Api(globals.algos_app)
api.add_resource(Test, '/test')

if __name__ == '__main__':
    globals.algos_app.run(host='0.0.0.0', debug=True)

globals.py

global algos_app
global cache

Test.py

from flask_restful import Resource
from lib import globals
from flask_restful import Resource
import time

class Test(Resource):

    def get(self):
        return self.someMethod()

    def post(self):
        globals.cache.clear()
        return self.someMethod()

    @globals.cache.cached()
    def someMethod(self):
        return str(time.ctime())

I have a GET method which needs to the value from the cache and a POST method which updates the cache by first clearing the cache.

However, no matter I call the GET or the POST method, it always gives me the value from the cache.

PS: At the moment I am simply testing on the development server however I do need to deploy it using WSGI later.

kosta
  • 4,302
  • 10
  • 50
  • 104

1 Answers1

0

I am not sure if it is the best way, but I did it using the following way.

class Test(Resource):

    def get(self):
        return globals.cache.get('curr_time')

    def post(self):
        result = self.someMethod()
        globals.cache.set('curr_time', result, timeout=3600)

    def someMethod(self):
        return str(time.ctime())
kosta
  • 4,302
  • 10
  • 50
  • 104