Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nuudles/CacheIsKing
A simple cache that can hold anything, including Swift items
https://github.com/nuudles/CacheIsKing
Last synced: 2 months ago
JSON representation
A simple cache that can hold anything, including Swift items
- Host: GitHub
- URL: https://github.com/nuudles/CacheIsKing
- Owner: nuudles
- License: mit
- Created: 2016-01-26T19:40:34.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-31T18:24:46.000Z (almost 8 years ago)
- Last Synced: 2024-09-20T15:11:09.790Z (4 months ago)
- Language: Swift
- Size: 23.4 KB
- Stars: 13
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swift-cn - CacheIsKing - A light memory caching library with native Swift support (structs/enums/etc.) (Libs / Utility)
README
# 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](http://cocoapods.org) 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`:
```ruby
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!pod 'CacheIsKing'
```Then, run the following command:
```bash
$ 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.
```swift
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:
```swift
let cache = KingCache()
cache["123"] = 123if 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.
```swift
let cache = KingCache()
cache.countLimit = 2cache[123] = 123
cache[234] = 234
cache[345] = 345print("\(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