Skip to content

Instantly share code, notes, and snippets.

@frankmeola
Created April 9, 2017 16:05
Show Gist options
  • Save frankmeola/fb5714f0a05935b6f10895642d2c85ab to your computer and use it in GitHub Desktop.
Save frankmeola/fb5714f0a05935b6f10895642d2c85ab to your computer and use it in GitHub Desktop.
Version based cache expiration example
from flask import Flask, Response
import json, datetime
app = Flask(__name__)
@app.route("/")
def hello():
versionFile = open('versionInfo.txt', 'r')
version = int(versionFile.read())
data = {
'greeting' : 'hello world '+str(version),
'version' : version,
'asOf' : datetime.datetime.now().isoformat()
}
js = json.dumps(data)
resp = Response(js, status=200, mimetype='application/json')
return resp
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8986, debug=True)
from flask import Flask, render_template_string
import json
from contextlib import closing
from urllib2 import urlopen
from microCache import CachedDict
app = Flask(__name__)
cache = CachedDict()
def getData():
with closing(urlopen("http://localhost:8986")) as serviceJson:
return json.loads(serviceJson.read())
@app.route("/")
def hello():
serviceData = cache.get('serviceData')
print "from cache serviceData is " + str(serviceData)
if serviceData is None:
serviceData = getData()
#default timeout based cache expiration
cache.set('serviceData', serviceData, 60)
return render_template_string("{{ serviceData.greeting }}! (as of {{ serviceData.asOf }})", serviceData=serviceData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8988, debug=True)
from flask import Flask, render_template_string
import json
from contextlib import closing
from urllib2 import urlopen
from microCache import CachedDict
app = Flask(__name__)
cache = CachedDict()
def getData():
with closing(urlopen("http://localhost:8986")) as serviceJson:
return json.loads(serviceJson.read())
def shouldUpdateServiceData(serviceData):
try:
newerServiceData = getData()
print 'newer data from service ' + str(newerServiceData)
return {'shouldUpdate':(newerServiceData["version"] > serviceData["version"]), 'data':newerServiceData}
except Exception as err:
print "failed to update:{0}".format(err)
return {'shouldUpdate':False, 'data':None}
@app.route("/")
def hello():
serviceData = cache.get('serviceData')
print "from cache serviceData is " + str(serviceData)
if serviceData is None:
serviceData = getData()
cache.set('serviceData', serviceData, None)
cache.set('shouldCheckForUpdates', True, 60)
print 'shouldCheckForUpdates ' + str(cache.get('shouldCheckForUpdates'))
# check for version updates every so often so we don't make the call every request
if cache.get('shouldCheckForUpdates') is None:
print 'checking for updates'
# expire cache when content changes only if service is available
serviceResponse = shouldUpdateServiceData(serviceData)
if serviceResponse["shouldUpdate"]:
cache.set('serviceData', serviceResponse["data"], None)
cache.set('shouldCheckForUpdates', True, 60)
else:
# this has expired so reset so we check again
cache.set('shouldCheckForUpdates', True, 60)
return render_template_string("{{ serviceData.greeting }}! (as of {{ serviceData.asOf }})", serviceData=serviceData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8989, debug=True)
import time
class CachedDict(dict):
def isexpired(self, cacheItem):
print cacheItem
return cacheItem["duration"] is not None and cacheItem["timeStamp"] + cacheItem["duration"] < time.time()
def get(self, key):
if key not in self:
return None
else:
if self.isexpired(self[key]):
return None
else:
return self[key]["value"]
def set(self, key, value, timeout):
self[key] = {"value":value, "timeStamp":time.time(), "duration":timeout}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment