mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-19 19:03:36 +00:00
20 lines
511 B
TypeScript
20 lines
511 B
TypeScript
export const createCircularCache = <T>(bufferSize = 10) => {
|
|
const bufferItems: { id: string; item: T }[] = [];
|
|
let bufferIndex = 0;
|
|
|
|
const push = (id: string, item: T) => {
|
|
if (bufferItems.length < bufferSize) {
|
|
bufferItems.push({ id, item });
|
|
} else {
|
|
bufferItems[bufferIndex] = { id, item };
|
|
}
|
|
bufferIndex = (bufferIndex + 1) % bufferSize;
|
|
};
|
|
|
|
const getItem = (id: string) => {
|
|
return bufferItems.find((i) => i.id === id)?.item;
|
|
};
|
|
|
|
return { push, getItem };
|
|
};
|