Skip to content

Commit

Permalink
add the initialization strategy, seprate the memory and storage
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidlazio committed Sep 7, 2023
1 parent 3fbfdd3 commit 051e532
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 210 deletions.
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?
}
6 changes: 4 additions & 2 deletions Sources/ConfidenceProvider/Cache/Models/CacheData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct CacheData: Codable {
mutating func setEventStatus(resolveToken: String, name: String, status: ApplyEventStatus = .sent) {
let flagEventIndexes = flagEventIndex(resolveToken: resolveToken, name: name)
guard let resolveIndex = flagEventIndexes.resolveEventIndex,
let flagIndex = flagEventIndexes.flagEventIndex else {
let flagIndex = flagEventIndexes.flagEventIndex
else {
return
}

Expand Down Expand Up @@ -116,7 +117,8 @@ struct CacheData: Codable {

func flagEvent(resolveToken: String, name: String) -> FlagApply? {
guard let resolveTokenIndex = resolveEventIndex(resolveToken: resolveToken),
let flagEventIndex = applyEventIndex(resolveToken: resolveToken, name: name) else {
let flagEventIndex = applyEventIndex(resolveToken: resolveToken, name: name)
else {
return nil
}

Expand Down
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 051e532

Please sign in to comment.