Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rs/SDURLCache
URLCache subclass with on-disk cache support on iPhone/iPad
https://github.com/rs/SDURLCache
Last synced: about 2 months ago
JSON representation
URLCache subclass with on-disk cache support on iPhone/iPad
- Host: GitHub
- URL: https://github.com/rs/SDURLCache
- Owner: rs
- License: mit
- Created: 2010-03-16T09:16:25.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2017-04-03T04:56:56.000Z (almost 8 years ago)
- Last Synced: 2024-11-21T22:04:35.252Z (about 2 months ago)
- Language: Objective-C
- Homepage:
- Size: 57.6 KB
- Stars: 798
- Watchers: 38
- Forks: 246
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome - SDURLCache - URLCache subclass with on-disk cache support on iPhone/iPad (etc)
- awesome - SDURLCache - URLCache subclass with on-disk cache support on iPhone/iPad (etc)
README
On iPhone OS, Apple did remove on-disk cache support for unknown reason. Some will say it's to save
flash-drive life, others will arg it's to save disk capacity. As it is explained in the
NSURLCacheStoragePolicy, the NSURLCacheStorageAllowed constant is always treated as
NSURLCacheStorageAllowedInMemoryOnly and there is no way to force it back, the code is certainly
gone on this platform. For whatever reason Apple removed this feature, you may be interested by
having on-disk HTTP request caching in your application. SDURLCache gives back this feature to this
iPhone OS for you.To use it, you just have create an instance, replace the default shared NSURLCache with it and
that's it, you instantly give on-disk HTTP request caching capability to your application.SDURLCache *urlCache = [[SDURLCache alloc] initWithMemoryCapacity:1024*1024 // 1MB mem cache
diskCapacity:1024*1024*5 // 5MB disk cache
diskPath:[SDURLCache defaultCachePath]];
[NSURLCache setSharedURLCache:urlCache];
[urlCache release];To save flash drive, SDURLCache doesn't cache on disk responses if cache expiration delay is lower
than 5 minutes by default. You can change this behavior by changing the `minCacheInterval` property.Cache eviction is done automatically when disk capacity is reached in a periodic maintenance
thread. All disk write operations are done in a separated thread so they can't block the main run
loop.To control the caching behavior, use the `NSURLRequest`'s `cachePolicy` property. If you want a
response not to be cached on disk but still in memory, you can implement the `NSURLConnection`
`connection:willCacheResponse:` delegate method and change the `NSURLCachedResponse` `storagePolicy`
property to `NSURLCacheStorageAllowedInMemoryOnly`. See example below:- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSCachedURLResponse *memOnlyCachedResponse =
[[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response
data:cachedResponse.data
userInfo:cachedResponse.userInfo
storagePolicy:NSURLCacheStorageAllowedInMemoryOnly];
return [memOnlyCachedResponse autorelease];
}