2024-09-07 16:40:16 -05:00
|
|
|
import Keyv from "keyv";
|
2025-03-25 08:26:53 +00:00
|
|
|
import { CACHE_PATH, CACHE_TIME } from "./env.js";
|
2025-04-05 15:13:05 +01:00
|
|
|
import logger from "./logger.js";
|
|
|
|
|
|
|
|
const log = logger.extend("cache");
|
2024-09-07 16:40:16 -05:00
|
|
|
|
2024-09-25 13:37:32 -05:00
|
|
|
async function createStore() {
|
|
|
|
if (!CACHE_PATH || CACHE_PATH === "in-memory") return undefined;
|
|
|
|
else if (CACHE_PATH.startsWith("redis://")) {
|
|
|
|
const { default: KeyvRedis } = await import("@keyv/redis");
|
2025-04-05 15:13:05 +01:00
|
|
|
log(`Using redis cache at ${CACHE_PATH}`);
|
2024-09-25 13:37:32 -05:00
|
|
|
return new KeyvRedis(CACHE_PATH);
|
|
|
|
} else if (CACHE_PATH.startsWith("sqlite://")) {
|
|
|
|
const { default: KeyvSqlite } = await import("@keyv/sqlite");
|
2025-04-05 15:13:05 +01:00
|
|
|
log(`Using sqlite cache at ${CACHE_PATH}`);
|
2024-09-25 13:37:32 -05:00
|
|
|
return new KeyvSqlite(CACHE_PATH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const store = await createStore();
|
|
|
|
|
|
|
|
store?.on("error", (err) => {
|
2025-04-05 15:13:05 +01:00
|
|
|
log("Connection Error", err);
|
2024-09-07 16:40:16 -05:00
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2024-09-25 13:37:32 -05:00
|
|
|
const opts = store ? { store } : {};
|
|
|
|
|
2025-03-25 08:26:53 +00:00
|
|
|
/** A cache that maps a domain to a pubkey ( domain -> pubkey ) */
|
2025-04-05 15:31:28 +01:00
|
|
|
export const pubkeyDomains = new Keyv<string | undefined>({
|
2024-09-26 12:48:13 -05:00
|
|
|
...opts,
|
|
|
|
namespace: "domains",
|
2025-03-25 08:26:53 +00:00
|
|
|
ttl: CACHE_TIME * 1000,
|
2024-09-26 12:48:13 -05:00
|
|
|
});
|
|
|
|
|
2025-03-25 08:26:53 +00:00
|
|
|
/** A cache that maps a pubkey to a set of blossom servers ( pubkey -> servers ) */
|
2025-04-05 15:31:28 +01:00
|
|
|
export const pubkeyServers = new Keyv<string[] | undefined>({
|
2024-09-25 13:37:32 -05:00
|
|
|
...opts,
|
|
|
|
namespace: "servers",
|
2025-03-25 08:26:53 +00:00
|
|
|
ttl: CACHE_TIME * 1000,
|
2024-09-25 13:37:32 -05:00
|
|
|
});
|
|
|
|
|
2025-03-25 08:26:53 +00:00
|
|
|
/** A cache that maps a pubkey to a set of relays ( pubkey -> relays ) */
|
2025-04-05 15:31:28 +01:00
|
|
|
export const pubkeyRelays = new Keyv<string[] | undefined>({
|
2024-09-25 13:37:32 -05:00
|
|
|
...opts,
|
|
|
|
namespace: "relays",
|
2025-03-25 08:26:53 +00:00
|
|
|
ttl: CACHE_TIME * 1000,
|
|
|
|
});
|
|
|
|
|
2025-04-05 15:13:05 +01:00
|
|
|
/** A cache that maps a pubkey + path to sha256 hash of the blob ( pubkey/path -> sha256 ) */
|
|
|
|
export const pathBlobs = new Keyv<string | undefined>({
|
2025-03-25 08:26:53 +00:00
|
|
|
...opts,
|
|
|
|
namespace: "paths",
|
|
|
|
ttl: CACHE_TIME * 1000,
|
2024-09-25 13:37:32 -05:00
|
|
|
});
|
2025-04-05 15:13:05 +01:00
|
|
|
|
|
|
|
/** A cache that maps a sha256 hash to a set of URLs that had the blob ( sha256 -> URLs ) */
|
|
|
|
export const blobURLs = new Keyv<string[] | undefined>({
|
|
|
|
...opts,
|
|
|
|
namespace: "blobs",
|
|
|
|
ttl: CACHE_TIME * 1000,
|
|
|
|
});
|