SwiftyCache is a dynamic and auto-managed cache written in Swift

Overview

swifty-cache-logo

SwiftyCache is a dynamic and auto-managed cache written in Swift. Unlike a simple cache system, it allows you to keep some data even in different executions. Guaranteeing persistence, when desired, without increasing the time required to save or recover data.

  • Easy to use: it uses a key-value syntax like any dictionary or some NoSql databases.
  • Shared: it is based on a single instance shared by the whole app. A data saved in one point can be easily accessed in another.
  • Heterogeneous: it allows you to store objects of a different types in the same space. Native types are already compatible.
  • Dynamic: During its life it changes its size continuously according to the actual content. Re-compacting when possible and deleting expired data. It is also possible to insert a limit to the maximum reachable size.
  • Auto-managed: you don't have to think about how, when and where to store a data. SwiftyCache manages everything for you.
  • Fast: it guarantees the same performances of NSCache despite offering more functionalities.
  • Safe: it is thread safe, so you can access it from any thread without worrying about managing threads.
  • Lightweight: it efficiently uses memory, disk space, and battery life. In critical situations it automatically chooses if and which data to remove to improve performance.

Installation

You can use Swift Package Manager to add SwiftyPackage to your project.

Add Package Dependency

In Xcode, select File > Add Packages...

Specify the Repository

Copy and paste the following into the search/input box.

https://github.com/antonio-war/SwiftyCache

Specify options

Use Up to Next Major Version as dependency rule and enter the current SwiftyCache version. Then click Add Package.


Getting Started

Keys

SwiftyCache allows you to use any type conforming to the SwiftyCacheKey protocol as a key. Most native types already conform to this protocol, so you can use Int, String, URL etc. without having to do anything.

If you want to use a custom type as cache key, just make it compliant with the protocol.

struct Person: SwiftyCacheKey {
    let name: String
    let surname: String
}

Values

SwiftyCache allows you to use any type conforming to the SwiftyCacheValue protocol as a value. The reasoning is the same applied to the keys.

struct Person: SwiftyCacheValue {
    let name: String
    let surname: String
}

Store an object

To store an object in the cache you need to use the set method, which provides some possible configurations.

Default storage

By default SwiftyCache stores data only in memory and for an indefinite time. In fact, they will be deleted only if explicitly requested by the user or in critical cases in which the operating system needs to free up the memory. However, this is a non-persistent storage so the data is lost when the user closes the App.

The following example downloads data from the web and saves it in the cache to retrieve them later.

let url = URL(string: "https://cdn.cocoacasts.com/cc00ceb0c6bff0d536f25454d50223875d5c79f1/above-the-clouds.jpg")!
if let data = try? Data(contentsOf: url) {
    SwiftyCache.shared.set(value: data, for: url)
}

Storage with expiration

Sometimes the data downloaded from the web has a limited duration, after which it becomes invalid. Therefore SwiftyCache provides the possibility of associating an expiration with this data so that it is automatically deleted when necessary.

To do this SwiftyCache provides a utility called SwiftyCacheDuration. It provides a quick and easy way to specify seconds, minutes, hours and even days of validity.

The following example considers the data to be valid for only 30 seconds.

SwiftyCache.shared.set(value: data, for: url, duration: .seconds(quantity: 30))

Persistent storage

As already mentioned, SwiftyCache allows you to store data persistently, in order to find it cached even after restarting the App.

To ensure the persistent cache works, it is necessary to specify among the schema environment variables: SWIFT_DETERMINISTIC_HASHING = 1

scheme-settings

Obviously, an expiration date can also be associated with this type of storage, as seen previously.

SwiftyCache.shared.set(value: data, for: url, duration: .days(quantity: 1), persisted: true)

Retrieve an object

SwiftyCache allows you to retrieve objects using the get method. It requires you to specify the type of data to be recovered. The method returns 'nil' if the data does not exist or has expired.

If you want to recover the previously saved data, which were of the Data type, the call will be as follows.

let value = SwiftyCache.shared.get(key: url, as: Data.self)

Delete objects

As for the deletion of objects, SwiftyCache provides two methods: one for single deletion and one for deleting all data. In the first case it is obviously necessary to specify the key of the data to be deleted.

SwiftyCache.shared.remove(key: url)

SwiftyCache.shared.removeAll()

Set a maximum size

As previously mentioned, SwiftyCache allows you to set a maximum size in order to prevent the data to be managed from becoming too heavy. All you have to do is call the setMaximumSize method, the memory will then be managed automatically by SwiftyCache which will remove the oldest and / or least used data.

To do this SwiftyCache provides a utility called SwiftyCacheSize. It provides a quick and easy way to specify bytes, megabytes and gigabytes.

SwiftyCache.shared.setMaximumSize(size: .megabytes(quantity: 500))

Image Caching

Images are even easier to cache than other objects. In fact, everything will be possible through a constructor that encapsulates and manages all the calls relating to the methods seen above. If your goal is to download images via their URL and cache them, SwiftyCache is for you.

let url = URL(string: "https://cdn.cocoacasts.com/cc00ceb0c6bff0d536f25454d50223875d5c79f1/above-the-clouds.jpg")!
let image = UIImage(url: url)

In the basic form the constructor caches the image in memory and does not set any duration, so it is always valid within the same session. Whenever the App tries to instantiate an image with the same URL, the cached version will be taken.

Duration

