Git Product home page Git Product logo

sdurlcache's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sdurlcache's Issues

removeAllCachedResponses doesn't clear disk cache

removeAllCachedResponses simply calls [super removeAllCachedResponses] which clears the memory cache and leaves the disk cache untouched.

I've added the following brute force code to the method, which seems to work, but there's probably something less drastic you could do given an understanding of the cache internals.

- (void)removeAllCachedResponses { [super removeAllCachedResponses];

//delete disk cache
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:diskCachePath])
{
[fileManager removeItemAtPath:diskCachePath error:NULL];
}
[fileManager release];

//clear info
[diskCacheInfo release];
diskCacheInfo = nil;
}

diskCache shouldStore and cache purge problem

I may be wrong here, but what I'm seeing reading the code is that storeCachedResponse: says

if ( ... && cachedResponse.data.length < self.diskCapacity )
    // ... store to disk

but the only way diskCapacity decreases is in balanceDiskUsage but that method begins with:

- (void)balanceDiskUsage
{
    if (diskCacheUsage < self.diskCapacity)
    {
         // Already done
         return;
    }
    ....

That method is only called from periodicMaintenance but only if disk usage "overrich" capacity.

if (diskCacheUsage > self.diskCapacity)

which, as far as I can tell reading the code is impossible !

so, it seems like the disk usage will grow up to below the limit and then any response that is attempted to cache will fail, but the cache will never get cleared out and so nothing will ever again be cached to disk.

what am I missing?

Thanks.

diskCapacity is changing

Hi,

I have a weird behavior, I set diskCapacity to 0.5_1024_1024 to make some tests on limits. I check it in initWithMemoryCapacity: diskCapacity:diskPath: and it is good.
diskCapacity = 524288

Then I'm doing some access to fill the cache, and in the periodicMaintenance i found that diskCapacity is now 52428800.

Does anyone already seen this ?
As diskCapacity is not changed in SDURLCache, i'm wondering if NSURLCache is changing it and what rules it use.

Why not save the expire-date?

For example, I cache a file which would expired 10 days later. It would saved.
And 20 days later , when I want to requst this file again, in the method cachedResponseForRequest , it would return a cache for me , but the cache had been expired.
In my opinion, it is better to remove the expired cache ,when I request.
So, I should save the expire information ,when save to disk.

Cache in offline

Hi,

This is not a bug but a comment about offline use of SDURLCache.

I'm trying to use SDURLCache to make an offline cache for UIWebView. Actually it doesn't work as in offline I get an error "no connexion available" (no sure about the english message). Code=-1009.

So what I did to solve this problem is to change the cachedResponseForRequest: method to create a new NSCachedURLResponse from the cache instance. This trick works for almost all resources except the JQuery AJAX request because it seems that the statusCode returned is 0.
I tried much modifications to make it work but actually I cannot solve this problem :
I tried to subclass NSHTTPURLResponse to force statusCode to 0 => statusCode is never called
I tried a category on NSURLResponse which implements statusCode => statusCode is called so I can return 200 but jquery still get a 0 status

So I don't know how to control the final response from the cache to the client, mainly the statusCode and the headers.

If you have any information about this, I might be interested.

If you are interested by the changes I made, the code is :

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
    request = [SDURLCache canonicalRequestForRequest:request];

    NSCachedURLResponse *memoryResponse = [super cachedResponseForRequest:request];
    if (memoryResponse)
    {
        if ([self isConnectedToNetwork])
        {
            return memoryResponse;
        }
        else 
        {
            NSURLResponse* response = [[NSURLResponse alloc] initWithURL:request.URL 
                                                            MIMEType:[[memoryResponse response] MIMEType] 
                                               expectedContentLength:[[memoryResponse data] length] 
                                                    textEncodingName:[[memoryResponse response] textEncodingName]];     
            NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:[memoryResponse data] userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly];
            [response release];
            return [cachedResponse autorelease];
        }
    }


    NSString *cacheKey = [SDURLCache cacheKeyForURL:request.URL];

    // NOTE: We don't handle expiration here as even staled cache data is necessary for NSURLConnection to handle cache revalidation.
    //       Staled cache data is also needed for cachePolicies which force the use of the cache.
    @synchronized(self.diskCacheInfo)
    {
        NSMutableDictionary *accesses = [self.diskCacheInfo objectForKey:kSDURLCacheInfoAccessesKey];
        if ([accesses objectForKey:cacheKey]) // OPTI: Check for cache-hit in a in-memory dictionnary before to hit the FS
        {
            NSCachedURLResponse *diskResponse = [NSKeyedUnarchiver unarchiveObjectWithFile:[diskCachePath stringByAppendingPathComponent:cacheKey]];
            if (diskResponse)
            {
                // OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary
                //       on disk now in order to save IO and time
                [accesses setObject:[NSDate date] forKey:cacheKey];
                diskCacheInfoDirty = YES;

                // OPTI: Store the response to memory cache for potential future requests
                [super storeCachedResponse:diskResponse forRequest:request];

                // SRK: Work around an interesting retainCount bug in CFNetwork on iOS << 3.2.
                if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_2)
                {
                    diskResponse = [super cachedResponseForRequest:request];
                }

                if (diskResponse)
                {
                    if ([self isConnectedToNetwork])
                    {
                        return diskResponse;
                    }
                    else
                    {
                        NSURLResponse* response = [[NSURLResponse alloc] initWithURL:[[diskResponse response] URL] 
                                                                        MIMEType:[[diskResponse response] MIMEType] 
                                                           expectedContentLength:[[diskResponse data] length] 
                                                                textEncodingName:[[diskResponse response] textEncodingName]];
                        NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:[diskResponse data] userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly];

                        [response release];

                        return [cachedResponse autorelease];
                    }
                }
            }
        }
    }
    return nil;
}

