ios缓存系列问题

我记得当时在学ios时,真心是想知道缓存问题  那缓存问题是什么问题呢?

怎么创建缓存?   缓存是以什么形式存在着?  缓存应该是怎么样的活动轨迹?  缓存如何存在 ?  缓存如何消除?  缓存是手机内置的机制吗?

以上问题都是我当时学习时的问题,然后结余面子也不好意思问,时间越久就越不好意思问,一直希望自己能成为一个大神级别的人物,虽然我知道自己一直很菜  很菜!

那么简单的来介绍以上的问题:

1.怎么创建缓存? ---我一直以为调用ios的方法   但是我现在发现我错了    都是自己的创建的文件

2.缓存是以什么形式存在着?---说的更加直接明白点,就是创建文件,然后文件利用字典  数组  把这些问题存起来   然后下次就成了缓存了

3.缓存应该是怎么样的活动轨迹?---更简单了   有了新数据就直接缓存  直接存到文件夹  把所有的数据都放到缓存的文件夹中

4.缓存如何存在? -----和上面的2条类似   就是有需要的话 就直接从缓存中调用  缓存的格式:1个数组 里面好多的字典(好多的分项)然后每个分项里面都是存的数组字典

5.缓存如何消除? -----这个消除就是不使用时,或者是不存在数据时,直接删除文件。预示着缓存的消失

6.缓存是手机内置的机制吗? ----不是手机内置的机制  就是一个文件夹   就是对文件夹的操作  然后对文件夹 增删改查


简单的来点代码:

看看怎么使用的哈  (主要是面对想我这个类似的装B小菜   想问又不好意思问的情况)

成了  直接来代码吧!

这是来个缓存数组

// 存储帮一帮首页的动态列表
@property (nonatomic , retain) NSMutableArray      * helpHomePageList;

//创建所有文件路径
- (void)createFilePath
{
    //文件是否存在
    if ([FileTools fileExists:[FileTools besideRootPath]])
    {
        NSString *helpFilePath = [FileTools helpFilePath];
        if (![FileTools fileExists:helpFilePath])
        {
            //创建文件路径
            [FileTools createFilePath:helpFilePath];
        }
    }
    
    //帮一帮的缓存文件路径 存在
    if ([FileTools fileExists:[FileTools helpFilePath]])
    {
        // 帮一帮发送草稿
        NSString *helpSendingFilePath = [FileTools helpSendingFilePath];
        //发送草稿不存在  重新创建 且保存
        if (![FileTools fileExists:helpSendingFilePath])
        {
            NSMutableDictionary *helpSendingDict = [[NSMutableDictionary alloc] initWithCapacity:1];
            [FileTools saveFile:helpSendingDict toPath:helpSendingFilePath];
            [helpSendingDict release];
        }
        
        // 帮一帮列表
        NSString *helpListFilePath = [FileTools helpListFilePath];
        //如果不存在帮一帮列表
        if (![FileTools fileExists:helpListFilePath])
        {
            NSMutableDictionary *helpListDict = [[NSMutableDictionary alloc] init];
            // 帮一帮首页列表
            [helpListDict setObject:[NSMutableArray array] forKey:kHelpHomePageList];
            //按路径保存文件
            [FileTools saveFile:helpListDict toPath:helpListFilePath];
            [helpListDict release];
        }
    }
}
//读取文件
- (void)load
{
    //读取帮一帮列表
    NSMutableDictionary *tmpDic = [NSMutableDictionary dictionaryWithContentsOfFile:[FileTools helpListFilePath]];
    //如果数组不为空 则赋值给wholeHelpListDic
    if (tmpDic && [tmpDic count] > 0)
    {
        self.wholeHelpListDic = tmpDic;
    }
    
    
    
    NSMutableArray *tmpArray = nil;
    //读取帮一帮首页的动态列表
    tmpArray = [self.wholeHelpListDic objectForKey:kHelpHomePageList];
    if (tmpArray && [tmpArray count] > 0)
    {
        helpHomePageList = [[NSMutableArray alloc] initWithCapacity:0];
        //判断是否是这个类或者这个类的子类的实例
        if([tmpArray isKindOfClass:[NSMutableArray class]] || [tmpArray isKindOfClass:[NSArray class]])
        {
            //赋值给helpHomePageList
            [helpHomePageList addObjectsFromArray:tmpArray];
        }
        else
        {
            //重新创建helpHomePageList数组
            [self.wholeHelpListDic setObject:[NSMutableArray array] forKey:kHelpHomePageList];
            //按路径保存文件
            [FileTools saveFile:self.wholeHelpListDic toPath:[FileTools helpListFilePath]];
        }
    }
}

