iOS-手机缓存模型

一:HPCache

一个简单的线程安全且高吞吐量的模型,用于存储简单数据,支持model存储,无需转换。

二:思路

  • 创业一个自定义的DISPATCH_QUEUE_CONCURRENT队列。
  • dispatch_sync(或dispatch_async)用于不修改状态的操作。
  • dispatch_barrier_sync(或dispatch_barrier_async)用于可能修改状态的操作。

注意:这里的属性被标记为nonatomic,因为这里有自定义的代码使用了自定义的队列和屏障来管理线程安全。

三:代码

#import 

@interface HPCache : NSObject

+ (HPCache *)sharedInstance;

- (id)objectForKey:(nonnull id)key;
- (void)setObject:(id)object forKey:(nonnull id)Key;
- (void)removeObjectForKey:(nonnull id)Key;

@end
#import "HPCache.h"

@interface HPCache()

@property (nonatomic, readonly) NSMutableDictionary *cacheObjects;
@property (nonatomic, readonly) dispatch_queue_t queue;

@end

@implementation HPCache

const char *_Nullable kCacheQueueName = "kCacheQueueName";

- (instancetype)init{
    if (self = [super init]) {
        _cacheObjects = [NSMutableDictionary dictionary];
        _queue = dispatch_queue_create(kCacheQueueName, DISPATCH_QUEUE_CONCURRENT);
    }
    return self;
}

+ (HPCache *)sharedInstance{
    static HPCache *instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HPCache alloc] init];
    });
    return instance;
}

- (id)objectForKey:(id)key{
    __block id rv = nil;
    
    dispatch_sync(self.queue, ^{
        rv = [self.cacheObjects objectForKey:key];
    });
    
    return rv;
}

- (void)setObject:(id)object forKey:(id)Key{
    dispatch_barrier_sync(self.queue, ^{
        [self.cacheObjects setObject:object forKey:Key];
    });
}

- (void)removeObjectForKey:(nonnull id)Key{
    dispatch_barrier_sync(self.queue, ^{
        [self.cacheObjects removeObjectForKey:Key];
    });
}

@end

附上Demo

你可能感兴趣的:(iOS-手机缓存模型)