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

feat: Initialization strategy #55

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"pins": [
{
"package": "OpenFeature",
"repositoryURL": "[email protected]:spotify/openfeature-swift-sdk.git",
"repositoryURL": "[email protected]:open-feature/swift-sdk.git",
"state": {
"branch": null,
"revision": "ef8dd46fb9623fc85b0b75788def006f7b59132c",
"version": "0.2.5"
"revision": "114321e376b3d80e6623c5cbd7e8bcef9c6a86d2",
"version": "0.0.2"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extension ConfidenceDemoApp {
}
let provider = ConfidenceFeatureProvider
.Builder(credentials: .clientSecret(secret: secret))
.with(initializationStrategy: .activateAndFetchAsync)
.build()
let ctx = MutableContext(targetingKey: UUID.init().uuidString, structure: MutableStructure())
OpenFeatureAPI.shared.setProvider(provider: provider, initialContext: ctx)
Expand Down
67 changes: 67 additions & 0 deletions Sources/ConfidenceProvider/Cache/InMemoryProviderCache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Combine
import Foundation
import OpenFeature
import os

public class InMemoryProviderCache: ProviderCache {
private var rwCacheQueue = DispatchQueue(label: "com.confidence.cache.rw", attributes: .concurrent)
static let currentVersion = "0.0.1"
private let cache: [String: ResolvedValue]

private var storage: Storage
private var curResolveToken: String?
private var curEvalContextHash: String?

init(storage: Storage, cache: [String: ResolvedValue], curResolveToken: String?, curEvalContextHash: String?) {
self.storage = storage
self.cache = cache
self.curResolveToken = curResolveToken
self.curEvalContextHash = curEvalContextHash
}

public func getValue(flag: String, ctx: EvaluationContext) throws -> CacheGetValueResult? {
if let value = self.cache[flag] {
guard let curResolveToken = curResolveToken else {
vahidlazio marked this conversation as resolved.
Show resolved Hide resolved
throw ConfidenceError.noResolveTokenFromCache
}
return .init(
resolvedValue: value, needsUpdate: curEvalContextHash != ctx.hash(), resolveToken: curResolveToken)
} else {
return nil
}
}

public static func from(storage: Storage) -> InMemoryProviderCache {
do {
let storedCache = try storage.load(
defaultValue: StoredCacheData(
version: currentVersion, cache: [:], curResolveToken: nil, curEvalContextHash: nil))
return InMemoryProviderCache(
storage: storage,
cache: storedCache.cache,
curResolveToken: storedCache.curResolveToken,
curEvalContextHash: storedCache.curEvalContextHash)
} catch {
Logger(subsystem: "com.confidence.cache", category: "storage").error(
"Error when trying to load resolver cache, clearing cache: \(error)")

if case .corruptedCache = error as? ConfidenceError {
try? storage.clear()
}

return InMemoryProviderCache(storage: storage, cache: [:], curResolveToken: nil, curEvalContextHash: nil)
}
}
}

public struct ResolvedKey: Hashable, Codable {
var flag: String
var targetingKey: String
}

struct StoredCacheData: Codable {
var version: String
var cache: [String: ResolvedValue]
var curResolveToken: String?
var curEvalContextHash: String?
}
124 changes: 0 additions & 124 deletions Sources/ConfidenceProvider/Cache/PersistentProviderCache.swift

This file was deleted.

2 changes: 0 additions & 2 deletions Sources/ConfidenceProvider/Cache/ProviderCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import OpenFeature

public protocol ProviderCache {
func getValue(flag: String, ctx: EvaluationContext) throws -> CacheGetValueResult?

func clearAndSetValues(values: [ResolvedValue], ctx: EvaluationContext, resolveToken: String) throws
}

public struct CacheGetValueResult {
Expand Down
Loading
Loading