公司这几天底层进行重构,本地需要缓存一些数据,对比目前一些比较火的三方,最终决定选用Objective-LevelDB。Objective-LevelDB,是一个基于Google's LevelDB的Objective-C数据库。然后对其进行了简单封装,支持内存和本地缓存,废话少说,直接用法。
支持pod, pod ‘Objective-LevelDB’
源码git地址https://github.com/matehat/Objective-LevelDB
下面是.h封装
#import <Foundation/Foundation.h>
/**
* 存储服务 基于Objective-LevelDB
*/
@interface FlyStorageManage : NSObject
/*
* 存储对象到内存
*
* @parm object 对象
* @parm akey key
*/
+ (void)setObjectToMemory:(id)object forKey:(NSString *)akey;
/**
* 内存中读取某个对象
*
* @parm akey key
*
* @return
*/
+ (id)objectFromMemoryForkey:(NSString *)akey;
/**
* 从内存中移除某个对象
*
* @parm akey key
*/
+ (void)removeObjectFromMemoryForKey:(NSString *)akey;
/**
* 存储对象到本地缓存
*
* @parm object 对象 对象需要实现NSCoding序列化
* 也可配合MJExtension/JsonModel转换成字典存储
* @parm akey key
*/
+ (void)setObjectToLocalCache:(id)object forKey:(NSString *)akey;
/**
* 从本地缓存中读取某个对象
*
* @parm akey key
*
* @return
*/
+ (id)objectFromLocalCacheForkey:(NSString *)akey;
/**
* 从本地缓存中移除某个对象
*
* @parm akey key
*/
+ (void)removeObjectFromLocalCacheForKey:(NSString *)akey;
@end
然后是.m的实现
#import "FlyStorageManage.h"
#import "Objective-LevelDB/Classes/LevelDB.h"
@interface FlyStorageManage ()
@property (nonatomic, strong) NSMutableDictionary *memoryServiceDict;
@property (nonatomic, strong) LevelDB *levelDB;
@end
static NSString *const kLevelDBName = @"level.db";
@implementation FlyStorageManage
+ (FlyStorageManage *)shareInstance
{
static dispatch_once_t onceToken;
static FlyStorageManage *service = nil;
dispatch_once(&onceToken, ^{
service = [[FlyStorageManage alloc] init];
});
return service;
}
- (instancetype)init
{
if (self = [super init]) {
self.memoryServiceDict = [[NSMutableDictionary alloc] init];
self.levelDB = [LevelDB databaseInLibraryWithName:kLevelDBName];
self.levelDB.encoder = ^NSData * (LevelDBKey * key, id object) {
__block id result = nil;
result = [NSKeyedArchiver archivedDataWithRootObject:object];
return result;
};
self.levelDB.decoder = ^id (LevelDBKey * key, id data) {
__block id result = nil;
result = [NSKeyedUnarchiver unarchiveObjectWithData:data];
return result;
};
}
return self;
}
#pragma mark --- MemoryService
+ (void)setObjectToMemory:(id)object forKey:(NSString *)akey
{
// 线程安全
@synchronized (self) {
if (object && akey) {
[[self shareInstance].memoryServiceDict setObject:object forKey:akey];
}
}
}
+ (id)objectFromMemoryForkey:(NSString *)akey
{
@synchronized (self) {
return [[self shareInstance].memoryServiceDict objectForKey:akey];
}
}
+ (void)removeObjectFromMemoryForKey:(NSString *)akey
{
@synchronized (self) {
[[self shareInstance].memoryServiceDict removeObjectForKey:akey];
}
}
#pragma mark --- LocalCacheService
+ (void)setObjectToLocalCache:(id)object forKey:(NSString *)akey
{
[[FlyStorageManage shareInstance].levelDB setObject:object forKey:akey];
}
+ (id)objectFromLocalCacheForkey:(NSString *)akey
{
return [[FlyStorageManage shareInstance].levelDB objectForKey:akey];
}
+ (void)removeObjectFromLocalCacheForKey:(NSString *)akey
{
[[FlyStorageManage shareInstance].levelDB removeObjectForKey:akey];
}
@end
下面是调用部分 (嫌序列化太麻烦,所以调用了MJ大大的MJExtension转换成字典进行存储)
- (void)viewDidLoad {
[super viewDidLoad];
Person *person = [[Person alloc] init];
person.name = @"测试下";
person.age = @"18";
person.height = 14;
person.textArr = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
NSString *personKey = @"testKey";
NSDictionary *perDic = person.mj_keyValues;
[FlyStorageManage setObjectToLocalCache:perDic forKey:personKey];
NSDictionary *dic = [CBStorageService objectFromLocalCacheForKey:personKey];
}
是不是很方便简单,有需要的朋友可以直接拿去用了