PrefetchCache: extract and re-use LRUCache from SnapshotCache
#1469
+101
−65
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
First, extract the
LRUCache(named after the "least recently used"caching policy) class from the
SnapshotCache. The implementationis largely unchanged, with the following exceptions:
toCacheKeyargument to coerce key valuesthis.snapshotproperty to a more genericthis.entriessnapshotarguments toentrylocationarguments tokeyNext, implement
SnapshotCachein terms of theLRUCache. Since itserved as the original implementation, it is mostly an empty class that
passes the
toCacheKeyutility function to the constructor, andprovides a
snapshotsproperty alias that returns thethis.entriesproperty.
Also implement the
PrefetchCacheas an extension of theLRUCache.Rather than storing a reference to the "hot" entry, construct a cache of
size 1. When a new entry is put into the cache, the old entry is
expired. To supplement the "least recently used" purge strategy, also
declare a setInterval function to purge expired entries every
50ms.Finally, change all
PrefetchCache.getcall sites to pass the URLinstance directly, rather than the
String. Once passed as theargument, the
PrefetchCache.getimplementation uses the existingtoCacheKeyutility function already used by theSnapshotCache.Rename any
PrefetchCache.setLatercall sites to instead invokePrefetchCache.putLater(named to match theSnapshotCacheandLRUCache"put" method).Why
By adjusting the underlying
PrefetchCacheimplementation to expandupon an existing concept, it opens up possibilities for further
customization and configuration. Future changes could support
client-provided cache size and purge interval configuration to adjust
link prefetching behavior intervals to suit application-specific.