forked from earthobservations/wetterdienst
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 earthobservations#169 from panodata/improve-caching
Add TTL-based persistent caching using dogpile.cache
- Loading branch information
Showing
9 changed files
with
124 additions
and
46 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
import os | ||
import logging | ||
import platform | ||
|
||
import appdirs | ||
from dogpile.cache import make_region | ||
|
||
|
||
log = logging.getLogger() | ||
|
||
# Python on Windows has no "fcntl", which is required by the dbm backend. | ||
# TODO: Make backend configurable, e.g. better use Redis. | ||
platform = platform.system() | ||
backend = "dogpile.cache.dbm" | ||
if platform == "Windows": | ||
backend = "dogpile.cache.memory" | ||
|
||
# Compute cache directory. | ||
cache_dir = appdirs.user_cache_dir(appname="wetterdienst") | ||
if not os.path.exists(cache_dir): | ||
os.makedirs(cache_dir) | ||
log.info("Cache directory is %s", cache_dir) | ||
|
||
# Define cache regions. | ||
metaindex_cache = make_region().configure( | ||
backend, | ||
expiration_time=1 * 60, | ||
arguments={"filename": os.path.join(cache_dir, "metaindex_1min.dbm")}, | ||
) | ||
|
||
fileindex_cache_five_minutes = make_region().configure( | ||
backend, | ||
expiration_time=5 * 60, | ||
arguments={"filename": os.path.join(cache_dir, "fileindex_5min.dbm")}, | ||
) | ||
|
||
fileindex_cache_one_hour = make_region().configure( | ||
backend, | ||
expiration_time=60 * 60, | ||
arguments={"filename": os.path.join(cache_dir, "fileindex_60min.dbm")}, | ||
) | ||
|
||
payload_cache_one_hour = make_region().configure( | ||
backend, | ||
expiration_time=60 * 60, | ||
arguments={"filename": os.path.join(cache_dir, "payload_60min.dbm")}, | ||
) |
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
Oops, something went wrong.