在NSCache文档中苹果称
您可以从不同的线程,而不必自己锁定缓存。
简而言之,他们不在NSCache
上使用LOCK
。下面是它们的缓存实现。
它们有多种不同类型的缓存,唯一一种封装在LOCK/UNLOCK中的是weakCache
:
@property (nonatomic, strong, nonnull) NSMapTable<KeyType, ObjectType> *weakCache; // strong-weak cache
它是CCD_ 4的对象。NSMapTable
不是线程安全的:
// `setObject:forKey:` just call this with 0 cost. Override this is enough
- (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)g {
[super setObject:obj forKey:key cost:g];
if (!self.config.shouldUseWeakMemoryCache) {
return;
}
if (key && obj) {
// Store weak cache
LOCK(self.weakCacheLock);
[self.weakCache setObject:obj forKey:key];
UNLOCK(self.weakCacheLock);
}
}