Swift caching library

Overview

Cache

Version Carthage compatible

A generic caching library for Swift. Cache depends on Foundation.

This is still very much a work in progress.

Usage

Cache provides a simple Cache protocol:

protocol Cache {
  associatedtype Element

  func get(key key: String, completion: (Element? -> Void))
  func set(key key: String, value: Element, completion: (() -> Void)?)
  func remove(key key: String, completion: (() -> Void)?)
  func removeAll(completion completion: (() -> Void)?)
}

There are two (well actually three, but we'll get there) provided caches:

  • MemoryCache — Backed by an NSCache
  • DiskCache — Backed by files on disk using NSFileManager

Using both of these is exactly the same interface.

MultiCache

There is a third cache provided called MultiCache. This combines caches.

let cache = MultiCache(caches: [memoryCache, diskCache])
cache.set(key: "foo", value: "bar")

This will write to all caches in parallel. When accessing the multi cache, it will go in order. In this example, it will try the memory cache and if there's a miss it will try the disk cache. If it were to find it in the disk cache, it will write it to all previous caches for faster future reads.

Thanks

Thanks to Caleb Davenport for unintentionally inspiring this and lots of help along the way.

You might also like...
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.

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

A document-based app in Swift Playgrounds 4 for iPad
A document-based app in Swift Playgrounds 4 for iPad

A document-based SwiftUI app in Swift Playgrounds This sample project demonstrat

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

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

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.

iOS Swift Pazarama Bootcamp Week 3 task - App by builded with iTunes Api
iOS Swift Pazarama Bootcamp Week 3 task - App by builded with iTunes Api

MeTunes! a library for itunes! MeTunes. Key Features • Installation • Download • Technologies Used • Credits • License Key Features Fully Programmatic

Kingfisher is a powerful, pure-Swift library for downloading and caching images from the web
Kingfisher is a powerful, pure-Swift library for downloading and caching images from the web

Kingfisher is a powerful, pure-Swift library for downloading and caching images from the web. It provides you a chance to use a pure-Swift way to work

🍁🥓 Lightweight and fast Swift library for image downloading, caching and transformations
🍁🥓 Lightweight and fast Swift library for image downloading, caching and transformations

MapleBacon Introduction MapleBacon is a lightweight and fast Swift library for downloading and caching images. Example The folder Example contains a s

A high-performance image library for downloading, caching, and processing images in Swift.
A high-performance image library for downloading, caching, and processing images in Swift.

Features Asynchronous image downloader with priority queuing Advanced memory and database caching using YapDatabase (SQLite) Guarantee of only one ima

Swift caching library

Cache A generic caching library for Swift. Cache depends on Foundation. This is still very much a work in progress. Usage Cache provides a simple Cach

An extremely high-performance, lightweight, and energy-efficient pure Swift async web image loader with memory and disk caching for iOS and  Watch.

KFSwiftImageLoader KFSwiftImageLoader is an extremely high-performance, lightweight, and energy-efficient pure Swift async web image loader with memor

📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.
📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.

Apollo iOS is a strongly-typed, caching GraphQL client, written in Swift. It allows you to execute queries and mutations against a GraphQL server, and

Melodic Caching for Swift
Melodic Caching for Swift

Johnny is a generic caching library written for Swift 4. Features Johnny can cache any model object that conforms to the Cachable protocol. Out-of-the

Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support
Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support

Networking was born out of the necessity of having a simple networking library that doesn't have crazy programming abstractions or uses the latest rea

A Swift/SwiftUI utility for caching and displaying images in SwiftUI Views

A Swift/SwiftUI utility for caching and displaying images asynchronously. Built with Swift 5.5 and works with async/await.

iOS Offline Caching for Web Content

Mattress A Swift framework for storing entire web pages into a disk cache distinct from, but interoperable with, the standard NSURLCache layer. This i

A caching and consistency solution for immutable models.

🚀 Data Rocket Data is a model management system with persistence for immutable models. Motivation Immutability has many benefits, but keeping models

📽 A video player for SwiftUI, support for caching, preload and custom control view.
📽 A video player for SwiftUI, support for caching, preload and custom control view.

Features QuickStart Advances Installation Requirements License Demo Clone or download the project. In the terminal, run swift package resolve. Open Vi

Advanced framework for loading, caching, processing, displaying and preheating images.
Advanced framework for loading, caching, processing, displaying and preheating images.

Advanced framework for loading, caching, processing, displaying and preheating images. This framework is no longer maintained. Programming in Swift? C

Focus on avatar caching.

Navi Navi is designed for avatar caching, with style. The name of Navi from movie Avatar. Requirements Swift 3.1, iOS 8.0 Swift 2.3, use version 0.5.0

Comments
  • Argument syntax may need to be changed because of SE-0066

    Argument syntax may need to be changed because of SE-0066

    Swift Evolution 66 https://github.com/apple/swift-evolution/blob/master/proposals/0066-standardize-function-type-syntax.md will require arguments to be placed in parameters.

    There are parts that will need to be changed, and it's better to do it now, rather than later.

    e.g.

    func get(key key: String, completion: (Element? -> Void))
    

    should be

    func get(key key: String, completion: ((Element?) -> Void))

    Just putting this here for discussion, I'll take a stab at it if no one else wants to.

    opened by Abizern 3
Releases(v0.3.2)
Owner
Sam Soffes
Building things
Sam Soffes
Melodic Caching for Swift

Johnny is a generic caching library written for Swift 4. Features Johnny can cache any model object that conforms to the Cachable protocol. Out-of-the

Zoltán Matók 37 Nov 21, 2022
iOS Offline Caching for Web Content

Mattress A Swift framework for storing entire web pages into a disk cache distinct from, but interoperable with, the standard NSURLCache layer. This i

BuzzFeed 522 Oct 25, 2022
A caching and consistency solution for immutable models.

?? Data Rocket Data is a model management system with persistence for immutable models. Motivation Immutability has many benefits, but keeping models

Peter Livesey 652 Nov 11, 2022
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

Max Prokopenko 1 Oct 1, 2021
A library and tool for interacting with both the local and remote asset caches.

Asset Cache Tool 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 th

Kenneth Endfinger 20 Dec 23, 2022
Awesome Cache Delightful on-disk cache (written in Swift). Backed by NSCache for maximum performance

Awesome Cache Delightful on-disk cache (written in Swift). Backed by NSCache for maximum performance and support for expiry of single objects. Usage d

Alexander Schuch 1.3k Dec 29, 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
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

Yusuke Morishita 74 Nov 24, 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