-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add http caching mechanism (#309)
This PR aims to improve plugin's performance regarding HTTP requests which are performed to retrieve remote image length: - add a new option `cache_dir` - rely on https://pypi.org/project/CacheControl/ to manage local cache requests For now, it works only for GET requests. See upstream issue: psf/cachecontrol#337
- Loading branch information
Showing
8 changed files
with
99 additions
and
9 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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import http.client | ||
import logging | ||
from pathlib import Path | ||
|
||
import requests | ||
from cachecontrol import CacheControl | ||
from cachecontrol.caches.file_cache import FileCache | ||
|
||
http.client.HTTPConnection.debuglevel = 1 | ||
logging.basicConfig() | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
req_log = logging.getLogger("requests.packages.urllib3") | ||
req_log.setLevel(logging.DEBUG) | ||
req_log.propagate = True | ||
|
||
|
||
sess = CacheControl( | ||
requests.Session(), cache=FileCache(".web_cache"), cacheable_methods=("HEAD", "GET") | ||
) | ||
|
||
|
||
# get requests | ||
resp = sess.get("https://geotribu.fr") | ||
resp_img = sess.get( | ||
"https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/kevish_Air-Traffic.png" | ||
) | ||
|
||
# try again, cache hit expected | ||
resp = sess.get("https://geotribu.fr") | ||
resp_img = sess.get( | ||
"https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/kevish_Air-Traffic.png" | ||
) | ||
|
||
# head requests | ||
resp_img = sess.head( | ||
"https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/kevish_Air-Traffic.png" | ||
) | ||
|
||
|
||
# try again, cache hit expected | ||
resp_img = sess.head( | ||
"https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/kevish_Air-Traffic.png" | ||
) | ||
|
||
print(list(Path(".web_cache").iterdir())) |
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