-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a disk cache mechanism to cache images
- Loading branch information
1 parent
5d8c007
commit 0330216
Showing
11 changed files
with
230 additions
and
32 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 |
---|---|---|
|
@@ -42,3 +42,4 @@ site/ | |
gh_pages/ | ||
doc/README.md | ||
doc/references/cli.md | ||
data/diskcache |
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
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 |
---|---|---|
@@ -1,3 +1,46 @@ | ||
from robotoff.utils import get_logger | ||
from typing import Callable | ||
|
||
logger = get_logger(__name__) | ||
from diskcache import Cache | ||
|
||
from robotoff import settings | ||
|
||
# Disk-cache to store any kind of content (but currently mostly images). | ||
# It avoids having to download multiple times the same image from the server, | ||
# with a reasonable disk usage (default to 1GB). | ||
# diskcache Cache is thread-safe and process-safe, and every transaction is | ||
# atomic. We can therefore define a single cache here and use it across the | ||
# project. | ||
disk_cache = Cache(settings.DISKCACHE_DIR) | ||
|
||
|
||
def cache_http_request( | ||
key: str, | ||
func: Callable, | ||
cache_expire: int | None = None, | ||
tag: str | None = None, | ||
*args, | ||
**kwargs, | ||
) -> bytes | None: | ||
"""Cache raw response (bytes) of HTTP requests. | ||
:param key: the cache key | ||
:param func: the function to call, must return a Request object | ||
:param cache_expire: expiration time of the item in the cache, defaults to | ||
None (no expiration) | ||
:param tag: a tag of the item in the cache (optional), defaults to None | ||
:return: the response bytes or None if an error occured while calling | ||
`func` | ||
""" | ||
# Check if the item is already cached, and use it instead of sending | ||
# the HTTP request if it is | ||
content_bytes = disk_cache.get(key) | ||
if content_bytes is None: | ||
r = func(*args, **kwargs) | ||
if r is None: | ||
# Don't save in cache if an error (or HTTP 404) occurred | ||
return None | ||
content_bytes = r.content | ||
# We store the raw byte content of the response in the cache | ||
disk_cache.set(key, r.content, expire=cache_expire, tag=tag) | ||
|
||
return content_bytes |
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.