利用Core Data 的缓存机制

创建缓存的数据库

  

EpointCacheDB.h
#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface EpointCacheDB : NSObject
{
    sqlite3 *database;
}

//获取SQLITE3数据库对象
-(sqlite3*)getCacheDB;

//获取数据库路径
+(NSString *)getDBFilePath;

+(sqlite3*)getFrameDB;
@end
EpointCacheDB.m
#import "EpointCacheDB.h"

@implementation EpointCacheDB

-(id)init
{
    self = [super init];
    if (self) {
        sqlite3_open([[EpointCacheDB getDBFilePath] UTF8String],&database);
        [self createTable:"create table if not exists framecache(key text,value text)"];
        [self createTable:"create table if not exists webinfocache(InfoGuid text,txtTitle text,Content text,PostUserName text,PostDate text,InfoProperty text,IsRead text,HasAttach text,FeedBackCount text)"];
    }
    return self;
}

//建表操作
-(void)createTable:(char *)sql
{
    char *errorMsg;
    sqlite3_exec(database, sql, NULL, NULL, &errorMsg);
    if (errorMsg!=NULL) {
        NSLog(@"ERR:%s",errorMsg);
    }
}

+(NSString *)getDBFilePath
{
    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dbFilePath = [[documentsPaths objectAtIndex:0]stringByAppendingPathComponent:@"EpointCacheDB"];
    return dbFilePath;
}

-(sqlite3*)getCacheDB
{
    return database;
}

+(sqlite3*)getFrameDB
{
    sqlite3 *framedb;
    sqlite3_open([[EpointCacheDB getDBFilePath] UTF8String],&framedb);
    return framedb;
}
@end

#import <Foundation/Foundation.h>
#import <sqlite3.h>
#import "EpointFrameDB.h"

创建缓存数据库操作文件
DBCache.h
#import <Foundation/Foundation.h>
#import <sqlite3.h>
#import "EpointCacheDB.h"


@interface DBCache : NSObject

+(void)insertCache:(NSString *)keyValue withContent:(NSString *)content;
+(NSString *)getCache:(NSString*)key;
+(void)clearCache;
@end

DBCache.m
#import "DBCache.h"

@implementation DBCache
///操作cache表///
//插入数据
+(void)insertCache:(NSString *)keyValue withContent:(NSString *)content
{
    sqlite3 *database = [EpointCacheDB getFrameDB];
    
    if([content rangeOfString:@"'"].location!=NSNotFound){
        content=[content stringByReplacingOccurrencesOfString:@"'" withString:@"\""];
    }
    char *errorMsg;
    NSString *msInsertSql = [NSString stringWithFormat:@"insert into framecache values('%@','%@')",keyValue,content];
    
    NSString *delete = [NSString stringWithFormat:@"delete from framecache where key = '%@' ",keyValue];
    sqlite3_exec(database,[delete UTF8String],NULL,NULL,&errorMsg);
    if (sqlite3_exec(database, [msInsertSql UTF8String], NULL, NULL, &errorMsg)==SQLITE_OK) {
//        NSLog(@"缓存成功!");
    }else{
        NSLog(@"缓存失败!");
    }
    [self dealErrorMessage:errorMsg];
}

//获取缓存值(根据key)
+(NSString *)getCache:(NSString*)key
{
    
    sqlite3 *database = [EpointCacheDB getFrameDB];
NSString *selectSql=[NSString stringWithFormat:@"select value from framecache where key = '%@'",key]; sqlite3_stmt *statement; NSString *nsvalue = @""; if (sqlite3_prepare_v2(database, [selectSql UTF8String], -1, &statement, nil)==SQLITE_OK) { NSLog(@"Query OK"); } while (sqlite3_step(statement)==SQLITE_ROW) { char *value=(char *)sqlite3_column_text(statement, 0); nsvalue = [NSString stringWithUTF8String:value]; } sqlite3_finalize(statement); return nsvalue;}//清空缓存+(void)clearCache{ sqlite3 *database = [EpointCacheDB getFrameDB];
char *errorMsg; NSString *sql = [NSString stringWithFormat:@"delete from framecache"]; if(sqlite3_exec(database,[sql UTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK){ NSLog(@"清空失败!"); NSAssert1(NO,@"删除失败, %s", errorMsg); }else{// NSLog(@"清空成功!"); } }+(void)dealErrorMessage:(char *)errorMsg{ if (errorMsg!=NULL) { NSLog(@"err:%s",errorMsg); }}@end


接下来就可以进行读取,删除缓存操作

以下附IOS机制详细的解释地址:http://blog.csdn.net/wbw1985/article/details/19989709

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值