iOS手动实现缓存机制(以FMDB实现)


以对象模型进行缓存(推荐使用)

使用过程中,需要对模型进行归档处理,NSConding才可以序列化成NSData ,可以使MJExtenison中封装好的宏定义

MJCodingImplementation

#import <Foundation/Foundation.h>
@interface NSObject (MJCoding)
/**
 *  解码(从文件中解析对象)
 */
- (void)decode:(NSCoder *)decoder;
/**
 *  编码(将对象写入文件中)
 */
- (void)encode:(NSCoder *)encoder;
@end
/**
 归档的实现
 */
#define MJCodingImplementation \
- (id)initWithCoder:(NSCoder *)decoder \
{ \
if (self = [super init]) { \
[self decode:decoder]; \
} \
return self; \
} \
\
- (void)encodeWithCoder:(NSCoder *)encoder \
{ \
[self encode:encoder]; \
}



具体实现内容:

首先需要新建一个模型进行



<span style="font-family:Microsoft YaHei;font-size:14px;">//
//  IWStatusCacheTool.m
//  ItcastWeibo
//
//  Created by apple on 14-5-23.
//  Copyright (c) 2014年 itcast. All rights reserved.
//
#import "IWStatusCacheTool.h"
#import "IWAccount.h"
#import "IWAccountTool.h"
#import "IWStatus.h"
#import "FMDB.h"
@implementation IWStatusCacheTool
static FMDatabaseQueue *_queue;
+ (void)initialize
{
    // 0.获得沙盒中的数据库文件名
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"statuses.sqlite"];
    
    // 1.创建队列
    _queue = [FMDatabaseQueue databaseQueueWithPath:path];
    
    // 2.创表,对象 使用blob类型
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"create table if not exists t_status (id integer primary key autoincrement, access_token text, idstr text, status blob);"];
    }];
}
+ (void)addStatuses:(NSArray *)statusArray
{
    for (IWStatus *status in statusArray) {
        [self addStatus:status];
    }
}
+ (void)addStatus:(IWStatus *)status
{
    [_queue inDatabase:^(FMDatabase *db) {
        // 1.获得需要存储的数据
        NSString *accessToken = [IWAccountTool account].access_token;
        NSString *idstr = status.idstr;</span>
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="white-space:pre">	</span>//需要使用进行归档处理后才可以将对象模型进行序列化成NSData类型,方法见首行
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:status];
        
        // 2.存储数据
        [db executeUpdate:@"insert into t_status (access_token, idstr, status) values(?, ? , ?)", accessToken, idstr, data];
    }];
}</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">//提供获取缓存的方法,以对象模型数组的方式进行返回
+ (NSArray *)statuesWithParam:(IWHomeStatusesParam *)param
{
    // 1.定义数组
    __block NSMutableArray *statusArray = nil;
    
    // 2.使用数据库
    [_queue inDatabase:^(FMDatabase *db) {
        // 创建数组
        statusArray = [NSMutableArray array];
        
        // accessToken
        NSString *accessToken = [IWAccountTool account].access_token;
        
        FMResultSet *rs = nil;
        rs = [db executeQuery:@"select * from t_status where access_token = 10"]
        
        while (rs.next) {</span>
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="white-space:pre">	</span>//进入该循环则说明找到符合条件的记录
            NSData *data = [rs dataForColumn:@"status"];</span>
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="white-space:pre">		</span>//进行解码,以此获取数据复制到对象模型中
            IWStatus *status = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            [statusArray addObject:status];
        }
    }];
    
    // 3.返回数据
    return statusArray;
}
@end
</span>

通过在controller中的http请求前进行判断是否有缓存

如果有缓存则读取缓存

<span style="font-family:Microsoft YaHei;font-size:14px;">[IWStatus  statuesWithParam:param];</span>

如果没有缓存则进行http请求

在进行http请求之后需要将数据以面向模型的形式放进数据库中进行缓存

<span style="font-family:Microsoft YaHei;font-size:14px;">[IWStatus addStatus:status];</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值