LMKeyValueStore demo 有问题,有疑问欢迎加QQ:601113614
LMKeyValueStore是基于WCDB databas一套缓存框工具。支持网络json缓存
About WCDB : Tencent/WCDB , 微信WCDB进化之路 - 开源与开始
你可以在 Podfile 中加入下面一行代码来使用YTKKeyValueStore
pod "LMKeyValueStore"
缓存json的封装类为LMKeyValueStore
LMKeyValueStore *store = [[LMKeyValueStore alloc] initDBWithName:@"test.db"];
[store createTableWithName:@"test_table"];
// insert
NSString *tableName = @"test_table";
NSString *key1 = @"key1";
NSString *string = @"abc1";
[store putCacheString:string withId:key1 intoTable:tableName];
// select
NSString *result = [store getCacheStringById:key1 fromTable:tableName];
// 修改,使缓存过期
[store makeCacheExpiredById:key1 fromTable:tableName];
// delete
[store deletCacheStringById:key1 fromTable:tableName];
// delete all objcet
[store deleteAllCacheByTableName:tableName];
为了隔离c++的代码,不让引用wcdb引用的文件变成mm文件,特地封装了一个wcdb类,进行简单的数据库操作 接口都封装在LMWCDBOperation,然后更为复杂的数据库操作逻辑建议放在一个分类里面做更为合适
LMWCDBOperation *storeHelper = [[LMWCDBOperation alloc] initDBWithName:@"test.db"];
NSString *tableName = @"test_table";
LMKeyValueItem *item = [[LMKeyValueItem alloc] init];
item.itemId = @"key1";
item.itemObject = @"abc";
item.createdTime = [NSDate date];
//create
if (![storeHelper isTableExists:tableName]) {
[storeHelper createTableAndIndexesOfName:tableName withClass:[LMKeyValueItem class]];
}
// insert
[storeHelper insertObject:item into:tableName];
// select
LLMKeyValueItem *result = [storeHelper getOneObjectOfClass:[LMKeyValueItem class] fromTable:tableName primaryKeyName:@"id" primaryKey:item.itemId];
// modification
item.itemObject = @"def";
[storeHelper updateObjectInTable:tableName withObject:item primaryKeyName:@"id" primaryKey:item.itemId];
// delete
[storeHelper deleteObjectFromTable:tableName primaryKeyName:@"id" primaryKey:item.itemId];