Nikolay Shevyakov
Nikolay Shevyakov

Reputation: 55

SDWebImage change cacheMaxCacheAge

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

Upvotes: 4

Views: 1944

Answers (3)

Mr.SwiftOak
Mr.SwiftOak

Reputation: 1844

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)

Upvotes: 0

Dipak
Dipak

Reputation: 2433

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

Upvotes: 8

Vinu David Jose
Vinu David Jose

Reputation: 2638

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

Upvotes: 1

Related Questions