-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the initialization strategy, seprate the memory and storage
- Loading branch information
1 parent
3fbfdd3
commit 051e532
Showing
12 changed files
with
264 additions
and
210 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
Sources/ConfidenceProvider/Cache/InMemoryProviderCache.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 0 additions & 124 deletions
124
Sources/ConfidenceProvider/Cache/PersistentProviderCache.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.