Last active
August 26, 2016 13:52
-
-
Save drjova/183d956db19060a5fa1c to your computer and use it in GitHub Desktop.
Flask-IIIF custom cache example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In your overlay's configuration (/s/overlay/YOUR_OVERLAY) | |
IIIF_CACHE_HANDLER = 'overlay.utils:FileSystemCache' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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