Skip to content

Add in-memory cache to store version listings #3163

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

Merged
merged 1 commit into from
Oct 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions lib/src/source/hosted.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ class BoundHostedSource extends CachedSource {
return result;
}

/// An in-memory cache to store the cached version listing loaded from
/// [_versionListingCachePath].
///
/// Invariant: Entries in this cache are the parsed version of the exact same
/// information cached on disk. I.e. if the entry is present in this cache,
/// there will not be a newer version on disk.
final Map<PackageRef, Pair<DateTime, Map<PackageId, _VersionInfo>>>
_responseCache = {};

/// If a cached version listing response for [ref] exists on disk and is less
/// than [maxAge] old it is parsed and returned.
///
Expand All @@ -351,26 +360,35 @@ class BoundHostedSource extends CachedSource {
Future<Map<PackageId, _VersionInfo>> _cachedVersionListingResponse(
PackageRef ref,
{Duration maxAge}) async {
if (_responseCache.containsKey(ref)) {
final cacheAge = DateTime.now().difference(_responseCache[ref].first);
if (maxAge == null || maxAge > cacheAge) {
// The cached value is not too old.
return _responseCache[ref].last;
}
}
final cachePath = _versionListingCachePath(ref);
final stat = io.File(cachePath).statSync();
final now = DateTime.now();
if (stat.type == io.FileSystemEntityType.file) {
if (maxAge == null || now.difference(stat.modified) < maxAge) {
try {
final cachedDoc = jsonDecode(await readTextFileAsync(cachePath));
final cachedDoc = jsonDecode(readTextFile(cachePath));
final timestamp = cachedDoc['_fetchedAt'];
if (timestamp is String) {
final cacheAge =
DateTime.now().difference(DateTime.parse(timestamp));
final parsedTimestamp = DateTime.parse(timestamp);
final cacheAge = DateTime.now().difference(parsedTimestamp);
if (maxAge != null && cacheAge > maxAge) {
// Too old according to internal timestamp - delete.
tryDeleteEntry(cachePath);
} else {
return _versionInfoFromPackageListing(
var res = _versionInfoFromPackageListing(
cachedDoc,
ref,
Uri.file(cachePath),
);
_responseCache[ref] = Pair(parsedTimestamp, res);
return res;
}
}
} on io.IOException {
Expand Down Expand Up @@ -402,6 +420,9 @@ class BoundHostedSource extends CachedSource {
},
),
);
// Delete the entry in the in-memory cache to maintain the invariant that
// cached information in memory is the same as that on the disk.
_responseCache.remove(ref);
} on io.IOException catch (e) {
// Not being able to write this cache is not fatal. Just move on...
log.fine('Failed writing cache file. $e');
Expand Down