Skip to content

Commit

Permalink
feat: add buffer store LRU cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Aug 26, 2024
1 parent 1eb27b0 commit de891d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@web3-storage/filecoin-api": "^7.2.0",
"@web3-storage/filecoin-client": "^3.3.3",
"fzstd": "^0.1.0",
"lru-cache": "^11.0.0",
"multiformats": "12.0.1",
"p-all": "^5.0.0",
"p-retry": "^5.1.2",
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/store/aggregator-buffer-store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CBOR } from '@ucanto/core'
import { parseLink } from '@ucanto/server'
import { LRUCache } from 'lru-cache'

import { createBucketClient } from './bucket-client.js'

Expand Down Expand Up @@ -67,3 +68,25 @@ export function createClient (conf, context) {
decodeBucketResponse
})
}

const CACHE_MAX = 10_000

/**
* @param {import('@web3-storage/filecoin-api/aggregator/api').BufferStore} bufferStore
* @returns {import('@web3-storage/filecoin-api/aggregator/api').BufferStore}
*/
export const withCache = (bufferStore) => {
/** @type {LRUCache<string, import('@web3-storage/filecoin-api/aggregator/api').BufferRecord>} */
const cache = new LRUCache({ max: CACHE_MAX })
return {
...bufferStore,
async get (key) {
const cacheKey = key.toString()
const cached = cache.get(cacheKey)
if (cached) return { ok: cached }
const res = await bufferStore.get(key)
if (res.ok) cache.set(cacheKey, res.ok)
return res
}
}
}
10 changes: 6 additions & 4 deletions packages/functions/src/aggregator/handle-buffer-queue-message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Sentry from '@sentry/serverless'

import { createClient as createBufferStoreClient } from '@w3filecoin/core/src/store/aggregator-buffer-store.js'
import { createClient as createBufferStoreClient, withCache as withBufferStoreCache } from '@w3filecoin/core/src/store/aggregator-buffer-store.js'
import { createClient as createBufferQueueClient, decodeMessage } from '@w3filecoin/core/src/queue/buffer-queue.js'
import { createClient as createAggregateOfferQueueClient } from '@w3filecoin/core/src/queue/aggregate-offer-queue.js'
import * as aggregatorEvents from '@web3-storage/filecoin-api/aggregator/events'
Expand Down Expand Up @@ -83,9 +83,11 @@ function getContext () {
} = getEnv()

return {
bufferStore: createBufferStoreClient(
{ region: bufferStoreBucketRegion },
{ name: bufferStoreBucketName }
bufferStore: withBufferStoreCache(
createBufferStoreClient(
{ region: bufferStoreBucketRegion },
{ name: bufferStoreBucketName }
)
),
bufferQueue: createBufferQueueClient(
{ region: bufferQueueRegion },
Expand Down

0 comments on commit de891d9

Please sign in to comment.