Skip to content

Commit 3c623f5

Browse files
committed
fix: add 7-day TTL to org listing cache
getCachedOrganizations() now checks updated_at against a 7-day TTL. Stale entries are ignored, forcing a fresh API fetch. This ensures users who join new orgs see them within a bounded window without needing to run --fresh or auth status.
1 parent 0bef71c commit 3c623f5

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

src/lib/db/regions.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,32 @@ export type CachedOrg = {
160160
name: string;
161161
};
162162

163+
/**
164+
* Maximum age (ms) for cached organization entries.
165+
* Entries older than this are considered stale and ignored, forcing a
166+
* fresh API fetch. 7 days balances offline usability with picking up
167+
* new org memberships.
168+
*/
169+
const ORG_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
170+
163171
/**
164172
* Get all cached organizations with id, slug, and name.
165173
*
166-
* Returns organizations that have all three fields populated in the cache.
167-
* Rows with missing `org_id` or `org_name` (from before schema v9) are
168-
* excluded — callers should fall back to the API when the result is empty.
174+
* Returns organizations that have all three fields populated and were
175+
* updated within the TTL window. Rows with missing `org_id` or `org_name`
176+
* (from before schema v9) or stale `updated_at` are excluded — callers
177+
* should fall back to the API when the result is empty.
169178
*
170-
* @returns Array of cached org entries, or empty if cache is cold/incomplete
179+
* @returns Array of cached org entries, or empty if cache is cold/stale/incomplete
171180
*/
172181
export async function getCachedOrganizations(): Promise<CachedOrg[]> {
173182
const db = getDatabase();
183+
const cutoff = Date.now() - ORG_CACHE_TTL_MS;
174184
const rows = db
175185
.query(
176-
`SELECT org_slug, org_id, org_name FROM ${TABLE} WHERE org_id IS NOT NULL AND org_name IS NOT NULL`
186+
`SELECT org_slug, org_id, org_name FROM ${TABLE} WHERE org_id IS NOT NULL AND org_name IS NOT NULL AND updated_at > ?`
177187
)
178-
.all() as Pick<OrgRegionRow, "org_slug" | "org_id" | "org_name">[];
188+
.all(cutoff) as Pick<OrgRegionRow, "org_slug" | "org_id" | "org_name">[];
179189

180190
return rows.map((row) => ({
181191
slug: row.org_slug,

0 commit comments

Comments
 (0)