cybozu / cybozu/CachePropertyKit
Allow caching for Optional Value
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 6
- Forks
- 2
- Avg merge
- 1d 58m
- Merged PRs (30d)
- 1
Description
There is a use case where the result of fetching is nil.
Now nil means no cache, so when caching an Optional value, there is no distinction between no cache and cached nil .
struct R {
func fetch() async throws -> Data {
@Cache(key: "my_optional_data", lifetime: .duration(3600))
var data: Data?
if data == nil { // no cache or cached nil?
}
}
}
I want to extend the following API to support Optional values.
struct R {
func fetch() async throws -> Data {
@Cache(key: "my_data", lifetime: .duration(3600))
var data: Data
if data.validCacheExists {
return data
} else {
let newData = try? await fetchNewData()
cache(newData, into: data)
return newData
}
}
}
struct R {
func fetch() async throws -> Data {
@Cache(key: "my_optional_data", lifetime: .duration(3600))
var data: Data?
if data.validCacheExists {
return data
} else {
let newData = try? await fetchNewData()
cache(newData, into: data)
return newData
}
}
}
This will be a breaking change, so it doesn't have to be implemented exactly as proposed.
But the migration should not be difficult.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the Cache property-wrapper API and the validCacheExists and cache operations shown in the issue. Determine how cached nil can be distinguished from no cache while preserving a manageable migration path, then verify that Optional values support the proposed flow and that existing non-Optional caching still works.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100