Skip to content

Commit

Permalink
- first added AFCacheManager to manage mutiple named instances
Browse files Browse the repository at this point in the history
- removed deprecated context-code from AFCache
- renamed context to name , because its a named cache, context has a different meaning
  • Loading branch information
sgrimme committed Jul 29, 2014
1 parent 4d66316 commit 0bc0f2f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 59 deletions.
12 changes: 12 additions & 0 deletions AFCache.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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 @@ -214,6 +217,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 @@ -380,6 +385,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 +543,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,6 +567,7 @@
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 */,
Expand Down Expand Up @@ -796,6 +805,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 +911,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 +936,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
2 changes: 0 additions & 2 deletions src/shared/AFCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ typedef struct NetworkTimeoutIntervals {
*/
@property (nonatomic, assign) BOOL disableSSLCertificateValidation;

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

- (NSString *)filenameForURL: (NSURL *) url;
- (NSString *)filenameForURLString: (NSString *) URLString;
- (NSString *)filePath: (NSString *) filename;
Expand Down
67 changes: 10 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"

This comment has been minimized.

Copy link
@larsblumberg

larsblumberg Jul 29, 2014

Collaborator

AFCache should not know AFCacheManager to not only avoid cyclic dependencies but also to all AFCache being included without requiring to (implicitly) include AFCacheManager.


#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,21 @@ @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];

This comment has been minimized.

Copy link
@larsblumberg

larsblumberg Jul 29, 2014

Collaborator

this should say instead:

return [AFCache 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 +86,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 +147,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 +169,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 +202,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*)cacheForName:(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 cache-instances
_instanceDictionary = [[NSMutableDictionary alloc] init];
}
return self;
}

#pragma mark - Private API

- (AFCache*)cacheInstanceForName:(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] cacheInstanceForName:kAFCacheDefaultName];
}

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

@end

0 comments on commit 0bc0f2f

Please sign in to comment.