Skip to content

Commit

Permalink
openaire: fix image cache and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiaraBi authored and Glignos committed May 20, 2020
1 parent 205dc16 commit 60d4c78
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 73 deletions.
10 changes: 6 additions & 4 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@
from zenodo.modules.deposit.scopes import extra_formats_scope
from zenodo.modules.fixtures.records import loadsipmetadatatypes
from zenodo.modules.github.cli import github
from zenodo.modules.iiif.cache import FilteredImageRedisCache
from zenodo.modules.records.api import ZenodoRecord
from zenodo.modules.records.models import AccessRight
from zenodo.modules.records.serializers.bibtex import Bibtex
from zenodo.modules.thumbnails.cache import ImageRedisCache


def wrap_rate_limit():
Expand Down Expand Up @@ -1367,10 +1367,12 @@ def mock_datacite_minting(mocker, app):
app.config['DEPOSIT_DATACITE_MINTING_ENABLED'] = orig


def iiif_cache():
@pytest.fixture
def iiif_cache(app):
"""Fixture for iiif chache."""
cache = ImageRedisCache()
cache = app.extensions['iiif'].cache()
yield cache
iiif_keys = [k for k in cache.cache._client.keys() if k.find("iiif:") != -1]
iiif_keys = [k.decode('utf-8') for k in cache.cache._client.keys()
if k.decode('utf-8').startswith(u"iiif:")]
for key in iiif_keys:
cache.delete(key)
4 changes: 2 additions & 2 deletions tests/unit/iiif/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

def test_thumbnail_caching(app, iiif_cache):
"""Test thumbnail cache."""
key_250 = 'iiif:identifier1/full/250,/0/default.png'
key = 'iiif:identifier2/full/260,/0/default.jpg'
key_250 = 'iiif:identifier1/full/250,/default/0.png'
key = 'iiif:identifier2/full/260,/default/0.jpg'
value = 'value'

# only images with size == (250,) are cached
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/iiif/test_iiif_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@

from __future__ import absolute_import, print_function

from flask import current_app
from invenio_cache import current_cache
from invenio_iiif.utils import iiif_image_key
from invenio_indexer.api import RecordIndexer
from invenio_search import current_search
from pkg_resources import resource_stream
from werkzeug import LocalProxy

from zenodo.modules.iiif.tasks import preprocess_thumbnails

Expand All @@ -49,7 +47,8 @@ def test_preprocess_thumbnails(app, db, es, record_with_bucket, iiif_cache):
RecordIndexer().index(record)
current_search.flush_and_refresh(index='records')
preprocess_thumbnails('zenodo')
key = 'iiif:'+str(record.files['test.png'].obj)+'/full/250,/default/0.png'
key = 'iiif:' + iiif_image_key(record.files['test.png'].obj) + \
'/full/250,/default/0.png'
assert iiif_cache.get(key)
iiif_cache.delete(key)
preprocess_thumbnails('zenodo')
Expand Down
3 changes: 1 addition & 2 deletions zenodo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,6 @@ def _(x):
#: Use the Redis storage backend for caching IIIF images
# TODO: Fix Python 3 caching key issue to enable:
# https://github.com/inveniosoftware/flask-iiif/issues/66
# IIIF_CACHE_HANDLER = 'flask_iiif.cache.redis:ImageRedisCache'

# Redis storage for thumbnails caching.
IIIF_CACHE_REDIS_URL = CACHE_REDIS_URL
Expand All @@ -1100,7 +1099,7 @@ def _(x):
}

#: IIIF Cache handler
IIIF_CACHE_HANDLER = 'zenodo.modules.thumbnails.cache:ImageRedisCache'
IIIF_CACHE_HANDLER = 'zenodo.modules.iiif.cache:FilteredImageRedisCache'

#: Cache duration
# 60 seconds * 60 minutes (1 hour) * 24 (24 hours) * 2 (2 days) = 172800 secs
Expand Down
15 changes: 8 additions & 7 deletions zenodo/modules/iiif/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@

from __future__ import absolute_import

from flask_iiif.cache.redis import ImageRedisCache as ImageCache
from flask_iiif.cache.redis import ImageRedisCache


class ImageRedisCache(ImageCache):
class FilteredImageRedisCache(ImageRedisCache):
"""Redis image cache."""

def __init__(self):
"""Initialize the cache."""
super(ImageRedisCache, self).__init__()
super(FilteredImageRedisCache, self).__init__()

def set(self, key, value, timeout=None):
"""Cache the object.
Expand All @@ -44,7 +44,8 @@ def set(self, key, value, timeout=None):
:type value: `BytesIO` object
:param timeout: the cache timeout in seconds
"""
identifier, _, size, _, _ = key.split('/')
if size == '250,':
timeout = timeout if timeout else self.timeout
self.cache.set(key, value, timeout=timeout)
identifier, region, size, quality, rotation_format = key.split('/')
if size == '250,' and region == 'full' and quality == 'default' and \
rotation_format.startswith(u'0.'):
timeout = timeout if timeout else self.timeout
self.cache.set(key, value, timeout=timeout)
2 changes: 0 additions & 2 deletions zenodo/modules/iiif/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@

import arrow
from celery import shared_task
from flask import current_app
from flask_iiif.restful import IIIFImageAPI
from invenio_cache import current_cache
from invenio_iiif.utils import iiif_image_key
from invenio_indexer.api import RecordIndexer
from invenio_search.api import RecordsSearch


Expand Down
52 changes: 0 additions & 52 deletions zenodo/modules/thumbnails/cache.py

This file was deleted.

0 comments on commit 60d4c78

Please sign in to comment.