-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use django cache with large-image (#32)
* Use django cache with large-image * Linting * Remove print statements * Remove rest caching * Add test to validate large-image uses cache * Handle improper django configuration * Update README * Bump large image
- Loading branch information
1 parent
e39d46c
commit 5b51444
Showing
10 changed files
with
128 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import threading | ||
|
||
from django.conf import settings | ||
from django.core.cache import caches | ||
from django.core.exceptions import ImproperlyConfigured | ||
from large_image.cache_util.base import BaseCache | ||
from large_image.exceptions import TileCacheConfigurationError | ||
|
||
|
||
class DjangoCache(BaseCache): | ||
"""Use Django cache as the backing cache for large-image.""" | ||
|
||
def __init__(self, cache, getsizeof=None): | ||
super().__init__(0, getsizeof=getsizeof) | ||
self._django_cache = cache | ||
|
||
def __repr__(self): # pragma: no cover | ||
return f'DjangoCache<{repr(self._django_cache._alias)}>' | ||
|
||
def __iter__(self): # pragma: no cover | ||
# return invalid iter | ||
return None | ||
|
||
def __len__(self): # pragma: no cover | ||
# return invalid length | ||
return -1 | ||
|
||
def __contains__(self, key): | ||
hashed_key = self._hashKey(key) | ||
return self._django_cache.__contains__(hashed_key) | ||
|
||
def __delitem__(self, key): | ||
hashed_key = self._hashKey(key) | ||
return self._django_cache.delete(hashed_key) | ||
|
||
def __getitem__(self, key): | ||
hashed_key = self._hashKey(key) | ||
value = self._django_cache.get(hashed_key) | ||
if value is None: | ||
return self.__missing__(key) | ||
return value | ||
|
||
def __setitem__(self, key, value): | ||
hashed_key = self._hashKey(key) | ||
# TODO: do we want to use `add` instead to add a key only if it doesn’t already exist | ||
return self._django_cache.set(hashed_key, value) | ||
|
||
@property | ||
def curritems(self): # pragma: no cover | ||
raise NotImplementedError | ||
|
||
@property | ||
def currsize(self): # pragma: no cover | ||
raise NotImplementedError | ||
|
||
@property | ||
def maxsize(self): # pragma: no cover | ||
raise NotImplementedError | ||
|
||
def clear(self): | ||
self._django_cache.clear() | ||
|
||
@staticmethod | ||
def getCache(): # noqa: N802 | ||
try: | ||
name = getattr(settings, 'LARGE_IMAGE_CACHE_NAME', 'default') | ||
dajngo_cache = caches[name] | ||
except ImproperlyConfigured: | ||
raise TileCacheConfigurationError | ||
cache_lock = threading.Lock() | ||
cache = DjangoCache(dajngo_cache) | ||
return cache, cache_lock |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from large_image.cache_util.base import BaseCache | ||
import pytest | ||
|
||
from django_large_image import tilesource | ||
from django_large_image.cache import DjangoCache | ||
|
||
|
||
@pytest.fixture | ||
def cache_miss_counter(): | ||
class Counter: | ||
def __init__(self): | ||
self.count = 0 | ||
|
||
def reset(self): | ||
self.count = 0 | ||
|
||
counter = Counter() | ||
|
||
def missing(*args, **kwargs): | ||
counter.count += 1 | ||
BaseCache.__missing__(*args, **kwargs) | ||
|
||
original = DjangoCache.__missing__ | ||
DjangoCache.__missing__ = missing | ||
yield counter | ||
DjangoCache.__missing__ = original | ||
|
||
|
||
def test_tile(geotiff_path, cache_miss_counter): | ||
source = tilesource.get_tilesource_from_path(geotiff_path) | ||
cache_miss_counter.reset() | ||
# Check size of cache | ||
_ = source.getTile(0, 0, 0, encoding='PNG') | ||
assert cache_miss_counter.count == 1 | ||
_ = source.getTile(0, 0, 0, encoding='PNG') | ||
assert cache_miss_counter.count == 1 |
This file contains 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
This file contains 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