AKLruDictionary 2.0

AKLruDictionary 2.0

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年2月

Aleksey Kozhevnikov维护。



  • By
  • Aleksey Kozhevnikov

LRUDictionary

与 NSCache 接口类似的 LRU(最近最少使用)内存缓存数据结构

用法

// Create cache for UIImage objects with total max size of 2 MB, objects exceeding size of 150 KB will be ignored
self.cache = [[AKLruDictionary alloc] initWithCountLimit:NSUIntegerMax
                                      perObjectCostLimit:2 * 1024 * 1024
                                               costLimit:150 * 1024];

...

- (UIImage*)imageWithMagicText:(NSString*)magicText
{
    NSParameterAssert(magicText != nil);

    UIImage* image = [self.cache objectForKey:magicText];
    if( image == nil ) {
        image = [MagicClass drawImageWithMagicText:magicText];
        [self.cache setObject:image forKey:magicText cost:[image ak_bytesCost]];
    }
    return image;
}

线程安全

AKLruDictionary 类的实例不是线程安全的,如果需要,您可以使用 AKThreadSafeLruDictionary(仅使用了自旋锁包装)。

安装

最佳方法是使用 CocoaPods

如果尚未安装,请安装 CocoaPods gem 并设置其环境

$ [sudo] gem install cocoapods
$ pod setup

转到包含您的项目 .xcodeproj 文件的目录并创建 Podfile

$ cd ~/Projects/MyProject
$ vim Podfile

将以下行添加到 Podfile 中

platform :ios
pod 'AKLruDictionary'

最后,安装您的 pod 依赖项

$ [sudo] pod install

这样就完成了,现在打开刚刚创建的 .xcworkspace 文件

联系

Aleksey Kozhevnikov