Swiftpack.co - Package - pinterest/PINCache

PINCache

CocoaPods Carthage compatible Build status

Fast, non-deadlocking parallel object cache for iOS and OS X.

PINCache is a fork of TMCache re-architected to fix issues with deadlocking caused by heavy use. It is a key/value store designed for persisting temporary objects that are expensive to reproduce, such as downloaded data or the results of slow processing. It is comprised of two self-similar stores, one in memory (PINMemoryCache) and one on disk (PINDiskCache), all backed by GCD and safe to access from multiple threads simultaneously. On iOS, PINMemoryCache will clear itself when the app receives a memory warning or goes into the background. Objects stored in PINDiskCache remain until you trim the cache yourself, either manually or by setting a byte or age limit.

PINCache and PINDiskCache accept any object conforming to NSCoding. Put things in like this:

Objective-C

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

Swift

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

Get them back out like this:

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)
    }
}

Both PINMemoryCache and PINDiskCache use locks to protect reads and writes. PINCache coordinates them so that objects added to memory are available immediately to other threads while being written to disk safely in the background. Both caches are public properties of PINCache, so it's easy to manipulate one or the other separately if necessary.

Collections work too. Thanks to the magic of NSKeyedArchiver, objects repeated in a collection only occupy the space of one on disk:

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)

Installation

Manually

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 macOS 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.

Github

link
Stars: 2362

Dependencies

Used By

Total: 1

Releases

3.0.3 - 2020-10-22 17:43:44

Changelog

3.0.3 (2020-10-22)

Full Changelog

Swift Package Manager Support - 2020-10-06 21:13:37

3.0.2 (2020-10-06)

Full Changelog

Merged pull requests:

3.0! (technically 3.0.1) - 2020-08-21 00:49:08

This release contains breaking changes!

3.0 (actually 3.0.1) contains numerous enhancements and changes, most prominent:

  • Fixes building with Xcode 12.
  • Adds support for TTL (time to live caches).
  • Numerous performance enhancements.
  • Custom object serializer/deserializer.
  • PINCaching protocol.
  • Did we mention performance enhancements?

3.0.1 (2020-08-20)

Full Changelog

Implemented enhancements:

Merged pull requests:

3.0.1 -- Beta 8

  • [fix] Initing PINCache with TTL enabled should enable TTL on PINMemoryCache. #246
  • [performance] Return TTL cache objects without waiting for all metadata to be read. #228
  • [performance] Memory cache now performs some tasks such as trimming and removing experied objects with low priority. #234

3.0.1 -- Beta 7

  • [fix] Fix up warnings and upgrade to PINOperation 1.1.1: #213
  • [performance] Reduce locking churn in cleanup methods. #212
  • [fix] Don't set file protection unless requested. #220
  • [new] Add ability to set an object level TTL: #209
  • [performance] Improve performance of age limit trimming: #224

3.0.1 -- Beta 6

  • [fix] Add some sane limits to the disk cache: [#201]https://github.com/pinterest/PINCache/pull/201
  • [new] Update enumeration methods to allow a stop flag to be flipped by caller: #204
  • [performance] Improves cache miss performance by ~2 orders of magnitude on device: #202
  • [performance] Significantly improve startup performance: #203

3.0.1 -- Beta 5

  • [fix] Respect small byteLimit settings by checking object size in setObject: #198
  • [new] Added an ability to set custom encoder/decoder for file names: #192

3.0.1-beta.4 (2017-02-16)

Full Changelog

Merged pull requests:

3.0.1-beta.3 (2017-02-10)

Full Changelog

Merged pull requests:

3.0.1-beta.2 (2016-11-07)

Full Changelog

Merged pull requests:

3.0.1-beta (2016-08-09)

Full Changelog

Merged pull requests:

3.0.0-beta (2016-07-22)

Full Changelog

Merged pull requests:

Modernization - 2019-06-11 21:04:17

  • [fix] Initing PINCache with TTL enabled should enable TTL on PINMemoryCache. #246
  • [performance] Return TTL cache objects without waiting for all metadata to be read. #228
  • [performance] Memory cache now performs some tasks such as trimming and removing experied objects with low priority. #234

Per object TTL and performance fixes - 2018-06-08 18:30:26

  • [fix] Fix up warnings and upgrade to PINOperation 1.1.1: #213
  • [performance] Reduce locking churn in cleanup methods. #212
  • [fix] Don't set file protection unless requested. #220
  • [new] Add ability to set an object level TTL: #209
  • [performance] Improve performance of age limit trimming: #224

3.0.1-beta.6 - 2017-10-07 20:12:45

This release contains big performance gains to the disk cache!

  • [fix] Add some sane limits to the disk cache: [#201]https://github.com/pinterest/PINCache/pull/201
  • [new] Update enumeration methods to allow a stop flag to be flipped by caller: #204
  • [performance] Improves cache miss performance by ~2 orders of magnitude on device: #202
  • [performance] Significantly improve startup performance: #203

3.0.1 Beta 5 - 2017-06-27 22:26:32

Note this beta makes a slight API change, dropping support for a file extension in favor of a custom encoder / decoder for file names.

  • [new] Added an ability to set custom encoder/decoder for file names: #192

3.0.1 Beta 4 - 2017-02-16 23:46:46

  • This beta includes a complete refactor of the project and the way the framework is built with Carthage.

3.0.1 Beta 3 - 2017-02-10 00:51:27

This is beta 3 of 3.0.1 (a major release despite the minor version number)

  • Includes a bunch of bug fixes!
  • Improved Swift support
  • Includes a rethinking of the API to explicitly call out asynchronous methods.

Full list of changes since last beta: https://github.com/pinterest/PINCache/compare/3.0.1-beta.2...3.0.1-beta.3

3.0.1 Beta 2 - 2016-11-07 18:45:07

This is beta 2 of 3.0.1 (a major release despite the minor version number)

  • Includes a bunch of bug fixes!
  • Includes significant performance optimizations, especially of the disk cache!
  • Includes thread limiting of PINCache to reduce the number of outstanding threads spun up which end up just waiting on a lock.

3.0.1 beta - 2016-08-09 22:41:16

Includes support for custom serializer / deserializer

New disk cache callbacks, less locking - 2016-07-22 22:00:41

  • This release is needed mainly for an API change: #97

2.3 - 2016-07-21 22:59:39

  • [new] Added support for object subscripting (#78)
  • [new] Add support for file writing protection (#82)
  • [new] tvOS framework for Carthage (#90)
  • [fix] Start trash background task after dispatch (#75)
  • [fix] Nullability correction (#76)
  • [fix] Fix Xcode warnings printing a pointer value for self (#86)
  • [fix] Update total cost on item replace (#93)
  • [fix] Use pthread mutex instead of a semaphore for locking (#96)
  • [fix] Use NSConditionalLock (#98)
  • [cleanup] Removed local weak self references; It is the same as having one weak (#74)

Remove extension macro requirement - 2016-03-05 23:26:45

  • [new] Removed need for extension macro: #72

TTL Bugfix - 2016-02-17 01:21:35

[fixed] When ttlCache is enabled, skip updating the file modification time when accessing fileURLForKey: #70

TTL Support - 2016-02-12 19:08:34

Adds support for TTL

Fix disk caching - 2015-11-23 18:52:05

Fix disk caching due to NSURL formatting issue.

tvOS Support - 2015-11-18 00:11:09

Added tvOS compatibility.

Xcode 7 support - 2015-09-10 21:04:13

Xcode 7 support

- 2015-05-23 00:47:17

  • A few bug fixes

2.0 - 2015-02-25 18:04:02

Introducing PINCache