cybozu / cybozu/CachePropertyKit

Allow caching for Optional Value

Open
#4 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.