iOS緩存策略

iOS緩存策略

一、思維腦圖

思維腦圖

思維腦圖詳情

二、緩存思想

  • 數(shù)據(jù)庫底層基于Sqlite。
  • 每個數(shù)據(jù)庫表只有Key, Value兩個字段。
  • 直接將JSON數(shù)據(jù)存儲到Value中,并設(shè)置Key。
  • 通過Key查找對應Value數(shù)據(jù),來進行數(shù)據(jù)增刪改查操作,并更新視圖。

1.使用SDWebImage緩存圖片。
2.使用YTKKeyValueStore更方便使用FMDB。
3.使用FMDB操作數(shù)據(jù)庫。

SDWebImage官方地址: https://github.com/rs/SDWebImage
YTKKeyValueStore官方地址: https://github.com/yuantiku/YTKKeyValueStore
FMDB官方地址: https://github.com/ccgus/fmdb


三、使用YTKKeyValueStore操作數(shù)據(jù)庫

iOS端數(shù)據(jù)量不大,使用最簡單直接的Key-Value存儲就能帶來開發(fā)上的效率優(yōu)勢。
1.Model層的代碼編寫簡單,易于測試。
2.由于Value是JSON格式,所以在做Model字段更改時,易于擴展和兼容。

  • 簡單使用
//1.打開數(shù)據(jù)庫(若有),創(chuàng)建數(shù)據(jù)庫(若無)
YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"test.db"];
//2.創(chuàng)建數(shù)據(jù)庫表(若無),忽略(若無)
NSString *tableName = @"user_table";
[store createTableWithName:tableName];
//3.寫入數(shù)據(jù)
NSString *key = @"1";
NSDictionary *user = @{@"id": @1, @"name": @"tangqiao", @"age": @30};
[store putObject:user withId:key intoTable:tableName];
//4.讀取數(shù)據(jù)
NSDictionary *queryUser = [store getObjectById:key fromTable:tableName];
NSLog(@"query data result: %@", queryUser);
  • 打開(創(chuàng)建)數(shù)據(jù)庫

默認創(chuàng)建在Document路徑下。
若打開的數(shù)據(jù)庫不存在則創(chuàng)建。

// 打開名為test.db的數(shù)據(jù)庫,如果該文件不存在,則創(chuàng)新一個新的。
YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"test.db"];
  • 創(chuàng)建數(shù)據(jù)庫表

若創(chuàng)建的表存在則忽略。

YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"test.db"];
NSString *tableName = @"user_table";
// 創(chuàng)建名為user_table的表,如果已存在,則忽略該操作
[store createTableWithName:tableName];
  • 讀寫數(shù)據(jù)

通過Key-Value來讀寫數(shù)據(jù)庫表中數(shù)據(jù)。
Value支持類型:NSString, NSNumber, NSDictionary,NSArray。

//寫入數(shù)據(jù)
- (void)putString:(NSString *)string withId:(NSString *)stringId intoTable:(NSString *)tableName;
- (void)putNumber:(NSNumber *)number withId:(NSString *)numberId intoTable:(NSString *)tableName;
- (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName;
//讀取數(shù)據(jù)
- (NSString *)getStringById:(NSString *)stringId fromTable:(NSString *)tableName;
- (NSNumber *)getNumberById:(NSString *)numberId fromTable:(NSString *)tableName;
- (id)getObjectById:(NSString *)objectId fromTable:(NSString *)tableName;
  • 刪除數(shù)據(jù)
// 清除數(shù)據(jù)表中所有數(shù)據(jù)
- (void)clearTable:(NSString *)tableName;
// 刪除指定key的數(shù)據(jù)
- (void)deleteObjectById:(NSString *)objectId fromTable:(NSString *)tableName;
// 批量刪除一組key數(shù)組的數(shù)據(jù)
- (void)deleteObjectsByIdArray:(NSArray *)objectIdArray fromTable:(NSString *)tableName;
// 批量刪除所有帶指定前綴的數(shù)據(jù)
- (void)deleteObjectsByIdPrefix:(NSString *)objectIdPrefix fromTable:(NSString *)tableName;
  • 其他

YTKKeyValueItem類帶有createdTime字段,可以獲得該條數(shù)據(jù)的插入(或更新)時間,以便上層做復雜的處理(例如用來做緩存過期邏輯)。

// 獲得指定key的數(shù)據(jù)
- (YTKKeyValueItem *)getYTKKeyValueItemById:(NSString *)objectId fromTable:(NSString *)tableName;
// 獲得所有數(shù)據(jù)
- (NSArray *)getAllItemsFromTable:(NSString *)tableName;

YTKKeyValueStore參考文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容