As seen previously SwiftyCache allows you to set a duration for each object, so that after a certain period of time it is no longer considered valid. This possibility has also been guaranteed for images.

let image = UIImage(url: url, duration: .seconds(quantity: 30))

Persistence

A very useful function is to persistently cachare the images, in order to save time even in subsequent openings of the App.

let image = UIImage(url: url, persisted: true)

Using the cached version of the image rather than re-downloading it turns out to be about 10 times faster.

Update

In some cases you may want to re-download the image by discarding the saved version, this is possible through the force update option.

let image = UIImage(url: url, forceUpdate: true)

This does not bother to check if a version already exists in memory or if it is valid. It will directly download the image and save it by overwriting the old one.


License

SwiftyCache is published under the Apache 2.0 license.

You might also like...
MemoryCache - type-safe, thread-safe memory cache class in Swift

MemoryCache is a memory cache class in swift. The MemoryCache class incorporates LRU policies, which ensure that a cache doesn’t

A simple cache that can hold anything, including Swift items

CacheIsKing CacheIsKing is a simple cache that allows you to store any item, including objects, pure Swift structs, enums (with associated values), et

High performance cache framework for iOS.
High performance cache framework for iOS.

YYCache High performance cache framework for iOS. (It's a component of YYKit) Performance You may download and compile the latest version of sqlite an

UITableView cell cache that cures scroll-lags on cell instantiating

UITableView + Cache https://github.com/Kilograpp/UITableView-Cache UITableView cell cache that cures scroll-lags on a cell instantiating. Introduction

💾 Simple memory & disk cache

Cache 💾 Simple memory & disk cache Usage 🧑‍💻 Default let cache = CacheString() try memory.save("MyValue", forKey: "MyKey") let cached = try cac

Cache library for videos for React Native

@lowkey/react-native-cache Cache everything Installation npm install @lowkey/react-native-cache Usage import ReactNativeCache from "@lowkey/react-nati

CachedAsyncImage is the simplest way to add cache to your AsyncImage.

CachedAsyncImage 🗃️ CachedAsyncImage is AsyncImage, but with cache capabilities. Usage CachedAsyncImage has the exact same API and behavior as AsyncI

XCRemoteCache is a remote cache tool for Xcode projects.
XCRemoteCache is a remote cache tool for Xcode projects.

XCRemoteCache is a remote cache tool for Xcode projects. It reuses target artifacts generated on a remote machine, served from a simple REST server. H

A simple but flexible cache

Carlos A simple but flexible cache, written in Swift for iOS 13+ and WatchOS 6 apps. Breaking Changes Carlos 1.0.0 has been migrated from PiedPiper de

Releases(1.1.1)
Owner
Antonio Guerra
iOS Developer | Machine Learning & Big Data Student
Antonio Guerra
Cache - Nothing but Cache.

Cache doesn't claim to be unique in this area, but it's not another monster library that gives you a god's power. It does nothing but caching, but it does it well. It offers a good public API with out-of-box implementations and great customization possibilities. Cache utilizes Codable in Swift 4 to perform serialization.

HyperRedink 2.7k Dec 28, 2022
Apple Asset Cache (Content Cache) Tools

AssetCacheTool A library and tool for interacting with both the local and remote asset caches. This is based on research I did a few years ago on the

Kenneth Endfinger 21 Jan 5, 2023
CachyKit - A Caching Library is written in Swift that can cache JSON, Image, Zip or AnyObject with expiry date/TTYL and force refresh.

Nice threadsafe expirable cache management that can cache any object. Supports fetching from server, single object expire date, UIImageView loading etc.

Sadman Samee 122 Dec 28, 2022
Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift.

Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift. There already exists plenty of cache solutions, so why creat

Norsk rikskringkasting (NRK) 124 Nov 24, 2022
Carlos - A simple but flexible cache, written in Swift for iOS 13+ and WatchOS 6 apps.

Carlos A simple but flexible cache, written in Swift for iOS 13+ and WatchOS 6 apps. Breaking Changes Carlos 1.0.0 has been migrated from PiedPiper de

National Media & Tech 628 Dec 3, 2022
A lightweight generic cache for iOS written in Swift with extra love for images.

Haneke is a lightweight generic cache for iOS and tvOS written in Swift 4. It's designed to be super-simple to use. Here's how you would initalize a J

Haneke 5.2k Dec 29, 2022
Track is a thread safe cache write by Swift. Composed of DiskCache and MemoryCache which support LRU.

Track is a thread safe cache write by Swift. Composed of DiskCache and MemoryCache which support LRU. Features Thread safe: Implement by dispatch_sema

Cheer 268 Nov 21, 2022
Everyone tries to implement a cache at some point in their iOS app’s lifecycle, and this is ours.

Everyone tries to implement a cache at some point in their app’s lifecycle, and this is ours. This is a library that allows people to cache NSData wit

Spotify 1.2k Dec 28, 2022
Fast, non-deadlocking parallel object cache for iOS, tvOS and OS X

PINCache Fast, non-deadlocking parallel object cache for iOS and OS X. PINCache is a fork of TMCache re-architected to fix issues with deadlocking cau

Pinterest 2.6k Dec 28, 2022
🏈 Cache CocoaPods for faster rebuild and indexing Xcode project.

Motivation Working on a project with a huge amount of pods I had some troubles: - Slow and unnecessary indexing of pods targets, which implementation

Vyacheslav Khorkov 487 Jan 5, 2023