Skip to content

A Swift Package for easy in-memory caching of properties using Property Wrappers.

License

Notifications You must be signed in to change notification settings

cybozu/CachePropertyKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CachePropertyKit

A Swift Package for easy in-memory caching of properties using Property Wrappers.

Installation

Swift Package Manager

To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift.

dependencies: [
    .package(url: "https://github.com/cybozu/CachePropertyKit.git", from: "1.0.0")
]

Usage

@Cache

A property wrapper that caches the wrapped value at runtime.

import CachePropertyKit

struct Repository {
    func fetch() async throws -> Data {
        @Cache(key: "repository_data", lifetime: .duration(3600))
        var data: Data?

        if let data { // Cached data exists and has not expired.
            return data
        } else { // Cached data does not exist or has expired.
            let newData = try? await fetchNewData()
            data = newData

            return newData
        }
    }
}

CacheContainer

A class for managing cached data from outside the property wrapper.

CacheContainer.clearAll() // Remove all cached data.

CacheContainer.clearAll(where: { key in key == "repository_data" }) // Remove the specified cached data.

About

A Swift Package for easy in-memory caching of properties using Property Wrappers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages