Skip to content

Instantly share code, notes, and snippets.

@drjova
Last active August 26, 2016 13:52
Show Gist options
  • Save drjova/183d956db19060a5fa1c to your computer and use it in GitHub Desktop.
Save drjova/183d956db19060a5fa1c to your computer and use it in GitHub Desktop.
Flask-IIIF custom cache example
# In your overlay's configuration (/s/overlay/YOUR_OVERLAY)
IIIF_CACHE_HANDLER = 'overlay.utils:FileSystemCache'
import os
from flask_iiif.cache import ImageCache
class FileSystemCache(ImageCache):
"""File system cache."""
def __init__(self):
"""Initialize the cache."""
super(ImageSimpleCache, self).__init__()
def get(self, key):
"""Return the key value.
:param string key: the object's key
:return: the stored object
:rtype: `BytesIO` object
"""
# Make sure to return a BytesIO object
with open(key) as image:
return image.read()
def set(self, key, value, timeout=0):
"""Cache the object.
:param string key: the object's key
:param value: the stored object
:type value: `BytesIO` object
:param int timeout: the cache timeout in seconds
"""
with open(key, "w") as image:
image.write(value)
def delete(self, key):
"""Delete the specific key."""
os.remove(key)
def flush(self):
"""Flush the cache."""
# Delete the whole cache folder
raise NotImplementedError()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment