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.

Keith Smiley
  • 61,481
  • 12
  • 97
  • 110

3 Answers3

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

Dipak
  • 2,263
  • 1
  • 21
  • 27
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
Vinu David Jose
  • 2,569
  • 1
  • 21
  • 38
  • 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. – builder-7000 May 18 '18 at 16:02
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)
Mr.SwiftOak
  • 1,469
  • 3
  • 8
  • 19