Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow caching for Optional Value #4

Open
elmetal opened this issue May 30, 2024 · 0 comments
Open

Allow caching for Optional Value #4

elmetal opened this issue May 30, 2024 · 0 comments

Comments

@elmetal
Copy link
Contributor

elmetal commented May 30, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant