Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/context configuration #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions AFCache.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
05FA238B1357544B00050BCB /* AFCacheableItemInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAE8132A291B0087CEA1 /* AFCacheableItemInfo.h */; };
05FA238C1357544C00050BCB /* AFPackageInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAEA132A291B0087CEA1 /* AFPackageInfo.h */; };
05FA238D1357544D00050BCB /* AFURLCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAEC132A291B0087CEA1 /* AFURLCache.h */; };
05FA238E1357544F00050BCB /* Constants.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAEE132A291B0087CEA1 /* Constants.h */; };
05FA238F1357545000050BCB /* DateParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAEF132A291B0087CEA1 /* DateParser.h */; };
05FA23901357548D00050BCB /* libAFCacheOSXStatic.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 05FA235F1357539B00050BCB /* libAFCacheOSXStatic.a */; };
C73C71CC19816F13008EDA23 /* AFRequestConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = C73C71CA19816F13008EDA23 /* AFRequestConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -88,6 +87,9 @@
C7503D47198640AA0032E451 /* AFDownloadOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = C7503D44198640AA0032E451 /* AFDownloadOperation.h */; };
C7503D48198640AA0032E451 /* AFDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = C7503D45198640AA0032E451 /* AFDownloadOperation.m */; };
C7503D49198640AA0032E451 /* AFDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = C7503D45198640AA0032E451 /* AFDownloadOperation.m */; };
C799310A1987B974001A7724 /* AFCacheManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C79931081987B974001A7724 /* AFCacheManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
C799310B1987B974001A7724 /* AFCacheManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C79931091987B974001A7724 /* AFCacheManager.m */; };
C799310C1987C0BF001A7724 /* AFCacheManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C79931081987B974001A7724 /* AFCacheManager.h */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -174,7 +176,6 @@
05C9BAEB132A291B0087CEA1 /* AFPackageInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFPackageInfo.m; path = src/shared/AFPackageInfo.m; sourceTree = "<group>"; };
05C9BAEC132A291B0087CEA1 /* AFURLCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFURLCache.h; path = src/shared/AFURLCache.h; sourceTree = "<group>"; };
05C9BAED132A291B0087CEA1 /* AFURLCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFURLCache.m; path = src/shared/AFURLCache.m; sourceTree = "<group>"; };
05C9BAEE132A291B0087CEA1 /* Constants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Constants.h; path = src/shared/Constants.h; sourceTree = "<group>"; };
05C9BAEF132A291B0087CEA1 /* DateParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DateParser.h; path = src/shared/DateParser.h; sourceTree = "<group>"; };
05C9BAF0132A291B0087CEA1 /* DateParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DateParser.m; path = src/shared/DateParser.m; sourceTree = "<group>"; };
05C9BB08132A29370087CEA1 /* AFRegexString.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFRegexString.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -214,6 +215,8 @@
C73C71CB19816F13008EDA23 /* AFRequestConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFRequestConfiguration.m; path = src/shared/AFRequestConfiguration.m; sourceTree = "<group>"; };
C7503D44198640AA0032E451 /* AFDownloadOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFDownloadOperation.h; path = src/shared/AFDownloadOperation.h; sourceTree = "<group>"; };
C7503D45198640AA0032E451 /* AFDownloadOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFDownloadOperation.m; path = src/shared/AFDownloadOperation.m; sourceTree = "<group>"; };
C79931081987B974001A7724 /* AFCacheManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFCacheManager.h; path = src/shared/AFCacheManager.h; sourceTree = "<group>"; };
C79931091987B974001A7724 /* AFCacheManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFCacheManager.m; path = src/shared/AFCacheManager.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -368,7 +371,6 @@
05238D14151B6E470015D70E /* core */ = {
isa = PBXGroup;
children = (
05C9BAEE132A291B0087CEA1 /* Constants.h */,
05C9BADD132A291B0087CEA1 /* AFCache.h */,
05C9BADE132A291B0087CEA1 /* AFCache.m */,
05C9BAE3132A291B0087CEA1 /* AFCache+PrivateAPI.h */,
Expand All @@ -380,6 +382,8 @@
C73C71CB19816F13008EDA23 /* AFRequestConfiguration.m */,
C7503D44198640AA0032E451 /* AFDownloadOperation.h */,
C7503D45198640AA0032E451 /* AFDownloadOperation.m */,
C79931081987B974001A7724 /* AFCacheManager.h */,
C79931091987B974001A7724 /* AFCacheManager.m */,
);
name = core;
sourceTree = "<group>";
Expand Down Expand Up @@ -536,6 +540,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
C799310A1987B974001A7724 /* AFCacheManager.h in Headers */,
05C9BB6F132A30E60087CEA1 /* AFCacheLib.h in Headers */,
046BEFAB152D180A00FE16B8 /* AFCache.h in Headers */,
046BEFAC152D180A00FE16B8 /* AFCacheableItem.h in Headers */,
Expand All @@ -559,8 +564,8 @@
05FA23841357543A00050BCB /* AFCache_Logging.h in Headers */,
05FA23891357544400050BCB /* AFCacheableItem.h in Headers */,
05FA238B1357544B00050BCB /* AFCacheableItemInfo.h in Headers */,
C799310C1987C0BF001A7724 /* AFCacheManager.h in Headers */,
05FA238C1357544C00050BCB /* AFPackageInfo.h in Headers */,
05FA238E1357544F00050BCB /* Constants.h in Headers */,
05FA238F1357545000050BCB /* DateParser.h in Headers */,
05FA238D1357544D00050BCB /* AFURLCache.h in Headers */,
C7503D47198640AA0032E451 /* AFDownloadOperation.h in Headers */,
Expand Down Expand Up @@ -796,6 +801,7 @@
buildActionMask = 2147483647;
files = (
05C9BAF3132A291B0087CEA1 /* AFCache.m in Sources */,
C799310B1987B974001A7724 /* AFCacheManager.m in Sources */,
05C9BAF5132A291B0087CEA1 /* AFCache+Mimetypes.m in Sources */,
05C9BAFA132A291B0087CEA1 /* AFCacheableItem.m in Sources */,
05C9BAFC132A291B0087CEA1 /* AFCacheableItem+Packaging.m in Sources */,
Expand Down Expand Up @@ -901,6 +907,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
Expand All @@ -925,6 +932,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
Expand Down
5 changes: 2 additions & 3 deletions src/shared/AFCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,15 @@ typedef struct NetworkTimeoutIntervals {
*/
@property (nonatomic, assign) BOOL disableSSLCertificateValidation;

+ (AFCache*)cacheForContext:(NSString*)context;

- (NSString *)filenameForURL: (NSURL *) url;
- (NSString *)filenameForURLString: (NSString *) URLString;
- (NSString *)filePath: (NSString *) filename;
- (NSString *)filePathForURL: (NSURL *) url;
- (NSString *)fullPathForCacheableItem:(AFCacheableItem*)item;


+ (AFCache *)sharedInstance;
+ (AFCache *)sharedInstance __attribute__((deprecated("use defaultCache instead")));
+ (AFCache *)defaultCache;


- (AFCacheableItem *)cachedObjectForURL: (NSURL *) url
Expand Down
71 changes: 14 additions & 57 deletions src/shared/AFCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <sys/xattr.h>
#import "AFRegexString.h"
#import "AFCache_Logging.h"
#import "AFCacheManager.h"

#if USE_ASSERTS
#define ASSERT_NO_CONNECTION_WHEN_OFFLINE_FOR_URL(url) NSAssert( [(url) isFileURL] || [self isOffline] == NO, @"No connection should be opened if we're in offline mode - this seems like a bug")
Expand All @@ -47,7 +48,6 @@

@interface AFCache()

@property (nonatomic, copy) NSString *context;
@property (nonatomic, strong) NSMutableArray *downloadQueue;
@property (nonatomic, strong) NSTimer *archiveTimer;
@property (nonatomic, assign) BOOL wantsToArchive;
Expand All @@ -56,34 +56,25 @@ @interface AFCache()

- (void)serializeState:(NSDictionary*)infoStore;
- (void)cancelAllClientItems;
- (id)initWithContext:(NSString*)context;
@end

@implementation AFCache

static AFCache *sharedAFCacheInstance = nil;
static NSMutableDictionary* AFCache_contextCache = nil;

#pragma mark singleton methods

+ (AFCache *)sharedInstance {
@synchronized(self) {
if (sharedAFCacheInstance == nil) {
sharedAFCacheInstance = [[self alloc] initWithContext:nil];
sharedAFCacheInstance.diskCacheDisplacementTresholdSize = kDefaultDiskCacheDisplacementTresholdSize;
}
}
return sharedAFCacheInstance;
// left for backward compability
return [AFCacheManager defaultCache];
}

+ (AFCache *)defaultCache {
return [AFCacheManager defaultCache];
}

#pragma mark init methods

- (id)initWithContext:(NSString*)context {
if (!context && sharedAFCacheInstance != nil)
{
return [AFCache sharedInstance];
}

- (id)init
{
self = [super init];

if (self) {
Expand All @@ -99,17 +90,11 @@ - (id)initWithContext:(NSString*)context {
name:UIApplicationWillTerminateNotification
object:nil];
#endif
if (!AFCache_contextCache) {
AFCache_contextCache = [[NSMutableDictionary alloc] init];
}

if (context) {
[AFCache_contextCache setObject:[NSValue valueWithPointer:(__bridge const void *)(self)] forKey:context];
}

_context = [context copy];

[self reinitialize];
[self initMimeTypes];

self.diskCacheDisplacementTresholdSize = kDefaultDiskCacheDisplacementTresholdSize;
}
return self;
}
Expand Down Expand Up @@ -166,20 +151,14 @@ - (void)initialize {

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];

if (_context)
{
[AFCache_contextCache removeObjectForKey:_context];
}

}

- (NSUInteger)requestsPending {
return [self.pendingConnections count];
}

- (void)setDataPath:(NSString*)newDataPath {
if (self.context && self.dataPath)
if (self.dataPath)
{
NSLog(@"Error: Can't change data path on instanced AFCache");
NSAssert(NO, @"Can't change data path on instanced AFCache");
Expand All @@ -194,28 +173,6 @@ - (void)setDataPath:(NSString*)newDataPath {
self.maxItemFileSize = fileSize;
}

// TODO: If we really need "named" caches ("context" is the wrong word), then realize this concept as a category, but not here
+ (AFCache*)cacheForContext:(NSString *)context
{
if (!AFCache_contextCache)
{
AFCache_contextCache = [[NSMutableDictionary alloc] init];
}

if (!context)
{
return [self sharedInstance];
}

AFCache* cache = [[AFCache_contextCache objectForKey:context] pointerValue];
if (!cache)
{
cache = [[[self class] alloc] initWithContext:context];
}

return cache;
}

// The method reinitialize really initializes the cache.
// This is usefull for testing, when you want to, uh, reinitialize

Expand Down Expand Up @@ -249,7 +206,7 @@ -(void)addRedirectFromURL:(NSURL*)originalURL toURL:(NSURL*)redirectURL
[self.urlRedirects setObject:[redirectURL absoluteString] forKey:[originalURL absoluteString]];
}

-(void)addRedirectFromURLString:(NSString*)originalURLString toURLString:(NSString*)redirectURLString
-(void)addRedirectFromURLString:(NSString*)originalURLString toURL:(NSString*)redirectURLString
{
[self.urlRedirects setObject:redirectURLString forKey:originalURLString];
}
Expand Down
25 changes: 25 additions & 0 deletions src/shared/AFCacheManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// AFCacheManager.h
// AFCache
//
// Created by Sebastian Grimme on 29.07.14.
// Copyright (c) 2014 Artifacts - Fine Software Development. All rights reserved.
//

#import "AFCache.h"

extern NSString *kAFCacheDefaultName;

@interface AFCacheManager : NSObject

/**
* @return default cache instance
*/
+ (AFCache*)defaultCache;

/**
* @return named cache instance
*/
+ (AFCache*)cacheNamed:(NSString*)name;

@end
73 changes: 73 additions & 0 deletions src/shared/AFCacheManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// AFCacheManager.m
// AFCache
//
// Created by Sebastian Grimme on 29.07.14.
// Copyright (c) 2014 Artifacts - Fine Software Development. All rights reserved.
//

#import "AFCacheManager.h"

NSString *kAFCacheDefaultName = @"AFCacheDefaultName";

static AFCacheManager *sharedAFCacheManagerInstance = nil;

@interface AFCacheManager ()
@property (nonatomic, strong) NSMutableDictionary* instanceDictionary;
@end

@implementation AFCacheManager

#pragma mark singleton methods

+ (AFCacheManager*)sharedManager {
@synchronized(self) {
if (sharedAFCacheManagerInstance == nil) {
sharedAFCacheManagerInstance = [[self alloc] init];
}
}
return sharedAFCacheManagerInstance;
}

#pragma mark - Lifecycle

- (instancetype)init
{
self = [super init];
if (self) {
// create dictionary which holds all named cache-instances
_instanceDictionary = [[NSMutableDictionary alloc] init];
}
return self;
}

#pragma mark - Private API

- (AFCache*)cacheInstanceNamed:(NSString *)name
{
@synchronized (self.instanceDictionary) {
AFCache *cacheInstance = [[AFCacheManager sharedManager].instanceDictionary objectForKey:name];

if (!cacheInstance) {
cacheInstance = [[AFCache alloc] init];

[[AFCacheManager sharedManager].instanceDictionary setObject:cacheInstance forKey:name];
}

return cacheInstance;
}
}

#pragma mark - static factory/get methods

+ (AFCache*)defaultCache
{
return [[AFCacheManager sharedManager] cacheInstanceNamed:kAFCacheDefaultName];
}

+ (AFCache*)cacheNamed:(NSString*)name
{
return [[AFCacheManager sharedManager] cacheInstanceNamed:name];
}

@end
2 changes: 2 additions & 0 deletions src/shared/AFCacheableItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ - (AFCacheableItem*)initWithURL:(NSURL*)URL
_url = URL;
_cacheStatus = kCacheStatusFresh;
_validUntil = _info.expireDate;

// TODO: We cannot assume that this item's cache is the default sharedInstance
_cache = [AFCache sharedInstance];
}
return self;
Expand Down
6 changes: 4 additions & 2 deletions src/shared/AFHTTPURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ - (id)initWithRequest:(NSURLRequest *)aRequest

- (void)startLoading
{
[[AFCache sharedInstance] cachedObjectForRequest:self.request delegate:self];
// this URL-Protocol works with defaultCache only
[[AFCache defaultCache] cachedObjectForRequest:self.request delegate:self];
}

- (void) connectionDidFail: (AFCacheableItem *) cacheableItem {
Expand All @@ -98,7 +99,8 @@ - (void)connectionHasBeenRedirected: (AFCacheableItem*) cacheableItem {

- (void)stopLoading
{
[[AFCache sharedInstance] cancelAsynchronousOperationsForURL:[[self request] URL] itemDelegate:self];
// this URL-Protocol works with defaultCache only
[[AFCache defaultCache] cancelAsynchronousOperationsForURL:[[self request] URL] itemDelegate:self];
}

- (NSCachedURLResponse *)cachedResponse {
Expand Down
Loading