fix: DiskCache served a cache miss for entries that were still fresh - #26009
Open
jkmassel wants to merge 1 commit into
Open
fix: DiskCache served a cache miss for entries that were still fresh#26009jkmassel wants to merge 1 commit into
jkmassel wants to merge 1 commit into
Conversation
read(_:forKey:notOlderThan:) compared the entry's expiry against now and returned nil when the expiry was still in the future, so it discarded every entry inside the requested window and served only entries that had already aged past it. Also reads the creation date off the URL instead of a percent-encoded path string, matching the fileExists(at:) check two lines above, and takes the cache root as an init parameter so the tests don't touch the real caches directory.
Contributor
|
| App Name | WordPress | |
| Configuration | Release-Alpha | |
| Build Number | 34433 | |
| Version | PR #26009 | |
| Bundle ID | org.wordpress.alpha | |
| Commit | 90870bd | |
| Installation URL | 6jjlttnf25bng |
Contributor
|
| App Name | Jetpack | |
| Configuration | Release-Alpha | |
| Build Number | 34433 | |
| Version | PR #26009 | |
| Bundle ID | com.jetpack.alpha | |
| Commit | 90870bd | |
| Installation URL | 61vm5girviet0 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


Fixes a bug where
DiskCache.read(_:forKey:notOlderThan:)returned a cache miss for every entry inside the requested window, and a hit only for entries that had already aged past it.Found while sweeping for the
URL.path()shape from #26005. Unrelated to that bug, so it's on its own.Summary
notOlderThan:today, and every cache key in the app is an ASCII slug.Root Cause
Modules/Sources/WordPressCore/DiskCache.swift
creationDate + intervalis when the entry expires. The condition holds while that moment is still in the future — that is, while the entry is valid — and the branch returnsnil. The comparison needs to be<.The second issue is in the line above it:
path(forKey:)iscacheRoot.appendingPathComponent("\(key).cache.json")with the key unhashed, so a key containing a space or a%would pass the first check and throw on the second.Fix
1. Compare against the expiry correctly
<instead of>, with a comment naming what the branch means.2. Read the creation date off the URL
path.resourceValues(forKeys: [.creationDateKey])instead ofattributesOfItem(atPath: path.path()). This matches thefileExists(at:)call above it and removes the encoding question rather than restating it.3. Take the cache root as an init parameter
public init(cacheRoot: URL = .cachesDirectory). Defaulted, soDiskCache.shared,DiskCache()inAccountHelper, andCachedAndFetchedResultare unchanged. Without it the tests would read, write, andremoveAll()in the developer's real caches directory.Test plan
DiskCacheTests— 8 cases covering store/read round-trip, a fresh entry inside the window, an expired entry, no interval, a missing key, a key needing percent-encoding,remove(key:), andremoveAll().swift test --filter DiskCacheTests— 8/8 pass on macOS.Each test writes to its own
UUID-named directory undertemporaryDirectoryand removes it afterwards.