IOS缓存管理之YYCache使用详解

2020-01-21 00:45:44丽君

1.写入性能对比

YYCache


  //模拟数据
  NSString *value=@"I want to know who is lcj ?";
  //模拟一个key
  NSString *key=@"key";
  //YYCache
  YYCache *yyCache=[YYCache cacheWithName:@"LCJCache"];
  //写入数据
  CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
  [yyCache setObject:value forKey:key withBlock:^{
    CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();

    NSLog(@" yyCache async setObject time cost: %0.5f", end - start);
  }];

  CFAbsoluteTime start1 = CFAbsoluteTimeGetCurrent();
  [yyCache setObject:value forKey:key];
  CFAbsoluteTime end1 = CFAbsoluteTimeGetCurrent();
  NSLog(@" yyCache sync setObject time cost: %0.5f", end1 - start1);

运行结果

IOS,YYCache,IOS缓存管理

PINCache


   //PINCache
  //模拟数据
  NSString *value=@"I want to know who is lcj ?";
  //模拟一个key
  NSString *key=@"key";
  PINCache *pinCache=[PINCache sharedCache];
  //写入数据
  CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
  [pinCache setObject:value forKey:key block:^(PINCache * _Nonnull cache, NSString * _Nonnull key, id _Nullable object) {
    CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
    
    NSLog(@" pincache async setObject time cost: %0.5f", end - start);
  }];
  
  CFAbsoluteTime start1 = CFAbsoluteTimeGetCurrent();
  [pinCache setObject:value forKey:key];
  CFAbsoluteTime end1 = CFAbsoluteTimeGetCurrent();
  NSLog(@" pinCache sync setObject time cost: %0.5f", end1 - start1);

运行结果

IOS,YYCache,IOS缓存管理

通过上面的测试可以看出 同样大小的数据,无论同步方式还是异步方式,YYCache性能都要由于PINCache。

2.读取性能对比

YYCache


  YYCache *yyCache=[YYCache cacheWithName:@"LCJCache"];
  //模拟一个key
  NSString *key=@"key";
  CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
  //读取数据
  [yyCache objectForKey:key withBlock:^(NSString * _Nonnull key, id<NSCoding> _Nonnull object) {
    CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
    NSLog(@" yyCache async objectForKey time cost: %0.5f", end - start);
  }];

  CFAbsoluteTime start1 = CFAbsoluteTimeGetCurrent();
  [yyCache objectForKey:key];
  CFAbsoluteTime end1 = CFAbsoluteTimeGetCurrent();
  NSLog(@" yyCache sync objectForKey time cost: %0.5f", end1 - start1);

运行结果:

IOS,YYCache,IOS缓存管理

PINCache


 PINCache *pinCache=[PINCache sharedCache];
  //模拟一个key
  NSString *key=@"key";
  CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
  //读取数据
  [pinCache objectForKey:key block:^(PINCache * _Nonnull cache, NSString * _Nonnull key, id _Nullable object) {
    CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
    NSLog(@" pincache async objectForKey time cost: %0.5f", end - start);
  }] ;
  
  CFAbsoluteTime start1 = CFAbsoluteTimeGetCurrent();
  [pinCache objectForKey:key];
  CFAbsoluteTime end1 = CFAbsoluteTimeGetCurrent();
  NSLog(@" pinCache objectForKey time cost: %0.5f", end1 - start1);