UNPKG

@supabase-cache-helpers/postgrest-server

Version:

A collection of server-side caching utilities for working with Supabase.

116 lines (115 loc) 2.64 kB
// 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"); } }; export { MemoryStore, RedisStore }; //# sourceMappingURL=stores.js.map