利用Core Data 的缓存机制

来源:互联网 发布:韩国冒险岛 数据库 编辑:程序博客网 时间:2024/05/22 03:22

创建缓存的数据库

  

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
原创粉丝点击