Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Olivier Poitrey
committed
Mar 17, 2010
1 parent
5ce771e
commit f5a555b
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2010 Olivier Poitrey <rs@dailymotion.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 which cache max-age is lower than | ||
5 minutes by default. You can change this behavior by changing the `minCacheInterval` property. | ||
|
||
Cache eviction is done automatically on cache-in, when disk capacity is outreached. All write disk | ||
operation like cache storing or evicting is done in a separated thread (using NSOperation) so it | ||
won't block the NSURLConnection run loop thread. | ||
|
||
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]; | ||
} |