From 82fba88cc4131f1b2d67c9ac5b56793d634a48e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Sat, 14 Oct 2023 00:42:27 +0000 Subject: [PATCH] storage: set disk cache expiry dates for images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds expiry dates for images added to the Kingfisher cache. The expiry date depends on the context of the image: - Images from notes expire after a week - Images from profile banners expire after two weeks - Profile pictures never expire. Test ---- Device: iPhone 14 Pro (Simulator), iOS: 17.0 Special remarks: Requires minor local mods and debugger connection Steps: 1. Locally change the note image expiry to 5 seconds 2. Set a breakpoint in `removeExpiredValues` function in `DiskStorage.swift` in Kingfisher 3. Disable breakpoints for now 4. Start Damus and go to the profile feed of someone new 5. Scroll down through the images for about a minute 6. Turn on breakpoints 7. Switch to a different app in the simulator (Make Damus go to background mode) 8. Wait for a few seconds. Debugger should hit the breakpoint set. PASS 9. Take note of the fileURLs of the images being deleted 10. Go to that directory where the fileURLs are in via Finder 11. Look at some of the images being deleted. Perhaps save a copy for comparison. 12. Turn off breakpoints, resume execution and go back to Damus 13. Scroll back up. Some images there should match the images being automatically deleted from the cache. PASS Closes: https://github.com/damus-io/damus/issues/1565 Changelog-Added: Add expiry date for images in cache to be auto-deleted after a preset time to save space on storage Signed-off-by: Daniel D’Aquino Reviewed-by: William Casarin Signed-off-by: William Casarin --- damus/Util/Extensions/KFOptionSetter+.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/damus/Util/Extensions/KFOptionSetter+.swift b/damus/Util/Extensions/KFOptionSetter+.swift index e6751448..264fa77a 100644 --- a/damus/Util/Extensions/KFOptionSetter+.swift +++ b/damus/Util/Extensions/KFOptionSetter+.swift @@ -28,6 +28,18 @@ extension KFOptionSetter { options.scaleFactor = UIScreen.main.scale options.onlyLoadFirstFrame = disable_animation + switch imageContext { + case .pfp: + options.diskCacheExpiration = .never + break + case .banner: + options.diskCacheExpiration = .days(14) + break + case .note: + options.diskCacheExpiration = .days(7) + break + } + return self }