-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cache did docs in redis * drop table * expire from redis * fix tests * add cache class * update api * refactor * filter negative labels * fix up dev-env * refactor did cache to use new redis cache class * tidy * ensure caching negatives * redis cache tests * remove timeout on did cache * fix ns in test * rename driver * add timeout & fail open * add test for timeout & fail open * small pr feedback * refactor caches * bugfixg * test for caching negative values * little more to cache * wire up cache cfg * switch from redis scratch to redis * fix build issues * use different redis clients for tests * fix test * fix flaky test * use separate db for redis cache
- Loading branch information
Showing
29 changed files
with
802 additions
and
288 deletions.
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,143 @@ | ||
import { cacheLogger as log } from '../logger' | ||
import { Redis } from '../redis' | ||
|
||
export type CacheItem<T> = { | ||
val: T | null // null here is for negative caching | ||
updatedAt: number | ||
} | ||
|
||
export type CacheOptions<T> = { | ||
staleTTL: number | ||
maxTTL: number | ||
fetchMethod: (key: string) => Promise<T | null> | ||
fetchManyMethod?: (keys: string[]) => Promise<Record<string, T | null>> | ||
} | ||
|
||
export class ReadThroughCache<T> { | ||
constructor(public redis: Redis, public opts: CacheOptions<T>) {} | ||
|
||
private async _fetchMany(keys: string[]): Promise<Record<string, T | null>> { | ||
if (this.opts.fetchManyMethod) { | ||
return this.opts.fetchManyMethod(keys) | ||
} | ||
const got = await Promise.all(keys.map((k) => this.opts.fetchMethod(k))) | ||
const result: Record<string, T | null> = {} | ||
for (let i = 0; i < keys.length; i++) { | ||
result[keys[i]] = got[i] ?? null | ||
} | ||
return result | ||
} | ||
|
||
private async fetchAndCache(key: string): Promise<T | null> { | ||
const fetched = await this.opts.fetchMethod(key) | ||
this.set(key, fetched).catch((err) => | ||
log.error({ err, key }, 'failed to set cache value'), | ||
) | ||
return fetched | ||
} | ||
|
||
private async fetchAndCacheMany(keys: string[]): Promise<Record<string, T>> { | ||
const fetched = await this._fetchMany(keys) | ||
this.setMany(fetched).catch((err) => | ||
log.error({ err, keys }, 'failed to set cache values'), | ||
) | ||
return removeNulls(fetched) | ||
} | ||
|
||
async get(key: string, opts?: { revalidate?: boolean }): Promise<T | null> { | ||
if (opts?.revalidate) { | ||
return this.fetchAndCache(key) | ||
} | ||
let cached: CacheItem<T> | null | ||
try { | ||
const got = await this.redis.get(key) | ||
cached = got ? JSON.parse(got) : null | ||
} catch (err) { | ||
cached = null | ||
log.warn({ key, err }, 'failed to fetch value from cache') | ||
} | ||
if (!cached || this.isExpired(cached)) { | ||
return this.fetchAndCache(key) | ||
} | ||
if (this.isStale(cached)) { | ||
this.fetchAndCache(key).catch((err) => | ||
log.warn({ key, err }, 'failed to refresh stale cache value'), | ||
) | ||
} | ||
return cached.val | ||
} | ||
|
||
async getMany( | ||
keys: string[], | ||
opts?: { revalidate?: boolean }, | ||
): Promise<Record<string, T>> { | ||
if (opts?.revalidate) { | ||
return this.fetchAndCacheMany(keys) | ||
} | ||
let cached: Record<string, string> | ||
try { | ||
cached = await this.redis.getMulti(keys) | ||
} catch (err) { | ||
cached = {} | ||
log.warn({ keys, err }, 'failed to fetch values from cache') | ||
} | ||
|
||
const stale: string[] = [] | ||
const toFetch: string[] = [] | ||
const results: Record<string, T> = {} | ||
for (const key of keys) { | ||
const val = cached[key] ? (JSON.parse(cached[key]) as CacheItem<T>) : null | ||
if (!val || this.isExpired(val)) { | ||
toFetch.push(key) | ||
} else if (this.isStale(val)) { | ||
stale.push(key) | ||
} else if (val.val) { | ||
results[key] = val.val | ||
} | ||
} | ||
const fetched = await this.fetchAndCacheMany(toFetch) | ||
this.fetchAndCacheMany(stale).catch((err) => | ||
log.warn({ keys, err }, 'failed to refresh stale cache values'), | ||
) | ||
return { | ||
...results, | ||
...fetched, | ||
} | ||
} | ||
|
||
async set(key: string, val: T | null) { | ||
await this.setMany({ [key]: val }) | ||
} | ||
|
||
async setMany(vals: Record<string, T | null>) { | ||
const items: Record<string, string> = {} | ||
for (const key of Object.keys(vals)) { | ||
items[key] = JSON.stringify({ | ||
val: vals[key], | ||
updatedAt: Date.now(), | ||
}) | ||
} | ||
await this.redis.setMulti(items, this.opts.maxTTL) | ||
} | ||
|
||
async clearEntry(key: string) { | ||
await this.redis.del(key) | ||
} | ||
|
||
isExpired(result: CacheItem<T>) { | ||
return Date.now() > result.updatedAt + this.opts.maxTTL | ||
} | ||
|
||
isStale(result: CacheItem<T>) { | ||
return Date.now() > result.updatedAt + this.opts.staleTTL | ||
} | ||
} | ||
|
||
const removeNulls = <T>(obj: Record<string, T | null>): Record<string, T> => { | ||
return Object.entries(obj).reduce((acc, [key, val]) => { | ||
if (val !== null) { | ||
acc[key] = val | ||
} | ||
return acc | ||
}, {} as Record<string, T>) | ||
} |
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
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
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
14 changes: 14 additions & 0 deletions
14
packages/bsky/src/db/migrations/20231205T000257238Z-remove-did-cache.ts
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,14 @@ | ||
import { Kysely } from 'kysely' | ||
|
||
export async function up(db: Kysely<unknown>): Promise<void> { | ||
await db.schema.dropTable('did_cache').execute() | ||
} | ||
|
||
export async function down(db: Kysely<unknown>): Promise<void> { | ||
await db.schema | ||
.createTable('did_cache') | ||
.addColumn('did', 'varchar', (col) => col.primaryKey()) | ||
.addColumn('doc', 'jsonb', (col) => col.notNull()) | ||
.addColumn('updatedAt', 'bigint', (col) => col.notNull()) | ||
.execute() | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.