Code Monkey home page Code Monkey logo

cache's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.