PINCache

作者: 独木舟的木 | 来源:发表于2020-07-25 15:27 被阅读0次

PINCache 是一个适用于 iOS,tvOS 和 OS X 的快速、无死锁的并行对象缓存框架。

PINCacheTMCache 项目的一个分支,它对 TMCache 进行了重构,以修复大量使用导致的死锁问题。它是一个键/值存储,设计用于持久化临时对象,这些临时对象的重现成本很高,比如下载的数据或处理速度慢的结果。它由两个自相似存储组成,一个在内存中(PINMemoryCache),另一个在磁盘上(PINDiskCache),都由 GCD 支持,可以安全地从多个线程同时访问。在 iOS 系统中,当应用程序收到内存警告或进入后台时,PINMemoryCache 会自行清除。而存储在 PINDiskCache 中的对象会保留下来,直到你自己手动或通过设置字节或缓存时间限制来删除缓存。

PINCachePINDiskCache 接受任何遵守 <NSCoding> 协议的对象。设置缓存的方式如下:

Objective-C

UIImage *img = [[UIImage alloc] initWithData:data scale:[[UIScreen mainScreen] scale]];
[[PINCache sharedCache] setObject:img forKey:@"image" block:nil]; // 立即返回

Swift

let img = UIImage(data: data, scale:UIScreen.main.scale)
PINCache.shared().setObject(img, forKey: "img")

取出缓存的方式如下:

Objective-C

[[PINCache sharedCache] objectForKeyAsync:@"image" block:^(PINCache *cache, NSString *key, id object) {
    UIImage *image = (UIImage *)object;
    NSLog(@"image scale: %f", image.scale);
}];

Swift

PINCache.shared().object(forKey: "image") { (cache, key, object) in
    if let image = object as? UIImage {
        print("image scale: %f", image.scale)
    }
}

PINMemoryCachePINDiskCache 都使用锁来保护读和写。PINCache 对它们进行协调,使添加到内存中的对象在后台安全地写入磁盘的同时,可以立即被其他线程使用。这两个缓存都是 PINCache 的公共属性,所以如果需要的话,可以很容易地分别操作其中一个或另一个。

集合对象也可以使用。得益于 NSKeyedArchiver 的魔力,一个集合中重复的对象只占用磁盘上的一个空间:

Objective-C

NSArray *images = @[ image, image, image ];
[[PINCache sharedCache] setObject:images forKey:@"images"];
NSLog(@"3 for the price of 1: %d", [[[PINCache sharedCache] diskCache] byteCount]);

Swift

// In Swift, Array, String, and Dictionary are all value types.
let images = [image, image, image] as NSArray // Cast to NSArray
PINCache.shared.setObject(images, forKey: "images")
print("3 for the prices of 1: %d", PINCache.shared.diskCache.byteCount)

安装

手动安装

Download the latest tag and drag the PINCache folder into your Xcode project.

Install the docs by double clicking the .docset file under docs/, or view them online at cocoadocs.org

Git Submodule

git submodule add https://github.com/pinterest/PINCache.git
git submodule update --init

CocoaPods

Add PINCache to your Podfile and run pod install.

Carthage

Add the following line to your Cartfile and run carthage update --platform ios. Then follow this instruction of Carthage to embed the framework.

github "pinterest/PINCache"

Requirements

PINCache requires iOS 8.0, tvOS 9.0, watchOS 2.0 or OS X 10.8 and greater.

Contact

Garrett Moon

License

Copyright 2013 Tumblr, Inc.
Copyright 2015 Pinterest, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

相关文章

  • iOS - 常见框架使用

    1. PINCache -->PINCache是多线程安全的,使用键值对来保存数据。PINCache内部包含了2个...

  • PINCache

    PINCache 是一个适用于 iOS,tvOS 和 OS X 的快速、无死锁的并行对象缓存框架。 PINCach...

  • PINCache和YYCache

    PINCache根据键值对来进行存储, PINCache有磁盘缓存和内存缓存,分别由两个对象属性PINDiskCa...

  • ReactiveCocoa AFNetworking PINCa

    最近需要实现对网络数据的缓存,通过综合考虑最后决定采用PINCache作为底层缓存框架,PINCache是Pint...

  • iOS PINCache学习

    最近突发奇想,想对比下几个不同Cache框架的实现,于是就从项目中在用的PINCache着手分析。PINCache...

  • PINCache中的后台任务

    PINCache中的后台任务 在PINCache磁盘缓存策略中,针对数据写入,删除等磁盘操作,作者用了后台任务操作...

  • 使用PINCache优化代码

    前言 客户反馈查看图片的速度非常慢,于是我们需要做一下图片的缓存,由于图片都是后台base64传过来的,所以,需要...

  • Cocoapods解决Search不到新版本的问题

    今天升级了Xcode 12编译项目发现失败了,看了下编译日志 发现是PINCache和PINRemoteImage...

  • HDDNetworking网络组件

    基于AFNetworking的v3.1.0进行网络请求,基于PINCache的v3.0.1进行网络数据缓存,支持清...

  • iOS缓存框架-PINCache解读

    在项目中总是需要缓存一些网络请求数据以减轻服务器压力,业内也有许多优秀的开源的解决方案。通常的缓存方案都是由内存缓...

网友评论

      本文标题:PINCache

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