No way to clear the cache
Description
We are experiencing an issue where the cache seems to persist even after deleting the cache storage directories. This issue occurs when using the HttpCache plugin in Ktor. Despite deleting the cache directories and confirming that they are empty, cached responses are still being returned.
We need a way to clear the private storage cache, including any in-memory cache, when the user logs out in our Android app. This should ensure that no cached responses are returned after the cache has been cleared.
Steps to Reproduce
- Set up
HttpCachein Ktor with both public and private storage. - Make a request that gets cached.
- Delete the cache directories.
- Make the same request again.
Expected Behavior
The request should not return a cached response after the cache storage directories have been deleted.
Actual Behavior
The request still returns a cached response even after the cache storage directories have been deleted.
Additional Context
- The
HttpCacheplugin might be using an in-memory cache in addition to the file storage. - Tested with Ktor 3.0.3 in our Android app.
Can you share your configuration of the HttpCache plugin?
I'd like to add one more voice to this request: while I could trust our backend to either not send caching headers if there's anything that is dependent on the user token being present or not (most likely using Vary headers), I really don't trust it to do it consistently.
HttpCache plugin is setup with a regular FileStorage, as shown in the ktor documentation:
val client = HttpClient(OkHttp) {
install(HttpCache) {
val cacheFile = (context.cacheDir.toPath() / "ktor-cache").toFile()
publicStorage(FileStorage(cacheFile))
}
}
When removing the user token, I already perform a cleanup of the auth token on the Auth plugin (yes, this is using the old API, but it still works with the latest ktor when adapting with the changes):
client.plugin(Auth).providers.filterIsInstance<BearerAuthProvider>().first()
.clearToken()
I'd love to be able to clear the cache at the same time, to ensure no stale data would stay. Having to re-query isn't too much of a problem, but having stale data that might only be available when logged in is definitely a problem.
As it stands, CachingCacheStorage (which gets instantiated when calling FileStorage) does two things: an in-memory cache, and a file cache. While I could manually clear the cache directory, nothing allows me to cleanup the CachingCacheStorage, which has been warmed up by then. I don't believe clearing the OkHttp cache would be enough either, as Ktor's own caching is fully separated from it.