evermeer / EVURLCache

a NSURLCache subclass for handling all web requests that use NSURLRequest
Other
297 stars 49 forks source link

Cleaning expired caches #33

Closed ChristRm closed 8 years ago

ChristRm commented 8 years ago

When my cached data expires it is not cleaned. Is this is correct behaviour.

evermeer commented 8 years ago

When a new request for that cache item comes in, the dat of the item in the cache is checked. If it's too old it's ignored. The item is not removed from the cache. The item will not be thrown away because it will still be returned if there is no internet connection.

ChristRm commented 8 years ago

What is the best approach for cleaning cache? My app uses EVURLCach for caching web pages and I want to implement cleaning all pages that was cached earlier than for example 1-2 weeks. Is it right to remove all files from cache directory with creation date earlier than some date?

evermeer commented 8 years ago

That would be a good approach. You could see the function cacheItemExpired in EVURLCache how you could implement something like that. You only have to add a mechanism to scan all files in a folder plus it's sub folders and set a default maxAge (in seconds)

evermeer commented 8 years ago

Here is a short initial setup that you could use. If you have it working, then i think it would be nice if you could create a pull request for that.

open static func clearCacheOderThan(seconds: Double) {
// loop through all folders, subfolders, files and set storagePath
do {
            let attributes = try FileManager.default.attributesOfItem(atPath: storagePath)
            if let modDate: Date = attributes[FileAttributeKey.modificationDate] as? Date {
                    let modificationTimeSinceNow: TimeInterval? = -modDate.timeIntervalSinceNow
                    if  modificationTimeSinceNow > seconds {
                         // remove file
                    }
            }
        } catch {}
}
ChristRm commented 8 years ago

Thanks. I'll modify this code and after testing on my app I'll create pull request.

ChristRm commented 8 years ago

My cleaner is ready, can you give me access to make commit and pull request?

evermeer commented 8 years ago

Everybody can do a pull request. If you did a fork, then you can just do a pull request for that.