利用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;@endDBCache.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
- 利用Core Data 的缓存机制
- ios-客户端的缓存机制和Core Data
- IOS缓存机制详解 NSCoding协议方法 Core Data 原始的SQLite
- 利用cookie 测试浏览器的缓存机制
- Core Data的使用
- Core Data的使用
- Core Data的使用
- Core Data的使用
- Core Data 的用法
- core data的使用
- Core Data 的使用
- Core Data 的使用
- core data的学习
- Core Data的使用
- Core Data的使用
- Core Data的理解
- Core Data的理解
- Core Data的理解
- 汉子统计(杭电2030)
- Android:Layout_weight的深刻理解
- <INPUT标签的事件和value设置
- 一款APK,集成Lighttpd、phpMyAdmin、Drupal7、ownCloud等等工具
- java二叉查找树
- 利用Core Data 的缓存机制
- poj 3281 Dining 最大网络流
- Linux下查询CPU 缓存的工具
- 递归—简单背包问题
- HDU 1022 Train Problem I (数据结构 —— 栈)
- iOS后台定位实现
- Zookeeper原理及应用场景
- 关于static ,const 的一些小回顾
- 给定字符串,删除开始和结尾处的空格,并将中间的多个连续的空格合并成一个