Skip to content

Commit

Permalink
feat: Initialization strategy (#55)
Browse files Browse the repository at this point in the history
* add the initialization strategy, seprate the memory and storage

* add activate and fetch to the demo app
  • Loading branch information
vahidlazio authored Sep 14, 2023
1 parent 125ed50 commit 2c8c7f1
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"pins": [
{
"package": "OpenFeature",
"repositoryURL": "git@github.com:spotify/openfeature-swift-sdk.git",
"repositoryURL": "git@github.com: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 {
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

0 comments on commit 2c8c7f1

Please sign in to comment.