SDURLCache.m line 189 issue

Forgive me for my lack of cache knowledge. I was code reviewing SDURLCache.m the other day and was puzzled by line 189 in the "Expires" logic block. In this method, the code calculates "now" based on the Date in the response (or now if none) If a cached response header has a date of yesterday morning, and an expires of this morning, this method appears to calculate an interval of 24 hours, and then returns an expiration date based on 24 hours from now

Am I reading that entirely incorrectly? Or is it some case where if there is an Expires header, there won't be a Date header?

Thanks!

How to use Application Caches?

When I load a page with a manifest file in UIWebview , the UIWebView would create a file with a path "Library/Caches/com.apple.WebAppCache/ApplicationCache.db " ,where the offline cache stored.

Is there a interface to use the Application Cache?

Occasional SIGSEGV in saveCacheInfo:

I have two apps that currently use this library, and I'm seeing an occasional crash that looks like this from instances downloaded by users from the AppStore, in both of the apps:

0 libobjc.A.dylib 0x3188cfbc objc_msgSend 16316 <--- Signal SEGV occurs here
1 CoreFoundation 0x33824301 CFBinaryPlistWrite 447233
2 CoreFoundation 0x337dccdd CFPropertyListWrite 154845
3 CoreFoundation 0x337dcad5 CFPropertyListWriteToStream 154325
4 Foundation 0x32784f11 [NSPropertyListSerialization dataFromPropertyList:format:errorDescription:] 73489
5 MyAppName 0x0001ddab -[SDURLCache saveCacheInfo] 118187
6 MyAppName 0x0001e64b -[SDURLCache storeToDisk:] 120395
7 CoreFoundation 0x33873814 _invoking
772116
8 CoreFoundation 0x337ce7e1 -[NSInvocation invoke] 96225
9 Foundation 0x3280569f -[NSInvocationOperation main] 599711
10 Foundation 0x3279e3a3 -[__NSOperationInternal start] 177059
11 Foundation 0x328077a3 __block_global_6 608163
12 libdispatch.dylib 0x360e1d55 _dispatch_call_block_and_release 3413
13 libdispatch.dylib 0x360ed7a3 _dispatch_worker_thread2 51107
14 libsystem_c.dylib 0x35d6f1cf _pthread_wqthread 41423

Any ideas how to solve this?

Thanks!

Set default expiration for certain cache entries ?

In my application, I want to set cache-expiration to, say, 30 days for images coming from our CDN, irrespective of what the caching headers on the images say. Is this possible to do in SDURLCache ? I imagine this would entail setting the 'expires' header on the response received. But NSURLResponse (or NSHTTPURLResponse) is not mutable.

getting occasional crashes in dateFromHttpDateString:

In dateFromHttpDateString:, there is a call to dateFromString. Sometimes it crashes my code. The datestring looks compliant, and RFC1123DateFormatter looks valid. But for some reason it crashes on the first dateFromString call. Obviously not all the time, but still...

