GithubHelp home page GithubHelp logo

sdurlcache's Introduction

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 outreached 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];
}

sdurlcache's People

Contributors

saurik avatar cbess avatar danmorrow avatar ingmarstein avatar netj avatar kemenaran avatar romanbsd avatar yosit avatar

Watchers

James Cloos avatar 开水白菜 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.