Fix Cache.Key in README
Cache.Key
A type-safe swifty wrapper around NSCache.
NSCache
Add swift-cache as a dependency to your project using SPM.
swift-cache
.package(url: "https://github.com/binaryscraping/swift-cache", from: "0.1.0"),
And in your application/target, add "Cache" to your "dependencies".
"Cache"
"dependencies"
.target( name: "YourTarget", dependencies: [ .product(name: "Cache", package: "swift-cache"), ] )
Cache is accessed through a Cache.Key type, so start by defining your keys.
Cache
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()
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)
let value = cache.retrieve(at: .myKey)
cache.remove(at: .myKey)
This library provides some helpers for easy usage on tests such as:
An implementation of Cache that does nothing when called.
let cache = Cache.noop
An implementation of Cache that fails with an XCTFail call.
XCTFail
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.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
swift-cache
A type-safe swifty wrapper around
NSCache
.Getting Started
Add
swift-cache
as a dependency to your project using SPM.And in your application/target, add
"Cache"
to your"dependencies"
.Usage
Cache
is accessed through aCache.Key
type, so start by defining your keys.Instantiate a live implementation of the cache type.
Insert
You can provide an optional lifetime in seconds for the entry.
Retrieve
Remove
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.Failing
An implementation of
Cache
that fails with anXCTFail
call.At the code snipped above all calls to a method that wasn’t overriden will terminate with a
XCTFail
.