Swiftpack.co - Package - grsouza/Cache



The missing Cache type from Swift Foundation.


Cache is distributed using Swift Package Manager. To install it into a project, add it as a dependency within your Package.swift manifest:

let package = Package(
    dependencies: [
        .package(url: "https://github.com/grsouza/Cache.git", from: "0.1.0")

Then import Cache wherever you'd like to use it:

import Cache


To create a Cache type you'll need to provide Key and Value types that the Cache will hold, the only constraint is that Key must conform to Hashable.

import Cache

enum CacheKey: String, Codable {
    case one, two, three

struct TestModel: Codable, Equatable {
    let id: String
    let name: String

let cache = Cache<CacheKey, TestModel>()

Insert value into Cache

let value = TestModel(id: UUID().uuidString, name: "Cache test")
cache.insert(value, forKey: .one)

Retrieve value from Cache

let retrievedValue = cache.value(forKey: .one)

Remove value from Cache

cache.removeValue(forKey: .one)

Persist Cache to disk

If Key and Value conforms to Codable then you can automatically persist the cache to disk, like this:

try cache.saveToDisk(withName: "testmodel")

// and load from disk

let cache = try Cache<CacheKey, TestModel>.loadFromDisk(withName: "testmodel")


Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.


Apache License 2.0


Stars: 0


Used By

Total: 0


0.1.0 - 2020-07-17 01:00:42