Skip to content

Commit

Permalink
Fix getCache when improperly configured
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan committed Jun 16, 2022
1 parent 9850cbe commit 83dc6f1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
35 changes: 26 additions & 9 deletions large_image/cache_util/cachefactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from importlib_metadata import entry_points

from .. import config
from ..exceptions import TileCacheError

try:
from .memcache import MemCache
Expand Down Expand Up @@ -100,6 +101,26 @@ def pickAvailableCache(sizeEach, portion=8, maxItems=None, cacheName=None):
return numItems


def getFirstAvailableCache():
cacheBackend = config.getConfig('cache_backend', None)
if cacheBackend is not None:
raise ValueError('cache_backend already set')
loadCaches()
cache, cacheLock = None, None
for cacheBackend in _availableCaches:
try:
cache, cacheLock = _availableCaches[cacheBackend].getCache()
break
except TileCacheError:
continue
if cache is not None:
config.getConfig('logprint').info(
f'Automatically setting `{cacheBackend}` as cache_backend from availableCaches'
)
config.setConfig('cache_backend', cacheBackend)
return cache, cacheLock


class CacheFactory:
logged = False

Expand Down Expand Up @@ -130,20 +151,16 @@ def getCache(self, numItems=None, cacheName=None, inProcess=False):
# Default to `python` cache for inProcess
cacheBackend = config.getConfig('cache_backend', 'python' if inProcess else None)

# set the config cache_backend if caches available and not set
if cacheBackend is None and len(_availableCaches):
cacheBackend = next(iter(_availableCaches))
config.getConfig('logprint').info(
f'Automatically setting `{cacheBackend}` as cache_backend from availableCaches'
)
config.setConfig('cache_backend', cacheBackend)

if isinstance(cacheBackend, str):
cacheBackend = cacheBackend.lower()

cache = None
if not inProcess and cacheBackend in _availableCaches:
cache, cacheLock = _availableCaches[cacheBackend].getCache()
else: # fallback backend or inProcess
elif not inProcess and cacheBackend is None:
cache, cacheLock = getFirstAvailableCache()

if cache is None: # fallback backend or inProcess
cacheBackend = 'python'
cache = cachetools.LRUCache(self.getCacheSize(numItems, cacheName=cacheName))
cacheLock = threading.Lock()
Expand Down
8 changes: 8 additions & 0 deletions large_image/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def __init__(self, *args, **kwargs):
return super().__init__(errno.ENOENT, *args, **kwargs)


class TileCacheError(TileGeneralError):
pass


class TileCacheConfigurationError(TileCacheError):
pass


TileGeneralException = TileGeneralError
TileSourceException = TileSourceError
TileSourceAssetstoreException = TileSourceAssetstoreError

0 comments on commit 83dc6f1

Please sign in to comment.