美文网首页
缓存模型到数据库中

缓存模型到数据库中

作者: huicuihui | 来源:发表于2018-01-02 15:35 被阅读7次

缓存使用的数据库。使用第三方FMDB。
创建一个继承自NSObject类的文件去封装管理缓存的操作。
增删改查。
数据模型: NEWModel
1:保存数据到数据库中。

- (void) saveNews:(NEWModel *) news{
    
    NSMutableString * query = [NSMutableString stringWithFormat:@"INSERT INTO newsTB"];
    NSMutableString * keys = [NSMutableString stringWithFormat:@" ("];
    NSMutableString * values = [NSMutableString stringWithFormat:@" ( "];
    NSMutableArray * arguments = [NSMutableArray arrayWithCapacity:5];
    
//    [keys appendString:@"hasRead,"];
//    [values appendString:@"?,"];
//    [arguments addObject:@(0)];
    
    if (news.newsDetail) {
        [keys appendString:@"newsDetail,"];
        [values appendString:@"?,"];
        [arguments addObject:news.newsDetail];
    }
    if (news.articleID) {
        [keys appendString:@"articleID,"];
        [values appendString:@"?,"];
        [arguments addObject:news.articleID];
    }
    if (news.title) {
        [keys appendString:@"title,"];
        [values appendString:@"?,"];
        [arguments addObject:news.title];
    }
    if (news.createDate) {
        [keys appendString:@"createDate,"];
        [values appendString:@"?,"];
        [arguments addObject:news.createDate];
    }
    if (news.editDate) {
        [keys appendString:@"editDate,"];
        [values appendString:@"?,"];
        [arguments addObject:news.editDate];
    }
    if (news.topDate) {
        [keys appendString:@"topDate,"];
        [values appendString:@"?,"];
        [arguments addObject:news.topDate];
    }
    if (news.modifyedDate) {
        [keys appendString:@"modifyedDate,"];
        [values appendString:@"?,"];
        [arguments addObject:news.modifyedDate];
    }
    if (news.tag) {
        [keys appendString:@"tag,"];
        [values appendString:@"?,"];
        [arguments addObject:news.tag];
    }
    if (news.author) {
        [keys appendString:@"author,"];
        [values appendString:@"?,"];
        [arguments addObject:news.author];
    }
    if (news.commentTimes) {
        [keys appendString:@"commentTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.commentTimes];
    }
    if (news.createBy) {
        [keys appendString:@"createBy,"];
        [values appendString:@"?,"];
        [arguments addObject:news.createBy];
    }
    
    if (news.dispType) {
        [keys appendString:@"dispType,"];
        [values appendString:@"?,"];
        [arguments addObject:news.dispType];
    }
    if (news.modifiedBy) {
        [keys appendString:@"modifiedBy,"];
        [values appendString:@"?,"];
        [arguments addObject:news.modifiedBy];
    }
    if (news.picOne) {
        [keys appendString:@"picOne,"];
        [values appendString:@"?,"];
        [arguments addObject:news.picOne];
    }
    if (news.picTwo) {
        [keys appendString:@"picTwo,"];
        [values appendString:@"?,"];
        [arguments addObject:news.picTwo];
    }
    if (news.picThree) {
        [keys appendString:@"picThree,"];
        [values appendString:@"?,"];
        [arguments addObject:news.picThree];
    }
    if (news.realLikeTimes) {
        [keys appendString:@"realLikeTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.realLikeTimes];
    }
    if (news.realReadTimes) {
        [keys appendString:@"realReadTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.realReadTimes];
    }
    if (news.startLikeTimes) {
        [keys appendString:@"startLikeTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.startLikeTimes];
    }
    if (news.startReadTimes) {
        [keys appendString:@"startReadTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.startReadTimes];
    }
    if (news.source) {
        [keys appendString:@"source,"];
        [values appendString:@"?,"];
        [arguments addObject:news.source];
    }
    if (news.topFlag) {
        [keys appendString:@"topFlag,"];
        [values appendString:@"?,"];
        [arguments addObject:news.topFlag];
    }
    if (news.showTime) {
        [keys appendString:@"showTime,"];
        [values appendString:@"?,"];
        [arguments addObject:news.showTime];
    }
    if (news.isTopNews) {
        [keys appendString:@"isTopNews,"];
        [values appendString:@"?,"];
        [arguments addObject:news.isTopNews];
    }
    if (news.isAccountingNews) {
        [keys appendString:@"isAccountingNews,"];
        [values appendString:@"?,"];
        [arguments addObject:news.isAccountingNews];
    }
    if (news.isTaxmanageNews) {
        [keys appendString:@"isTaxmanageNews,"];
        [values appendString:@"?,"];
        [arguments addObject:news.isTaxmanageNews];
    }
    
    [keys appendString:@")"];
    [values appendString:@")"];
    [query appendFormat:@" %@ VALUES%@;",
     [keys stringByReplacingOccurrencesOfString:@",)" withString:@")"],
     [values stringByReplacingOccurrencesOfString:@",)" withString:@")"]];
    NSLog(@"%@",query);
    BOOL isSucess =     [_db executeUpdate:query withArgumentsInArray:arguments];
    if (!isSucess) {
        NSLog(@"插入财税头条数据失败");
    }
    
}

2:修改是否是已经读过的属性。其他的都不变,只修改这一个属性的话,可以只更新该条数据的这一个字段:

- (void) mergeWithArticleID:(NEWModel *) news{
    if (!news.articleID) {
        return;
    }
    [_db open];
    NSString * query = @"UPDATE newsTB SET";
    NSMutableString * temp = [NSMutableString stringWithCapacity:20];
    
    [temp appendFormat:@" hasRead = '%@'",@(1)];

    query = [query stringByAppendingFormat:@"%@ WHERE articleID = '%@'",[temp stringByReplacingOccurrencesOfString:@",)" withString:@""],news.articleID];
    NSLog(@"%@",query);
    
    if ([_db executeUpdate:query]) {
        NSLog(@"更新成功");
    }
    [_db close];
}

注:
一定要测试一下executeUpdate是否返回YES,不是的话就要去查看是否是sql语句最后多一个,的问题。 [temp appendFormat:@" hasRead = '%@'",@(1)];

网络请求成功回来数据之后,需要把本地数据库缓存的数据也都更新了:

- (void) mergeWithNews:(NEWModel *) news{
    if (!news.articleID) {
        return;
    }
    NSString * query = @"UPDATE newsTB SET";
    NSMutableString * temp = [NSMutableString stringWithCapacity:20];
    // xxx = xxx;
    if (news.newsDetail) {
        [temp appendFormat:@" newsDetail = '%@',",news.newsDetail];
    }
    
    if (news.articleID) {
        [temp appendFormat:@" articleID = '%@',",news.articleID];
    }
    if (news.title) {
        [temp appendFormat:@" title = '%@',",news.title];
    }
    if (news.createDate) {
        [temp appendFormat:@" createDate = '%@',",news.createDate];
    }
    if (news.editDate) {
        [temp appendFormat:@" editDate = '%@',",news.editDate];
        
    }
    if (news.topDate) {
        [temp appendFormat:@" topDate = '%@',",news.topDate];
    }
    if (news.modifyedDate) {
        [temp appendFormat:@" modifyedDate = '%@',",news.modifyedDate];
    }
    if (news.tag) {
        [temp appendFormat:@" tag = '%@',",news.tag];
    }
    if (news.author) {
        [temp appendFormat:@" author = '%@',",news.author];
    }
    if (news.commentTimes) {
        [temp appendFormat:@" commentTimes = '%@',",news.commentTimes];
    }
    if (news.createBy) {
        [temp appendFormat:@" createBy = '%@',",news.createBy];
    }
    
    if (news.dispType) {
        [temp appendFormat:@" dispType = '%@',",news.dispType];
    }
    if (news.modifiedBy) {
        [temp appendFormat:@" modifiedBy = '%@',",news.modifiedBy];
    }
    if (news.picOne) {
        [temp appendFormat:@" picOne = '%@',",news.picOne];
    }
    if (news.picTwo) {
        [temp appendFormat:@" picTwo = '%@',",news.picTwo];
    }
    if (news.picThree) {
        [temp appendFormat:@" picThree = '%@',",news.picThree];
    }
    if (news.realLikeTimes) {
        [temp appendFormat:@" realLikeTimes = '%@',",news.realLikeTimes];
    }
    if (news.realReadTimes) {
        [temp appendFormat:@" realReadTimes = '%@',",news.realReadTimes];
    }
    if (news.startLikeTimes) {
        [temp appendFormat:@" startLikeTimes = '%@',",news.startLikeTimes];
        
    }
    if (news.startReadTimes) {
        [temp appendFormat:@" startReadTimes = '%@',",news.startReadTimes];
    }
    if (news.source) {
        [temp appendFormat:@" source = '%@',",news.source];
    }
    if (news.topFlag) {
        [temp appendFormat:@" topFlag = '%@',",news.topFlag];
    }
    if (news.showTime) {
        [temp appendFormat:@" showTime = '%@',",news.showTime];
    }
    if (news.isTopNews) {
        [temp appendFormat:@" isTopNews = '%@',",news.isTopNews];
    }
    if (news.isAccountingNews) {
        [temp appendFormat:@" isAccountingNews = '%@',",news.isAccountingNews];
    }
    if (news.isTaxmanageNews) {
        [temp appendFormat:@" isTaxmanageNews = '%@',",news.isTaxmanageNews];
    }
    
    NSString *sqlStr = [temp substringToIndex:temp.length - 1];
    query = [query stringByAppendingFormat:@"%@ WHERE articleID = '%@'",[sqlStr stringByReplacingOccurrencesOfString:@",)" withString:@""],news.articleID];
    
    if (![_db executeUpdate:query]) {
        NSLog(@"更新失败");
    }
}

删除某一条数据

- (void) deleteNewsWithArticleID:(NSString *) articleID{
    NSString * query = [NSString stringWithFormat:@"DELETE FROM newsTB WHERE articleID = '%@'",articleID];
    [_db executeUpdate:query];
}

相关文章

  • 缓存模型到数据库中

    缓存使用的数据库。使用第三方FMDB。创建一个继承自NSObject类的文件去封装管理缓存的操作。增删改查。数据模...

  • Redis(六)-缓存方案-一致性

    概述 本节学习下“缓存 + 数据库”模型读写的一致性问题,比如,缓存中是否有可能被写入脏数据,策略的读写性能如何,...

  • Redis 常见问题

    1、缓存雪崩:缓存中大面积的key同时失效,查询请求都打到数据库中;2、缓存穿透:查询的数据在缓存中不存在时,到数...

  • Redis缓存使用技巧

    缓存穿透 问题:每次请求直接穿过缓存,回源到数据库中,每次请求都会到DB层,造成数据库负担过大.方案: 采用blo...

  • TP框架生成数据库表字段与路由缓存提升性能

    1.字段缓存 TP框架中的模型每次获取数据库表字段都是通过SQL语句动态获取的,每使用模型执行操作都会多余执行一条...

  • MyBatis | 一级缓存与二级缓存

    一、什么是缓存? 缓存,合理使用缓存是优化中最常见的,将从数据库中查询出来的数据放入缓存中,下次使用时不必从数据库...

  • HTTP缓存

    一、强缓存 定义:当缓存数据库中已有所请求的数据时,客户端直接从数据库读取数据。当缓存数据库中没有所请求的数据...

  • 缓存穿透和缓存雪崩问题

    缓存穿透,即黑客故意去请求缓存中不存在的数据,导致所有的请求都怼到数据库上,从而数据库连接异常。 解决方案: (一...

  • 缓存穿透、缓存击穿、缓存雪崩

    一、缓存系统 1.1 缓存处理流程 前台请求,后台先从缓存中取数据,取到直接返回结果,取不到时从数据库中取,数据库...

  • 前端HTTP相关面试总结

    数据库缓存 CDN缓存 代理服务缓存 浏览器缓存 强缓存关键词expiresHTTP 1.0 字段到其时间,根据判...

网友评论

      本文标题:缓存模型到数据库中

      本文链接:https://www.haomeiwen.com/subject/werfnxtx.html