-
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.
feat: Add feature flag caching mechanism
- Loading branch information
1 parent
124c2f9
commit 8f36112
Showing
3 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
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,79 @@ | ||
import { homedir } from "node:os"; | ||
import { join } from "node:path"; | ||
|
||
interface CachedFeatureFlag { | ||
value: boolean; | ||
expiresAt: number; | ||
} | ||
|
||
interface FeatureFlagCache { | ||
[key: string]: CachedFeatureFlag; | ||
} | ||
|
||
const ONE_DAY_MS = 24 * 60 * 60 * 1000; | ||
|
||
function getFeatureFlagCachePath(): string { | ||
const configDir = join(homedir(), ".sfcompute"); | ||
return join(configDir, "feature-flags"); | ||
} | ||
|
||
export async function saveFeatureFlags(flags: FeatureFlagCache): Promise<void> { | ||
const cachePath = getFeatureFlagCachePath(); | ||
const configDir = join(homedir(), ".sfcompute"); | ||
|
||
try { | ||
await Deno.mkdir(configDir, { recursive: true }); | ||
await Deno.writeTextFile(cachePath, JSON.stringify(flags, null, 2)); | ||
} catch (error) { | ||
console.error("boba error saving feature flags:", error); | ||
} | ||
} | ||
|
||
export async function loadFeatureFlags(): Promise<FeatureFlagCache> { | ||
const cachePath = getFeatureFlagCachePath(); | ||
|
||
try { | ||
const cacheData = await Deno.readTextFile(cachePath); | ||
return JSON.parse(cacheData); | ||
} catch { | ||
return {}; | ||
} | ||
} | ||
|
||
export async function getCachedFeatureFlag( | ||
feature: string, | ||
accountId: string | ||
): Promise<CachedFeatureFlag | null> { | ||
const cache = await loadFeatureFlags(); | ||
const key = `${accountId}:${feature}`; | ||
const cachedFlag = cache[key]; | ||
|
||
if (!cachedFlag) { | ||
return null; | ||
} | ||
|
||
if (Date.now() > cachedFlag.expiresAt) { | ||
// Cache expired, remove it | ||
delete cache[key]; | ||
await saveFeatureFlags(cache); | ||
return null; | ||
} | ||
|
||
return cachedFlag; | ||
} | ||
|
||
export async function cacheFeatureFlag( | ||
feature: string, | ||
accountId: string, | ||
value: boolean | ||
): Promise<void> { | ||
const cache = await loadFeatureFlags(); | ||
const key = `${accountId}:${feature}`; | ||
|
||
cache[key] = { | ||
value, | ||
expiresAt: Date.now() + ONE_DAY_MS, | ||
}; | ||
|
||
await saveFeatureFlags(cache); | ||
} |
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
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,8 @@ | ||
declare namespace Deno { | ||
function writeTextFile(path: string, data: string): Promise<void>; | ||
function readTextFile(path: string): Promise<string>; | ||
function mkdir( | ||
path: string, | ||
options?: { recursive?: boolean } | ||
): Promise<void>; | ||
} |