feat: introduce lru cache for requests
This commit is contained in:
parent
6b3a14cf1e
commit
a94781df83
13 changed files with 90 additions and 39 deletions
65
composables/cache.ts
Normal file
65
composables/cache.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import LRU from 'lru-cache'
|
||||
import type { Account, Status } from 'masto'
|
||||
|
||||
const cache = new LRU<string, any>({
|
||||
max: 1000,
|
||||
})
|
||||
|
||||
if (process.dev)
|
||||
// eslint-disable-next-line no-console
|
||||
console.log({ cache })
|
||||
|
||||
export function setCached(key: string, value: any) {
|
||||
cache.set(key, value)
|
||||
}
|
||||
|
||||
export function fetchStatus(id: string) {
|
||||
const key = `status:${id}`
|
||||
const cached = cache.get(key)
|
||||
if (cached)
|
||||
return cached
|
||||
const promise = masto.statuses.fetch(id)
|
||||
.then((status) => {
|
||||
cacheStatus(status)
|
||||
return status
|
||||
})
|
||||
cache.set(key, promise)
|
||||
return promise
|
||||
}
|
||||
|
||||
export function fetchAccount(id: string) {
|
||||
const key = `account:${id}`
|
||||
const cached = cache.get(key)
|
||||
if (cached)
|
||||
return cached
|
||||
const promise = masto.accounts.fetch(id)
|
||||
.then((account) => {
|
||||
cacheAccount(account)
|
||||
return account
|
||||
})
|
||||
cache.set(key, promise)
|
||||
return promise
|
||||
}
|
||||
|
||||
export function fetchAccountByName(acct: string) {
|
||||
const key = `account:${acct}`
|
||||
const cached = cache.get(key)
|
||||
if (cached)
|
||||
return cached
|
||||
const account = masto.accounts.fetch(acct)
|
||||
.then((r) => {
|
||||
cacheAccount(r)
|
||||
return r
|
||||
})
|
||||
cache.set(key, account)
|
||||
return account
|
||||
}
|
||||
|
||||
export function cacheStatus(status: Status) {
|
||||
setCached(`status:${status.id}`, status)
|
||||
}
|
||||
|
||||
export function cacheAccount(account: Account) {
|
||||
setCached(`account:${account.id}`, account)
|
||||
setCached(`account:${account.acct}`, account)
|
||||
}
|
|
@ -14,7 +14,7 @@ export function getAccountPath(account: Account) {
|
|||
}
|
||||
|
||||
export function getStatusPath(status: Status) {
|
||||
return `${getAccountPath(status.account)}/${status.id}`
|
||||
return `/status/${status.id}`
|
||||
}
|
||||
|
||||
// Batch requests for relationships when used in the UI
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue