A simple cache that can hold anything, including Swift items

Related tags

Cache CacheIsKing
Overview

CacheIsKing

CacheIsKing is a simple cache that allows you to store any item, including objects, pure Swift structs, enums (with associated values), etc. Simply put, it's designed to act like an NSCache for everything, including Swift variables.

Features

  • Simply set, get, and remove items based on any key that is Hashable
  • Remove items for any key that matches a filter
  • The cache is cleared when the app receives a memory warning
  • Similar to NSCache, the cache is cleared when the app enters the background
  • Subscripts are supported for String, Int, and Float keys
  • item(for:) uses generics so you don't have to cast the return value when the type is inferred correctly
  • Similar to NSCache, the cache can have a countLimit set to ensure that the cache doesn't get too large

Requirements

  • iOS 8.0+
  • tvOS 9.0+
  • Xcode 7+

Installation using CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

Because CacheIsKing is written in Swift, you must use frameworks.

To integrate CacheIsKing into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'CacheIsKing'

Then, run the following command:

$ pod install

Installation using Carthage

Add this to your Cartfile:

github "nuudles/CacheIsKing"

Usage

Simply use the KingCache class similar to how you'd use a NSCache. Using the setItem and itemForKey methods allow you to use type inference to get the values you want.

let cache = KingCache()
cache.set(item: 123, for: "123")

if let item: Int = cache.item(for: 456) {
	doSomethingWithItem(item)
}

You can also use subscripts to set/get items from the cache. Unfortunately since Swift doesn't support subscript methods with generics yet, you'll have to cast your items as necessary. Also currently only String, Int, and Float keys are supported:

let cache = KingCache()
cache["123"] = 123

if let item = cache[456] as? Int {
	doSomethingWithItem(item)
}

The KingCache also has a countLimit property, which allows you to set the maximum number of items in the cache. It currently evicts randomly until the countLimit is met.

let cache = KingCache()
cache.countLimit = 2

cache[123] = 123
cache[234] = 234
cache[345] = 345

print("\(cache.count)") // shows a count of 2

TODO

  • Refine eviction algorithm (currently evicts randomly)
  • Update with better subscript support once Swift supports subscripts with generics
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

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.

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

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 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

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

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

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

🏈 Cache CocoaPods for faster rebuild and indexing Xcode project.
🏈 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

Owner
Christopher Luu
Christopher Luu
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
MrCode is a simple GitHub iPhone App that can cache Markdown content (include images in HTML) for read it later.

MrCode is a simple GitHub iPhone App that can cache Markdown content (include images in HTML) for read it later.

hao 448 Dec 19, 2022
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
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
💾 Simple memory & disk cache

Cache ?? Simple memory & disk cache Usage ??‍?? Default let cache = Cache<String>() try memory.save("MyValue", forKey: "MyKey") let cached = try cac

SeongHo Hong 2 Feb 28, 2022
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

National Media & Tech 628 Dec 3, 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
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