Any idea why this might be?

Can't manage to make SDURLCache work

I've created a small application sample using SDURLCache. The example opens a web view and opens the Google URL. You can find it at http://dl.dropbox.com/u/145894/t/OfflineCache.zip. It's a template project with a modified viewDidLoad method to put all the code there for simplicity.

When I run this on the device the SDURLCache get's exercised and files are written to disk. However, when I enter the application with the iPad in "plane mode" the web view doesn't load resources even though SDURLCache seems to load disk cache resources.

Any ideas why this happens? I've tried replacing the Google url with other addresses and don't seem able to make it work in any way.

Unclear how to use this library

Hi,

I am not seeing any caching behavior. I have set my request's caching policy to NSURLRequestReturnCacheDataElseLoad as well.

Can you update the documentation to make it more clear where we need to alloc init the SDURLCache object? I assume that it's in viewDidLoad or right before an NSURLRequest is made?

Thanks

When running in simulator, the path passed to initWithMemoryCapacity:diskCapacity:diskPath: is not relative

The documentation for NSURLCache says:

In iOS, path is the name of a subdirectory of the application’s default cache directory in which to store the on-disk cache (the subdirectory is created if it does not exist).

this does not seem to be the case for me under Xcode 3.2.6 and simulator 4.3. When I pass in @"myCache" the cache is created at /myCache at the root of my hard drive (interesting that it has permission to write there).

Further, on a device I am not finding the cached data inside the application sandbox. Don't yet know if it's just never created or created at /myCache on the iPhone filesystem (hope not as that's a pretty nasty security hole !!).

So, two things to fix relating to this:

  1. Document that the developer documentation is incorrect (full path required, relative to the application sandbox's Cache directory)
  2. createDiskCachePath should check the error from NSFileManager createDirectoryAtPath: and emit a log message or something to let the programmer know they've goofed.

Suggest init diskCacheInfo on mainthread

I found that diskCacheInfo may create in WebThread., thus the timer run at WebThread .
You can't control WebThread's time life.
I think it should start timer on main thread.

Disk usage goes back to zero upon app restart

I have a method that prints the current disk usage & the current memory usage. I do some activity in my app & verify that these numbers are both non-zero. Then when I restart my app, both these numbers go back to zero. I can understand memory usage going back to zero, but can't understand why the disk usage goes back to zero too? After the restart, I can still see the files that SDURLCache created on disk which means that the caching had indeed succeeded.
I am testing against iOS 4.2.1 on an iPad.
I'm calling [[NSURLCache sharedURLCache] currentDiskUsage] to get the disk usage.

Warning

Pods/SDURLCache/SDURLCache.m:75:17:

Implicit conversion loses integer precision: 'unsigned long' to 'CC_LONG' (aka 'unsigned int')

that's nothing, but just to remove this annoying warning from xcode

Are you sure the on-disk cache was removed?

I found there are two db files on folder"Library/Catches/com.company.appname/" with UIWebView app:

ApplicationCache.db, Cache.db

==and==
try those sqls:

"select * from CacheResourceData", after opening ApplicationCache.db

"select * from cfurl_cache_receiver_data" on Cache.db

Both are cache file content on the disk, so I do not really agree with you that:

There is no cache on iPhone & iPad.

Where is cache in ios5.1.1?

In ios4.x or ios5.0.1 ,there is a directory named"webkit" in the app libarary/webkit , where the cache db and file in.
But after upgrade to 5.1.1 ,the webkit folder disappered.
Where the cache foloder move to ?

SDURLCache and NSURLRequestReturnCacheDataDontLoad

I cannot use properly SDURLCache disk.

When I use NSURLCache NSURLRequestReturnCacheDataDontLoad policy works properly but when I add SDURLCache doesn't return cached requests.

Somebody could help me?

Thanks in advance,

Raúl Pérez

Etags override Cache-Control headers?

You say here,

https://github.com/rs/SDURLCache/blob/master/SDURLCache.m#L478

that you skipping the examination of other headers because of http://tools.ietf.org/html/rfc2616#section-13.3.4 - this document seems to indicate, however, that Etags should be given precedence over Last-Modified headers. But I believe that if a response contains and Etag and a Cache-Control header, the client should respect the Cache-Control header values, like max-age, for example.

not work in iOS9

seems it store the response but never ask the response in sdurlcache, in iOS 9.
(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request never be called. Any similar case?

AFNetworking's AFImageRequestOperation cannot find cached images sometimes

Hi;

I am using SDURLCache like this:

SDURLCache *urlCache = [[SDURLCache alloc] initWithMemoryCapacity:1024*1024 * 20
                                                     diskCapacity:1024*1024*20 
                                                         diskPath:[SDURLCache defaultCachePath]];    
[urlCache setIgnoreMemoryOnlyStoragePolicy:YES];
[NSURLCache setSharedURLCache:urlCache];

And for image request:

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:c.imagePath] cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval:30.0];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest
                                                                                  imageProcessingBlock:nil
                                                                                             cacheName:@"nscache"
                                                                                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){                                                                                                                                                                                                   imageview.image = image;
                                                                                               }
                                                                                               failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ NSLog([error description]); }
                                              ];
        [operation start];

