Skip to content

Instantly share code, notes, and snippets.

@MercuryRising
Created October 12, 2012 00:14
Show Gist options
  • Save MercuryRising/3876558 to your computer and use it in GitHub Desktop.
Save MercuryRising/3876558 to your computer and use it in GitHub Desktop.
Flask Redis Cache
import redis
import time
def cache_or_get(fp, expiry=300, r=redis.Redis(db=5)):
'''
Input fp: file to cache or get
Input expiry: time to expire for file (default 300 seconds)
Input r: redis instance (default new instance with db set to 4)
Usage:
Wherever you have a render_template(), replace it with a
render_from_string(get_or_cache(FilePath))
You may need to prefix the templates/ to your html files.
'''
fpKey = "cached:"+fp
if r.exists(fpKey):
return r.get(fpKey)
else:
with open(fp, "rb") as f:
data = f.read()
p = r.pipeline()
p.set(fpKey, data)
p.expire(fpKey, expiry)
p.execute()
return data
def stressTest(fp="templates/index.html", trials = 10000):
'''
Input fp: File path of cached files
Input trials: number of runs
'''
# Read the file x number of times
a = time.time()
for x in range(trials):
with open(fp, "rb") as f:
data = f.readlines()
b = time.time()
readAvg = trials/(b-a)
# Read the file, cache it, read it with a new instance each time
a = time.time()
for x in range(trials):
data = cache_or_get(fp)
b = time.time()
cachedAvg = trials/(b-a)
# Read file, cache it, pass in redis instance each time
a = time.time()
r = redis.Redis(db=6)
for x in range(trials):
data = cache_or_get(fp, r=r)
b = time.time()
inCachedAvg = trials/(b-a)
print "Average time for reading file %s times: %.2f ms" %(trials, readAvg)
print "Average time for cached file with new instance %s times: %.2f ms" %(trials, cachedAvg)
print "Average time for reading file with set instance %s times: %.2f ms" %(trials, inCachedAvg)
print "New Instance Caching yields a %.1f percent increase in access time" %(100*cachedAvg/(cachedAvg+readAvg))
print "Persistent Instance Caching yields a %.1f percent increase in access time" %(100*inCachedAvg/(inCachedAvg+readAvg))
if __name__ == "__main__":
stressTest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment