-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #876 from girder/caches
Abstract caching and support entrypoints
- Loading branch information
Showing
6 changed files
with
202 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import hashlib | ||
import threading | ||
import time | ||
from typing import Tuple | ||
|
||
import cachetools | ||
|
||
|
||
class BaseCache(cachetools.Cache): | ||
"""Base interface to cachetools.Cache for use with large-image.""" | ||
|
||
def __init__(self, *args, getsizeof=None, **kwargs): | ||
super().__init__(*args, getsizeof=getsizeof, **kwargs) | ||
self.lastError = {} | ||
self.throttleErrors = 10 # seconds between logging errors | ||
|
||
def logError(self, err, func, msg): | ||
""" | ||
Log errors, but throttle them so as not to spam the logs. | ||
:param err: error to log. | ||
:param func: function to use for logging. This is something like | ||
logprint.exception or logger.error. | ||
:param msg: the message to log. | ||
""" | ||
curtime = time.time() | ||
key = (err, func) | ||
if (curtime - self.lastError.get(key, {}).get('time', 0) > self.throttleErrors): | ||
skipped = self.lastError.get(key, {}).get('skipped', 0) | ||
if skipped: | ||
msg += ' (%d similar messages)' % skipped | ||
self.lastError[key] = {'time': curtime, 'skipped': 0} | ||
func(msg) | ||
else: | ||
self.lastError[key]['skipped'] += 1 | ||
|
||
def __repr__(self): | ||
raise NotImplementedError | ||
|
||
def __iter__(self): | ||
raise NotImplementedError | ||
|
||
def __len__(self): | ||
raise NotImplementedError | ||
|
||
def __contains__(self, key): | ||
raise NotImplementedError | ||
|
||
def __delitem__(self, key): | ||
raise NotImplementedError | ||
|
||
def _hashKey(self, key): | ||
return hashlib.sha256(key.encode()).hexdigest() | ||
|
||
def __getitem__(self, key): | ||
# hashedKey = self._hashKey(key) | ||
raise NotImplementedError | ||
|
||
def __setitem__(self, key, value): | ||
# hashedKey = self._hashKey(key) | ||
raise NotImplementedError | ||
|
||
@property | ||
def curritems(self): | ||
raise NotImplementedError | ||
|
||
@property | ||
def currsize(self): | ||
raise NotImplementedError | ||
|
||
@property | ||
def maxsize(self): | ||
raise NotImplementedError | ||
|
||
def clear(self): | ||
raise NotImplementedError | ||
|
||
@staticmethod | ||
def getCache() -> Tuple['BaseCache', threading.Lock]: | ||
# return cache, cacheLock | ||
raise NotImplementedError |
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