I am currently using Flask for API development. With regards to caching, can I ask what is the best practice to decide on what to actually cache? To give some context, I tried caching an image generated by flask's send_file(<BytesIO>, minmetype="image/png", cache_timeout=0)
. To do this I used Flask Caching and configured it to use memcached which linked directly to python-memcached:
import pickle
class Image(Resource):
@... decorators
def get(self, image_id):
key = "view//image/image_id"
if cache.has(key):
data = cache.get(key)
response = pickle.loads(data)
return response
else:
image_png_str = #retrieved from database
response = send_file(io.BytesIO(base64.b64decode(image_png_str)), mimetype="image/png", cache_timeout=0)
cache.add(key, pickle.dumps(data))
return response
I observed the response time of retrieving the image and it doesnt seem like a big performance improvement. So I'm actually wondering what is the best practice of what should we actually cache? Thank you
P.S I was unable to use @cache.cached(timeout=...)
because I get
TypeError: BytesIO object cannot be pickled
- this is if my response was flask's send_file(...)