Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching for provider metadata #600

Merged
merged 5 commits into from
Sep 15, 2020
Merged

Conversation

jimmylewis
Copy link
Contributor

This affects the scenarios where we need to fetch the latest library versions or the file lists for a given library. For Cdnjs, because they offer a full catalog, it also affects that.

When fetching versions (or catalog), we want to have live data if possible, as new versions (or packages) can be released. These operations will initially attempt a web request, and if it fails, fall back to the cached contents on disk (if any). Note that there is now a 1-day cache lifetime applied to the initial request, which should cut down on repetitive traffic.

When fetching file lists, we assume that released libraries are static. In this case, we first check for the information on disk, and then fallback to a web request if it's missing. There is no lifetime applied in this case. If the library is mutated, a user will have to clear the cache to get the updated metadata for the library (but, they'd have to do the same to get updated versions of any cached library assets as well).

This resolves #598, which should in turn help with some of the reliability issues reported in #370.

Currently we download these files and cache them locally with a hardcoded lifetime.  Once the file is expired, we attempt to download a new copy; however, if that request fails, we just fail the operation.  With this change, we'll at least fall back to the already-stored copy, which may be stale but should still work in most cases.
@jimmylewis jimmylewis requested a review from tlmii September 14, 2020 18:04
Copy link
Contributor

@tlmii tlmii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Along with the couple things Rob pointed out already, looks good to me!

This change reduces the repentitiveness of implementing request-and-cache or check-cache-else-request patterns when requesting provider metadata.  This simplified code in each provider, and also centralized testing that behavior on the CacheService class rather than duplicating cache behavior testing.

It also removes the GetCatalogAsync and GetMetadataAsync as those names were Cdnjs-specifc, and the catalog is just another form of metadata.

Testing was also largely refactored to apply fakes at the ICacheService layer instead of the underlying IWebRequestHandler, as that should be transparent to the Catalog implementations.  There are a couple exceptions to verify that no web requests are issued at all during some test scenarios.
- Add ConfigureAwait(false) to the files changed in this PR
- Remove unnecessary using in UnpkgCatalogTest
- Remove unused sample string CdnjsProviderTest
@jimmylewis jimmylewis merged commit c44defa into aspnet:master Sep 15, 2020
@jimmylewis jimmylewis deleted the cacheMetadata branch September 15, 2020 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request: Add metadata cache for JSDelivr and Unpkg providers
3 participants