Skip to content

Commit

Permalink
Merge pull request #66 from schmidt9/objective-c
Browse files Browse the repository at this point in the history
added ignoring logic for requests
  • Loading branch information
evermeer authored Aug 26, 2019
2 parents 13915aa + 2826347 commit 86a47fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions pod/EVURLCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@interface EVURLCache : NSURLCache

+(void)activate;
+(void)setIgnoredMasks:(NSArray<NSString*>*)ignoredMasks;
+(NSString*)storagePathForRequest:(NSURLRequest*)request;
+(NSString*)storagePathForRequest:(NSURLRequest*)request rootPath:(NSString*)rootPath;

Expand Down
36 changes: 36 additions & 0 deletions pod/EVURLCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

static NSString* _cacheDirectory;
static NSString* _preCacheDirectory;
static NSArray<NSString*>* _ignoredMasks;

@implementation EVURLCache

Expand All @@ -32,6 +33,31 @@ +(void)activate
[NSURLCache setSharedURLCache:urlCache];
}

+(void)setIgnoredMasks:(NSArray<NSString*>*)ignoredMasks {
_ignoredMasks = ignoredMasks;
}

-(BOOL)shouldIgnoreCachedResponseForRequest:(nonnull NSURLRequest *)request
{
for (NSString *mask in _ignoredMasks) {
NSError *error;
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:mask
options:NSRegularExpressionCaseInsensitive error:&error];

if (error) {
[EVURLCache log:@"Unable to parse ignored mask (%@)", error.localizedDescription];
break;
}

NSString *urlString = request.URL.absoluteString;

if ([regularExpression numberOfMatchesInString:urlString options:0 range:NSMakeRange(0, urlString.length)] > 0) {
return YES;
}
}

return NO;
}

// initializing the cache. Only used by the method above.
-(id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)diskPath {
Expand All @@ -52,6 +78,12 @@ -(NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request{
[EVURLCache log:@"CACHE not allowed for emtpy urls"];
return nil;
}
// If we have saved cache for given response before ignoring rule was set
if([self shouldIgnoreCachedResponseForRequest:request])
{
[EVURLCache log:@"CACHE ignored for %@", request.URL.absoluteString];
return nil;
}
// is caching allowed
if(request.cachePolicy == NSURLRequestReloadIgnoringCacheData || [request.URL.absoluteString hasPrefix:@"file:/"] || ([request.URL.absoluteString hasPrefix:@"data:"] && [EVURLCache networkAvailable]))
{
Expand Down Expand Up @@ -113,6 +145,10 @@ -(NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request{
// Will be called by NSURLConnection when a request is complete.
-(void)storeCachedResponse:(NSCachedURLResponse*)cachedResponse forRequest:(nonnull NSURLRequest *)request
{
if ([self shouldIgnoreCachedResponseForRequest:request]) {
return;
}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) cachedResponse.response;
if(httpResponse != nil && [httpResponse respondsToSelector:@selector(statusCode:)] && httpResponse.statusCode >= 400)
Expand Down

0 comments on commit 86a47fa

Please sign in to comment.