SDWebImage change cacheMaxCacheAge
Asked Answered
T

3

4

How I can change a static value cacheMaxCacheAge when I use SDWebImage? What way is good? I need to cache images for 5 minutes.

Thallophyte answered 16/2, 2015 at 19:46 Comment(1)
[SDWebImageManager sharedManager].imageCache.maxCacheAge = 300;Englert
V
8

In SDWebImage 5.0+ maxCacheAge renamed to maxDiskAge. You can set maximum caching time in seconds like this.

Objetive-c

[SDImageCache sharedImageCache].config.maxDiskAge = 60 * 5; //5 minutes

Swift 4+

SDImageCache.shared.config.maxDiskAge = 60 * 5 //5 minutes

Reference link: https://github.com/SDWebImage/SDWebImage/wiki/5.0-Migration-guide

Vector answered 5/4, 2019 at 13:16 Comment(2)
should maxDiskSize be maxDiskAge in your Objective C example?Fidole
Thanks @Fidole for pointing out, both are different, updated my answer.Vector
D
1

SDWebImage is providing maxCacheAge property to set the maximum caching time in seconds. We can use shared instance method to access this property.

[SDImageCache sharedImageCache].config.maxCacheAge = 60 * 5; //5 minutes
Drone answered 18/5, 2018 at 13:2 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Publius
G
0

SWIFT 5+ , SDWebImage 5.13 +, tvOS15+ and maybe other platforms

I found out that simply setting new maxDiskAge property on tvOS 15+ is not enough when storing image with SDImageCache.shared.store , but you indeed need to manually delete the old cache with deleteOldFiles() function. It happens in my case, even though the performing of old cache deletion is allegedly done by library itself after terminating the app as specified in following SO post. This might be only the case for tvOS, but I am not entirely sure if the problem persists also on another platforms. Here is the solution that worked for me:

Deleting expired cache:

SDImageCache.shared.deleteOldFiles()

Setting max cache disk time in seconds:

SDImageCache.shared.config.maxDiskAge = 100000

Storing images to disk:

SDImageCache.shared.store(image, forKey: key, toDisk: true)
Graveyard answered 6/9, 2022 at 11:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.