-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from autonomys/feat/back-hot-cache
File download cache
- Loading branch information
Showing
7 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { LRUCache } from 'lru-cache' | ||
import { bufferToAsyncIterable } from '../../utils/async.js' | ||
|
||
const ONE_GB = 1024 ** 3 | ||
|
||
const cache = new LRUCache<string, Buffer>({ | ||
maxSize: Number(process.env.MAX_CACHE_SIZE ?? ONE_GB), | ||
}) | ||
|
||
const has = (cid: string) => { | ||
const has = cache.has(cid) | ||
return has | ||
} | ||
|
||
const get = (cid: string) => { | ||
const value = cache.get(cid) | ||
if (!value) { | ||
return null | ||
} | ||
|
||
return bufferToAsyncIterable(value) | ||
} | ||
|
||
const set = async function* ( | ||
cid: string, | ||
value: AsyncIterable<Buffer>, | ||
): AsyncIterable<Buffer> { | ||
let buffer = Buffer.alloc(0) | ||
for await (const chunk of value) { | ||
buffer = Buffer.concat([buffer, chunk]) | ||
yield chunk | ||
} | ||
cache.set(cid, buffer, { | ||
sizeCalculation: (value) => value.length, | ||
}) | ||
} | ||
|
||
export const downloadCache = { | ||
has, | ||
get, | ||
set, | ||
} |
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