@supabase-cache-helpers/postgrest-server
Version:
A collection of server-side caching utilities for working with Supabase.
144 lines (141 loc) • 3.71 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/stores/index.ts
var stores_exports = {};
__export(stores_exports, {
MemoryStore: () => MemoryStore,
RedisStore: () => RedisStore
});
module.exports = __toCommonJS(stores_exports);
// src/stores/memory.ts
var MemoryStore = class {
state;
capacity;
name = "memory";
constructor(config) {
this.state = config.persistentMap;
this.capacity = config.capacity;
}
setMostRecentlyUsed(key, value) {
this.state.delete(key);
this.state.set(key, value);
}
async get(key) {
const value = this.state.get(key);
if (!value) {
return Promise.resolve(void 0);
}
if (value.expires <= Date.now()) {
await this.remove(key);
}
if (this.capacity) {
this.setMostRecentlyUsed(key, value);
}
return Promise.resolve(value.entry);
}
async set(key, entry) {
if (this.capacity) {
this.setMostRecentlyUsed(key, {
expires: entry.staleUntil,
entry
});
} else {
this.state.set(key, {
expires: entry.staleUntil,
entry
});
}
if (this.capacity && this.state.size > this.capacity) {
const oldestKey = this.state.keys().next().value;
if (oldestKey !== void 0) {
this.state.delete(oldestKey);
}
}
return Promise.resolve();
}
async remove(keys) {
const cacheKeys = Array.isArray(keys) ? keys : [keys];
for (const key of cacheKeys) {
this.state.delete(key);
}
return Promise.resolve();
}
async removeByPrefix(prefix) {
for (const key of this.state.keys()) {
if (key.startsWith(prefix)) {
this.state.delete(key);
}
}
}
};
// src/stores/redis.ts
var RedisStore = class {
redis;
name = "redis";
prefix;
constructor(config) {
this.redis = config.redis;
this.prefix = config.prefix || "sbch";
}
buildCacheKey(key) {
return [this.prefix, key].join("::");
}
async get(key) {
const res = await this.redis.get(this.buildCacheKey(key));
if (!res) return;
return JSON.parse(res);
}
async set(key, entry) {
await this.redis.set(
this.buildCacheKey(key),
JSON.stringify(entry),
"PXAT",
entry.staleUntil
);
}
async remove(keys) {
const cacheKeys = (Array.isArray(keys) ? keys : [keys]).map(
(key) => this.buildCacheKey(key).toString()
);
this.redis.del(...cacheKeys);
}
async removeByPrefix(prefix) {
const pattern = `${prefix}*`;
let cursor = "0";
do {
const [nextCursor, keys] = await this.redis.scan(
cursor,
"MATCH",
pattern,
"COUNT",
100
);
cursor = nextCursor;
if (keys.length > 0) {
await this.redis.del(...keys);
}
} while (cursor !== "0");
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MemoryStore,
RedisStore
});
//# sourceMappingURL=stores.cjs.map