mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-31 18:44:27 +02:00
Cache DL requests
This commit is contained in:
@@ -12,12 +12,14 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import json
|
||||||
import time
|
import time
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
from platformio import __registry_mirror_hosts__
|
from platformio import __registry_mirror_hosts__
|
||||||
|
from platformio.cache import ContentCache
|
||||||
from platformio.clients.http import HTTPClient
|
from platformio.clients.http import HTTPClient
|
||||||
from platformio.clients.registry import RegistryClient
|
from platformio.clients.registry import RegistryClient
|
||||||
from platformio.package.exception import UnknownPackageError
|
from platformio.package.exception import UnknownPackageError
|
||||||
@@ -39,6 +41,21 @@ class RegistryFileMirrorIterator(object):
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
|
cache_key = ContentCache.key_from_args(
|
||||||
|
"head", self.download_url, self._visited_mirrors
|
||||||
|
)
|
||||||
|
with ContentCache("http") as cc:
|
||||||
|
result = cc.get(cache_key)
|
||||||
|
if result is not None:
|
||||||
|
try:
|
||||||
|
headers = json.loads(result)
|
||||||
|
return (
|
||||||
|
headers["Location"],
|
||||||
|
headers["X-PIO-Content-SHA256"],
|
||||||
|
)
|
||||||
|
except (ValueError, KeyError):
|
||||||
|
pass
|
||||||
|
|
||||||
http = self.get_http_client()
|
http = self.get_http_client()
|
||||||
response = http.send_request(
|
response = http.send_request(
|
||||||
"head",
|
"head",
|
||||||
@@ -58,6 +75,18 @@ class RegistryFileMirrorIterator(object):
|
|||||||
if any(stop_conditions):
|
if any(stop_conditions):
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
self._visited_mirrors.append(response.headers.get("X-PIO-Mirror"))
|
self._visited_mirrors.append(response.headers.get("X-PIO-Mirror"))
|
||||||
|
cc.set(
|
||||||
|
cache_key,
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"Location": response.headers.get("Location"),
|
||||||
|
"X-PIO-Content-SHA256": response.headers.get(
|
||||||
|
"X-PIO-Content-SHA256"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
"1h",
|
||||||
|
)
|
||||||
return (
|
return (
|
||||||
response.headers.get("Location"),
|
response.headers.get("Location"),
|
||||||
response.headers.get("X-PIO-Content-SHA256"),
|
response.headers.get("X-PIO-Content-SHA256"),
|
||||||
|
Reference in New Issue
Block a user