美文网首页
NSCache的使用

NSCache的使用

作者: 浪淘沙008 | 来源:发表于2018-05-14 16:24 被阅读35次

- (nullableObjectType)objectForKey:(KeyType)key;  //根据key取出某条数据

- (void)setObject:(ObjectType)obj forKey:(KeyType)key;// 0 cost    //添加某条数据

- (void)setObject:(ObjectType)obj forKey:(KeyType)key cost:(NSUInteger)g;  //添加某条数据并设置其占用的资源数目

- (void)removeObjectForKey:(KeyType)key;    //删除某条数据

- (void)removeAllObjects;  //删除所有数据

@property NSUInteger totalCostLimit; // 设置缓存大小限制

@property NSUInteger countLimit; // 设置总条数的限制

@property BOOL evictsObjectsWithDiscardedContent;   //是否自动舍弃那些内存已经被丢弃的对象

- (void)cache:(NSCache *)cache willEvictObject:(id)obj;   //代理方法当对象呗删除时调用


//将对象数组保存到cache中

- (void)addDataArr:(NSArray*)dataArr modelStr:(NSString*)str {

    if(dataArr.count==0)return;

    for(NSIntegeri =0; i < dataArr.count; i++) {

        [self.cache setObject:dataArr[i] forKey:[NSString stringWithFormat:@"%@%ld", str,i] cost:1]; 

    }

}

//根据对应的字符串取出缓存数据

- (NSArray*)getDataWithModelStr:(NSString*)str {

    NSMutableArray * dataArr = @[].mutableCopy;

    for(NSIntegeri =0; i <self.cache.countLimit; i++) {

        NSObject * model = [_cache objectForKey:[NSString stringWithFormat:@"%@%ld", str,i]];

        if(model) {

            [dataArraddObject:model];

        }else{

            break;

        }

    }

    returndataArr;

}

//根据key值删除对应的缓存数据

- (void)removeObjectForKey:(NSString*)key

{

    for(NSIntegeri =0; i

        id model = [_cache objectForKey:[NSString stringWithFormat:@"%@%ld", key,i]];

        if(model) {

            [self.cache removeObjectForKey:[NSString stringWithFormat:@"%@%ld", key,i]];

        }else{

            return;

        }

    }

}

//删除所有数据

- (void)removeAllObjects

{

    [self.cache removeAllObjects];

}

相关文章

  • NSCache内存缓存

    NSCache 基本使用 NSCache缓存类介绍 NSCache源码

  • 了解NSCache的基本使用

    NSCache是专门用来进行缓存处理的, NSCache简单介绍:NSCache是苹果官方提供的缓存类,具体使用和...

  • 我们应该使用的NSCache(链接)

    我们应该使用的NSCache

  • iOS开发之NSCache

    NSCache的特点 NSCache是苹果推出专门用来处理内存缓存的类;NSCache默认是线程安全的,在使用的时...

  • NSCache

    NSCache类 NSCacheDelegate协议 一、NSCache特点 1、使用方便,类似字典 2、线程安全...

  • NSCache简介

    NSCache苹果提供的一套缓存机制,当今主流的SDWebImage正是使用了NSCache进行缓存 相对比使用N...

  • NSCache

    NSCache简单说明 1.NSCache是苹果官方提供的缓存类,具体使用和NSMutableDictionary...

  • NSCache的使用

    - (nullableObjectType)objectForKey:(KeyType)key; //根据key取...

  • NSCache

    NSCache 介绍 NSCache 是苹果提供的一个专门用来做缓存的类 使用和 NSMutableDiction...

  • NSCache

    NSCache 介绍 NSCache 是苹果提供的一个专门用来做缓存的类 使用和 NSMutableDiction...

网友评论

      本文标题:NSCache的使用

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