NSCache

2019-09-30  本文已影响0人  Code_人生
  • NSCache类
@interface NSCache <KeyType, ObjectType> : NSObject {
@private
    id _delegate;
    void *_private[5];
    void *_reserved;
}

@property (copy) NSString *name;

@property (nullable, assign) id<NSCacheDelegate> delegate;

- (nullable ObjectType)objectForKey:(KeyType)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;    // limits are imprecise/not strict
@property NSUInteger countLimit;    // limits are imprecise/not strict
@property BOOL evictsObjectsWithDiscardedContent;

@end
@protocol NSCacheDelegate <NSObject>
@optional
- (void)cache:(NSCache *)cache willEvictObject:(id)obj;
@end

一、NSCache特点

    _cache = [[NSCache alloc] init];
    _cache.countLimit = 5;
    _cache.delegate = self;
    for (int i = 0; i < 9; i++) {
        [_cache setObject:[NSString stringWithFormat:@"dengyazhou%d",i] forKey:[NSString stringWithFormat:@"DYZ%d",i]];
    }
    for (int i = 0; i < 12; i++) {
        NSLog(@"Cache object:%@, at index :%d",[_cache objectForKey:[NSString stringWithFormat:@"DYZ%d",i]],i);
    }

// countLimit,先移除,后添加
2019-09-05 14:17:01.198612+0800 LGTest[36708:7543575] obj:dengyazhou0 will evict by Cache:<NSCache: 0x600002487080>
2019-09-05 14:17:01.198757+0800 LGTest[36708:7543575] obj:dengyazhou1 will evict by Cache:<NSCache: 0x600002487080>
2019-09-05 14:17:01.198853+0800 LGTest[36708:7543575] obj:dengyazhou2 will evict by Cache:<NSCache: 0x600002487080>
2019-09-05 14:17:01.198951+0800 LGTest[36708:7543575] obj:dengyazhou3 will evict by Cache:<NSCache: 0x600002487080>
2019-09-05 14:17:01.199050+0800 LGTest[36708:7543575] Cache object:(null), at index :0
2019-09-05 14:17:01.199116+0800 LGTest[36708:7543575] Cache object:(null), at index :1
2019-09-05 14:17:01.199195+0800 LGTest[36708:7543575] Cache object:(null), at index :2
2019-09-05 14:17:01.199274+0800 LGTest[36708:7543575] Cache object:(null), at index :3
2019-09-05 14:17:01.199346+0800 LGTest[36708:7543575] Cache object:dengyazhou4, at index :4
2019-09-05 14:17:01.199422+0800 LGTest[36708:7543575] Cache object:dengyazhou5, at index :5
2019-09-05 14:17:01.199493+0800 LGTest[36708:7543575] Cache object:dengyazhou6, at index :6
2019-09-05 14:17:01.199551+0800 LGTest[36708:7543575] Cache object:dengyazhou7, at index :7
2019-09-05 14:17:01.199616+0800 LGTest[36708:7543575] Cache object:dengyazhou8, at index :8
2019-09-05 14:17:01.199695+0800 LGTest[36708:7543575] Cache object:(null), at index :9
2019-09-05 14:17:01.199805+0800 LGTest[36708:7543575] Cache object:(null), at index :10
2019-09-05 14:17:01.199928+0800 LGTest[36708:7543575] Cache object:(null), at index :11
// app进入后台之后,会移除
2019-09-05 14:17:11.128147+0800 LGTest[36708:7543575] obj:dengyazhou5 will evict by Cache:<NSCache: 0x600002487080>
2019-09-05 14:17:11.128278+0800 LGTest[36708:7543575] obj:dengyazhou6 will evict by Cache:<NSCache: 0x600002487080>
2019-09-05 14:17:11.128396+0800 LGTest[36708:7543575] obj:dengyazhou7 will evict by Cache:<NSCache: 0x600002487080>
2019-09-05 14:17:11.128545+0800 LGTest[36708:7543575] obj:dengyazhou8 will evict by Cache:<NSCache: 0x600002487080>
2019-09-05 14:17:11.128667+0800 LGTest[36708:7543575] obj:dengyazhou4 will evict by Cache:<NSCache: 0x600002487080>

二、模拟内存警告

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dyz_didReceiveMemoryWaring:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
- (void)dyz_didReceiveMemoryWaring:(NSNotification *)notification{
    NSLog(@"notification----%@",notification);
    [_testPurgeableData endContentAccess];
}

三、NSDiscardableContent协议

@protocol NSDiscardableContent
@required
- (BOOL)beginContentAccess;
- (void)endContentAccess;
- (void)discardContentIfPossible;
- (BOOL)isContentDiscarded;
@end
@interface NSPurgeableData : NSMutableData <NSDiscardableContent> {
@private
    NSUInteger _length;
    int32_t _accessCount;
    uint8_t _private[32];
    void *_reserved;
}

@end
    UIImage *image = [UIImage imageNamed:@"logo"];;
    CFDataRef rawData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
    _testPurgeableData = [[NSPurgeableData alloc] initWithData:(__bridge NSData *)rawData];

    [_cache setObject:_testPurgeableData forKey:@"dyz5"];

- (void)dyz_didReceiveMemoryWaring:(NSNotification *)notification{
    NSLog(@"notification----%@",notification);
    [_testPurgeableData endContentAccess];
}
上一篇下一篇

猜你喜欢

热点阅读