Skip to content

Commit

Permalink
cache tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 5, 2023
1 parent 9abdf54 commit eab2f68
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-and-push-bsky-aws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- main
- cache-tweaks
env:
REGISTRY: ${{ secrets.AWS_ECR_REGISTRY_USEAST2_PACKAGES_REGISTRY }}
USERNAME: ${{ secrets.AWS_ECR_REGISTRY_USEAST2_PACKAGES_USERNAME }}
Expand Down
15 changes: 10 additions & 5 deletions packages/bsky/src/cache/read-through.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ export class ReadThroughCache<T> {
constructor(public redis: Redis, public opts: CacheOptions<T>) {}

private async _fetchMany(keys: string[]): Promise<Record<string, T | null>> {
let result: Record<string, T | null> = {}
if (this.opts.fetchManyMethod) {
return this.opts.fetchManyMethod(keys)
result = await this.opts.fetchManyMethod(keys)
} else {
const got = await Promise.all(keys.map((k) => this.opts.fetchMethod(k)))
for (let i = 0; i < keys.length; i++) {
result[keys[i]] = got[i] ?? null
}
}
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
// ensure caching negatives
for (const key of keys) {
result[key] ??= null
}
return result
}
Expand Down
9 changes: 7 additions & 2 deletions packages/bsky/src/services/label/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class LabelService {
...cacheOpts,
fetchMethod: async (subject: string) => {
const res = await fetchLabelsForSubjects(db, [subject])
return res[subject] ?? null
return res[subject] ?? []
},
fetchManyMethod: (subjects: string[]) =>
fetchLabelsForSubjects(db, subjects),
Expand Down Expand Up @@ -200,7 +200,7 @@ const fetchLabelsForSubjects = async (
.where('label.uri', 'in', subjects)
.selectAll()
.execute()
return res.reduce((acc, cur) => {
const labelMap = res.reduce((acc, cur) => {
acc[cur.uri] ??= []
acc[cur.uri].push({
...cur,
Expand All @@ -209,4 +209,9 @@ const fetchLabelsForSubjects = async (
})
return acc
}, {} as Record<string, Label[]>)
// ensure we cache negatives
for (const subject of subjects) {
labelMap[subject] ??= []
}
return labelMap
}

0 comments on commit eab2f68

Please sign in to comment.