With these codes, sometimes all of my images are cached and successfully retrieved from the cache. But sometimes, some images cannot be displayed. How can I find the problem?

SDURLCache differs from the usual cache when used with UIWebView

We're hitting this url using a UIWebView: http://web.moblin.com/apps/carlsberg/takanon.htm

These are the headers for the response
Content-Length 89830
Content-Type text/html
Last-Modified Tue, 12 Jun 2012 16:10:43 GMT
Accept-Ranges bytes
ETag "c89746ebb548cd1:7fb"
Server Microsoft-IIS/6.0
Date Thu, 14 Jun 2012 21:36:45 GMT

When we remove SDURLCache the response is not cached by iOS - but when with SDURCache it is cached forever.
We tried to understand why and it seems that once SDURLCache sees the ETag header all the following request to load that page again by UIWebView would result with the content of the first cached reponse. Are we missing something here?

setting memoryCapacity to anything means memory grows forever

OK, this is weird behavior, and I've studied it for quite some time, so here goes.

I ran three separate tests. Two tests were using SDURLCache as the cache, and the third used NSURLCache as the cache.

In the SDURLCache cases, I'm initializing the cache as so:

SDURLCache* curCache = [[[SDURLCache alloc] initWithMemoryCapacity:myMemCapacity
                                                              diskCapacity:1024*1024*50 // 50MB disk cache
                                                                  diskPath:[SDURLCache defaultCachePath]] autorelease];

First I set myMemCapacity to 1024*1024 (1Mb). I can see memory consumption increase as long as I'm running my app. But if I set myMemCapacity to 0, memory consumption stays stable and doesn't grow.

In the case where I'm using NSURLCache, I'm doing tihs:

        [[NSURLCache sharedURLCache] setMemoryCapacity:1024*1024];

In this case, I'm no longer using SDURLCache, but I did change the memory capacity. It does not grow unbounded. The memory cache grows as expected, and then memory consumption stabilizes, and does not increase.

I've looked at this using Allocations in instruments. You can see the total allocations grow when using SDURLCache with memoryCapacity set. You can also see a large number of CFURLRequest/CFURLResponse/CFCachedURLResponse values, that keep increasing over time.

In the last case, using NSURLCache, you see CFURLRequest, etc, growing, but then it stabilizes. It goes up and down a bit, but the memory cache value appears to be respected.

Now, the whole reason I'm using SDURLCache is to cache stuff to disk, so I guess setting memoryCapacity to 0 isn't so bad. But I wouldn't expect this behavior. I've looked over the code, and I don't see any reason why this should behave this way, but I'm not really an expert on URL caching behavior. So, I'm not sure how to address this.

storeCachedResponse:forRequest: not called all the time

This might be just my lack of understanding of how this method works, but is this always to be called all the time when I load a web page with resources in it or is there a constraint that made this not getting called.

  • (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request

expired cache items

I have been using SDURLCache for a while now. However, I don't seem to understand what SDURLCache does with expired cache items. What I understand from the code is the cachedResponseForRequest: method would return the cached copy from disk even though its expired, instead of loading it from the network. Is this understanding accurate ?

UIWebView caching failure for iOS>5.0

When the fallback to NSURLCache happens (iOS>5.0) caching for UIWebViews does not work. I assume it is because NSCachedURLResponse coming from an UIWebView has a storagePolicy set to NSURLCacheStorageAllowedInMemoryOnly as discussed here: #1 .

XCode 4.3.1 warning

"Category is implementing a method which will also be implemented by its primary class" for NSCachedURLResponse(NSCoder)

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.