-
Notifications
You must be signed in to change notification settings - Fork 8
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 #90 from benjeffery/caching
Add disk caching
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 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,39 @@ | ||
import functools | ||
import pathlib | ||
|
||
import appdirs | ||
import daiquiri | ||
import diskcache | ||
|
||
logger = daiquiri.getLogger("cache") | ||
|
||
|
||
def get_cache_dir(): | ||
cache_dir = pathlib.Path(appdirs.user_cache_dir("tsqc", "tsqc")) | ||
cache_dir.mkdir(exist_ok=True, parents=True) | ||
return cache_dir | ||
|
||
|
||
cache = diskcache.Cache(get_cache_dir()) | ||
|
||
|
||
def disk_cache(version): | ||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(self, *args, **kwargs): | ||
uuid = self.file_uuid | ||
if uuid is None: | ||
logger.info(f"No uuid, not caching {func.__name__}") | ||
return func(self, *args, **kwargs) | ||
key = f"{self.file_uuid}-{func.__name__}-{version}" | ||
if key in cache: | ||
logger.info(f"Fetching {key} from cache") | ||
return cache[key] | ||
logger.info(f"Calculating {key} and caching") | ||
result = func(self, *args, **kwargs) | ||
cache[key] = result | ||
return result | ||
|
||
return wrapper | ||
|
||
return decorator |
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
click | ||
daiquiri | ||
panel | ||
diskcache | ||
hvplot | ||
xarray | ||
datashader | ||
tskit | ||
seaborn | ||
pre-commit | ||
pytest | ||
tszip | ||
appdirs |
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