by soffes

soffes / Cache

Swift caching library

207 Stars 14 Forks Last release: 5 months ago (v0.3.2) MIT License 29 Commits 5 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


Version Carthage compatible

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

This is still very much a work in progress.


Cache provides a simple

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
  • DiskCache
    — Backed by files on disk using

Using both of these is exactly the same interface.


There is a third cache provided called

. 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 to Caleb Davenport for unintentionally inspiring this and lots of help along the way.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.