美文网首页
NSCache 删除最老的还是最不常用的?

NSCache 删除最老的还是最不常用的?

作者: 派大星的博客 | 来源:发表于2018-08-28 15:25 被阅读11次
    NSCache的主要方法.png
    @interface AutoPurgeCache : NSCache
    @end
    //  自动清除缓存的Cache类
    @implementation AutoPurgeCache
    
    - (id)init
    {
        self = [super init];
        if (self) {
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
        }
        return self;
    }
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
    }
    
    @end
    
    @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
    
    // eg:  countLimit = 10; 队列先进先出移除缓存 add 10 remove 0, add 11 remove 1 ......
    @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 删除最老的还是最不常用的?

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