//保存帮一帮首页的动态列表  type:两种状态 上拉更新 下拉加载
- (void)saveHomePageList:(NSDictionary *)respondDic state:(enum EUpdateType)type
{
    //如果respondDic为空或者为0 则返回空
    if(respondDic == nil || [respondDic count] == 0)
        return;
    //从字典中获取data数据
    NSArray *dataArray = [respondDic objectForKey:kJsonContentKey];
    
    //判断是更新还是加载
    if(type == kGetNewData)
    {//更新
        //移除之前的数据源
        [self.helpHomePageList removeAllObjects];
        //如果dataArray不为空或者大于0
        if (dataArray != nil && [dataArray count] > 0)
        {
            //将dataArray添加到帮一帮首页的动态列表
            [self.helpHomePageList addObjectsFromArray:dataArray];
        }
        //帮一帮首页的动态列表 不为空
        if(self.helpHomePageList != nil)
        {
            //重新创建数组  并且加入到整个帮一帮所有list字典中 然后数组释放(防止空指针)
            NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.helpHomePageList];
            [self.wholeHelpListDic setObject:array forKey:kHelpHomePageList];
            [array release];
        }
        else
        {
            //加入到整个帮一帮所有list字典中
            [self.wholeHelpListDic setObject:[NSMutableArray array] forKey:kHelpHomePageList];
        }
        //按路径保存所有list字典文件
        [FileTools saveFile:self.wholeHelpListDic toPath:[FileTools helpListFilePath]];
    }
    else
    {//加载
        //dataArray不为空 大于0
        if (dataArray != nil && [dataArray count] > 0)
        {
            //将dataArray添加到帮一帮首页的动态列表
            [self.helpHomePageList addObjectsFromArray:dataArray];
        }
    }
    //最新消息添加到所有帮一帮列表的索引表
    [self updateAllHelpListIndexDic:self.helpHomePageList];
}

