diff --git a/src/cache.ts b/src/cache.ts index 9e6424c..520c0ad 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -28,7 +28,7 @@ export class SimpleCache { private maxEntries: number; constructor(config?: { enabled?: boolean; ttl?: Record; ttlMs?: number; maxEntries?: number }) { - this.enabled = config?.enabled ?? (config?.ttl !== undefined || config?.ttlMs !== undefined); + this.enabled = config?.enabled ?? false; this.maxEntries = config?.maxEntries ?? (this.enabled ? 1000 : 0); this.ttlConfig = config?.ttl ?? {}; if (config?.ttlMs !== undefined) { diff --git a/src/client.ts b/src/client.ts index 2bd1d8d..5202182 100644 --- a/src/client.ts +++ b/src/client.ts @@ -463,7 +463,7 @@ export class StellarSplitClient { private _pluginInstances: StellarSplitPlugin[] = []; private _pluginRegistry = new PluginRegistry(); private _dedup = new Deduplicator(); - private _cache: SimpleCache | ICacheStore | null = null; + private _cache: SimpleCache | ICacheStore | null = null; private _auditLogger: AuditLogger | null = null; private _degradation: DegradationManager | null = null; private _rateLimiter: RateLimiter | null = null; @@ -620,9 +620,8 @@ export class StellarSplitClient { this.contract = new Contract(config.contractId); - this._cache = - config.container?.getCacheStore() ?? - (config.cache ? new SimpleCache(config.cache) : null); + this._cache = config.container?.getCacheStore() ?? + (config.cache?.enabled ? new SimpleCache(config.cache) : null); if (config.telemetry) { telemetry.init(config.telemetry); @@ -643,10 +642,6 @@ export class StellarSplitClient { ); } - if (config.cache && !config.container?.getCacheStore()) { - this._cache = new SimpleCache(config.cache); - } - // Initialize hooks this._hooks = config.hooks ?? {}; @@ -1453,27 +1448,18 @@ export class StellarSplitClient { args: any[], fetcher: () => Promise, ): Promise { - const isSimpleCache = - this._cache && typeof (this._cache as any).getStats === "function"; - - if (isSimpleCache) { - const key = `${methodName}:${JSON.stringify(args)}`; - const cached = (this._cache as any).get(key) as T | undefined; - if (cached) return cached; - } else if (this._cache && methodName === "getInvoice") { - const cached = this._cache.get(args[0]) as T | undefined; - if (cached) return cached; + if (!this._cache) { + return fetcher(); } - const result = await fetcher(); - - if (isSimpleCache) { - const key = `${methodName}:${JSON.stringify(args)}`; - (this._cache as any).set(key, result); - } else if (this._cache && methodName === "getInvoice") { - this._cache.set(args[0], result as any); + const key = `${methodName}:${JSON.stringify(args)}`; + const cached = this._cache.get(key); + if (cached !== undefined) { + return cached as T; } + const result = await fetcher(); + this._cache.set(key, result); return result; } @@ -5012,22 +4998,24 @@ export class StellarSplitClient { }): Promise< Array<{ creator: string; invoiceCount: number; totalVolume: bigint }> > { - return this._withTelemetry( - "getLeaderboard", - undefined, - async () => { - const operation = this.contract.call("get_leaderboard"); - const raw = await this._simulateView(operation, opts?.traceId); - if (!Array.isArray(raw)) return []; - return (raw as Array>).map((entry) => ({ - creator: String(entry.creator ?? ""), - invoiceCount: Number(entry.invoice_count ?? 0), - totalVolume: BigInt( - (entry.total_volume as string | number | bigint) ?? 0, - ), - })); - }, - opts, + return this._withCache("getLeaderboard", [], () => + this._withTelemetry( + "getLeaderboard", + undefined, + async () => { + const operation = this.contract.call("get_leaderboard"); + const raw = await this._simulateView(operation, opts?.traceId); + if (!Array.isArray(raw)) return []; + return (raw as Array>).map((entry) => ({ + creator: String(entry.creator ?? ""), + invoiceCount: Number(entry.invoice_count ?? 0), + totalVolume: BigInt( + (entry.total_volume as string | number | bigint) ?? 0, + ), + })); + }, + opts, + ), ); }