A type-safe swifty wrapper around NSCache.

Overview

swift-cache

A type-safe swifty wrapper around NSCache.

Getting Started

Add swift-cache as a dependency to your project using SPM.

.package(url: "https://github.com/binaryscraping/swift-cache", from: "0.1.0"),

And in your application/target, add "Cache" to your "dependencies".

.target(
  name: "YourTarget",
  dependencies: [
    .product(name: "Cache", package: "swift-cache"),
  ]
)

Usage

Cache is accessed through a Cache.Key type, so start by defining your keys.

extension Cache.Key where Value == String {
  // A key that stores a string value.
  static let myKey = Cache.Key("my_key")
}

Instantiate a live implementation of the cache type.

let cache = Cache.live()

Insert

cache.set("string value", at: .myKey)

You can provide an optional lifetime in seconds for the entry.

cache.set("string value", at: .myKey, lifetime: 60)

Retrieve

let value = cache.retrieve(at: .myKey)

Remove

cache.remove(at: .myKey)

Usage in tests

This library provides some helpers for easy usage on tests such as:

Noop

An implementation of Cache that does nothing when called.

let cache = Cache.noop

Failing

An implementation of Cache that fails with an XCTFail call.

var setEntryCalled = false

let cache = Cache.failing
  .override(
    setEntry: { entry, key in 
      setEntryCalled = true
    }
  )
  
cache.set("string value", at: .myKey)
  
XCTAssertTrue(setEntryCalled)

At the code snipped above all calls to a method that wasn't overriden will terminate with a XCTFail.

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

Modern thread-safe and type-safe key-value observing for Swift and Objective-C

Now Archived and Forked PMKVObserver will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork o

A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety
A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety

A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety. Type Safe No more userInfo dictionary and Downcasting,

A phantom type is a custom type that has one or more unused type parameters.

PhantomTypes A phantom type is a custom type that has one or more unused type parameters. Phantom types allow you to enforce type-safety without sacri

NoticeObserveKit is type-safe NotificationCenter wrapper.

NoticeObserveKit NoticeObserveKit is type-safe NotificationCenter wrapper. // .keyboardWillShow is a static property. Notice.Center.default.observe(na

NoticeObserveKit is type-safe NotificationCenter wrapper.

NoticeObserveKit NoticeObserveKit is type-safe NotificationCenter wrapper. // .keyboardWillShow is a static property. Notice.Center.default.observe(na

Swifty, modern UIAlertController wrapper.
Swifty, modern UIAlertController wrapper.

Alertift Alertift.alert(title: "Alertift", message: "Alertift is swifty, modern, and awesome UIAlertController wrapper.") .action(.default("❤️"))

Github API V3 Swifty Wrapper

GithubPilot - Github API V3 Swifty Wrapper This is a Swift Github API Wrapper, it could make your life a little easier if you want to make an App with

React Native utility library around image and video files for getting metadata like MIME type, timestamp, duration, and dimensions. Works on iOS and Android using Java and Obj-C, instead of Node 🚀.

Qeepsake React Native File Utils Extracts information from image and video files including MIME type, duration (video), dimensions, and timestamp. The

DataKernel is a minimalistic wrapper around CoreData stack to ease persistence operations.

DataKernel What is DataKernel? DataKernel is a minimalistic wrapper around CoreData stack to ease persistence operations. It is heavily inspired by Su

A stand-alone Swift wrapper around the mongo-c client library, enabling access to MongoDB servers.
A stand-alone Swift wrapper around the mongo-c client library, enabling access to MongoDB servers.

This package is deprecated in favour of the official Mongo Swift Driver. We advise users to switch to that pack

Perfect - a Swift wrapper around the MySQL client library, enabling access to MySQL database servers.
Perfect - a Swift wrapper around the MySQL client library, enabling access to MySQL database servers.

Perfect - MySQL Connector This project provides a Swift wrapper around the MySQL client library, enabling access to MySQL database servers. This packa

A stand-alone Swift wrapper around the libpq client library, enabling access to PostgreSQL servers.
A stand-alone Swift wrapper around the libpq client library, enabling access to PostgreSQL servers.

Perfect - PostgreSQL Connector This project provides a Swift wrapper around the libpq client library, enabling access to PostgreSQL servers. This pack

Thin wrapper around NSURLSession in swift. Simplifies HTTP requests.

SwiftHTTP SwiftHTTP is a thin wrapper around NSURLSession in Swift to simplify HTTP requests. Features Convenient Closure APIs Simple Queue Support Pa

💡 A light Swift wrapper around Objective-C Runtime
💡 A light Swift wrapper around Objective-C Runtime

A light wrapper around Objective-C Runtime. What exactly is lumos? lumos as mentioned is a light wrapper around objective-c runtime functions to allow

A Swift wrapper around the CoreSymbolication private framework on macOS.

CoreSymbolication provides a very powerful system for looking up and extracting symbolic information from mach-o executables, dyld shared caches, and dSYMs.

A testable RxSwift wrapper around MultipeerConnectivity

A testable RxSwift wrapper around MultipeerConnectivity

A Cocoa / Objective-C wrapper around SQLite

FMDB v2.7 This is an Objective-C wrapper around SQLite. The FMDB Mailing List: https://groups.google.com/group/fmdb Read the SQLite FAQ: https://www.s

A feature-light wrapper around Core Data that simplifies common database operations.
A feature-light wrapper around Core Data that simplifies common database operations.

Introduction Core Data Dandy is a feature-light wrapper around Core Data that simplifies common database operations. Feature summary Initializes and m

Releases(0.1.0)
Owner
Binary Scraping
We are a small business specialized in software development and consultancy for the Apple ecosystem.
Binary Scraping
LRUCache is an open-source replacement for NSCache that behaves in a predictable, debuggable way

Introduction Installation Usage Concurrency Performance Credits Introduction LRUCache is an open-source replacement for NSCache that behaves in a pred

Nick Lockwood 290 Oct 20, 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
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
Thread -safe access to a lazily retrieved value, with optional validity checking

SerialUpdatingValue Thread-safe access to a lazily retrieved value, with optional validity checking Motivation Swift's Structured Concurrency provides

Smiley Rob 3 Nov 11, 2022
A type-safe auto-generated Swift wrapper around fluentui-emoji

fluentui-emoji.swift A type-safe auto-generated Swift wrapper around fluentui-emoji. Usage Requirements iOS 13+ macOS 10.15+ tvOS 13+ watchOS 6+ Insta

Yubo Qin 11 Dec 27, 2022
Type-safe networking abstraction layer that associates request type with response type.

APIKit APIKit is a type-safe networking abstraction layer that associates request type with response type. // SearchRepositoriesRequest conforms to Re

Yosuke Ishikawa 1.9k Dec 30, 2022
Type-safe CAAnimation wrapper. It makes preventing to set wrong type values.

TheAnimation TheAnimation is Type-safe CAAnimation wrapper. Introduction For example, if you want to animate backgroundColor with CABasicAnimation, yo

Taiki Suzuki 222 Dec 6, 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
LRUCache is an open-source replacement for NSCache that behaves in a predictable, debuggable way

Introduction Installation Usage Concurrency Performance Credits Introduction LRUCache is an open-source replacement for NSCache that behaves in a pred

Nick Lockwood 290 Oct 20, 2022
A wrapper around UICollectionViewController enabling a declarative API around it's delegate methods using protocols.

Thunder Collection Thunder Collection is a useful framework which enables quick and easy creation of collection views in iOS using a declarative appro

3 SIDED CUBE 4 Nov 6, 2022