//更新最新的帮助数据
- (void)updateHelpList:(NSDictionary *)helpBroad
{
    if (helpBroad == nil || [helpBroad count] == 0)
    {
        return;
    }
    //获取帮助动态字典中获取帮助HidId
    NSString *hidId = [SNSHelpBroadMethods getHidId:helpBroad];
    if(hidId == nil || [hidId longLongValue] == 0)
        return;
    
    //更新帮一帮主页列表
    for (NSUInteger i = 0; i < [self.helpHomePageList count]; i++)
    {
        NSDictionary * dic = [self.helpHomePageList objectAtIndex:i];
        //获取帮助动态字典中获取帮助HidId
        NSString *dicHidId = [SNSHelpBroadMethods getHidId:dic];
        if([hidId longLongValue] == [dicHidId longLongValue])
        {
            /*
             replaceObjectAtIndex 方法可以修改数组中的某一个元素
             第一个参数为 索引的id
             第二个参数为修改对象的指针
             */
            [self.helpHomePageList replaceObjectAtIndex:i withObject:helpBroad];
            break;
        }
    }
    
    
    // 帮一帮首页的动态列表
    BOOL fileNeedModify = NO;
    NSMutableArray *homeListArray = [self.wholeHelpListDic objectForKey:kHelpHomePageList];
    if (homeListArray != nil && [homeListArray count] > 0)
    {
        for (NSUInteger i = 0; i < [homeListArray count]; i++)
        {
            NSDictionary * dic = [homeListArray objectAtIndex:i];
            NSString *dicHidId = [SNSHelpBroadMethods getHidId:dic];
            if([hidId longLongValue] == [dicHidId longLongValue])
            {
                fileNeedModify = YES;
                [homeListArray replaceObjectAtIndex:i withObject:helpBroad];
                [self.wholeHelpListDic setObject:homeListArray forKey:kHelpHomePageList];
                break;
            }
        }
    }
    
    
    if(fileNeedModify)
    {
        //按文件路径保存数组
        [FileTools saveFile:self.wholeHelpListDic toPath:[FileTools helpListFilePath]];
    }
}
//保存新的发送帮助动态
- (void)saveNewSendHelpBroad:(NSDictionary *)helpBroad
{
    if (helpBroad == nil || [helpBroad count] == 0)
    {
        return;
    }
    NSString *hidId = [SNSHelpBroadMethods getHidId:helpBroad];
    if(hidId == nil)
        return;
    
    //当存在首页的动态列表时 角标为0 没有动态时 直接加上
    if([self.helpHomePageList count] > 0)
    {
        //当存在首页的动态列表时  添加到最新的数据上(角标为0)
        [self.helpHomePageList insertObject:helpBroad atIndex:0];
    }
    else
    {
        //没有首页的动态列表时  添加到第一条数据
        [self.helpHomePageList addObject:helpBroad];
    }
    
    
    //根据hidId添加到所有的帮助列表动态索引表中
    [self.allHelpListBroadIndexDic setObject:helpBroad forKey:hidId];
    
    //在所有的帮助列表中 获取首页的动态列表
    NSMutableArray *homeListArray = [self.wholeHelpListDic objectForKey:kHelpHomePageList];
    if (homeListArray == nil)
    {
        homeListArray = [[[NSMutableArray alloc] initWithCapacity:0]autorelease];
    }
    if([homeListArray count] > 0)
    {
        [homeListArray insertObject:helpBroad atIndex:0];
    }
    else
    {
        [homeListArray addObject:helpBroad];
    }
    
    
    
    //所有帮一帮列表的动态索引表 添加到 所有的帮助列表中
    [self.wholeHelpListDic setObject:self.allHelpListBroadIndexDic forKey:kAllHelpListBroadIndexDic];
    
    //按路径保存文件
    [FileTools saveFile:self.wholeHelpListDic toPath:[FileTools helpListFilePath]];
}

// 获取存储帮一帮首页的动态列表
- (NSMutableArray *)getHelpHomePageListData
{
    NSMutableArray *array = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
    [array addObjectsFromArray:helpHomePageList];
    return array;
}
// 获取存储所有帮一帮列表的动态索引表
- (NSMutableDictionary *)getHelpListBroadDic:(NSString *)broadHidId
{
    if(![broadHidId isKindOfClass:[NSString class]] && ![broadHidId isKindOfClass:[NSNumber class]])
    {
        return nil;
    }
    if(broadHidId == nil || [broadHidId longLongValue] == 0)
        return nil;
    NSString *hidId = [NSString stringWithFormat:@"%@", broadHidId];
    NSDictionary * helpDic = [allHelpListBroadIndexDic objectForKey:hidId];
    NSMutableDictionary *dic = [[[NSMutableDictionary alloc] initWithDictionary:helpDic] autorelease];
    return dic;
}

因为已经看过2天了   印象不是特别清楚了   但是简单的缓存肯定是在这里了   无非就是没有什么删除文件类似的东西  但是整体的架构已经出来了      到时候用的时候,在稍微的加一学习
应该就可以了哈 

各位  ios未来的大牛们   都需要加油哈 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值