Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class SimpleCache<T> {
private maxEntries: number;

constructor(config?: { enabled?: boolean; ttl?: Record<string, number>; 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) {
Expand Down
70 changes: 29 additions & 41 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class StellarSplitClient {
private _pluginInstances: StellarSplitPlugin[] = [];
private _pluginRegistry = new PluginRegistry();
private _dedup = new Deduplicator<Invoice>();
private _cache: SimpleCache<Invoice> | ICacheStore<Invoice> | null = null;
private _cache: SimpleCache<any> | ICacheStore<any> | null = null;
private _auditLogger: AuditLogger | null = null;
private _degradation: DegradationManager | null = null;
private _rateLimiter: RateLimiter | null = null;
Expand Down Expand Up @@ -620,9 +620,8 @@ export class StellarSplitClient {

this.contract = new Contract(config.contractId);

this._cache =
config.container?.getCacheStore() ??
(config.cache ? new SimpleCache<Invoice>(config.cache) : null);
this._cache = config.container?.getCacheStore() ??
(config.cache?.enabled ? new SimpleCache<any>(config.cache) : null);

if (config.telemetry) {
telemetry.init(config.telemetry);
Expand All @@ -643,10 +642,6 @@ export class StellarSplitClient {
);
}

if (config.cache && !config.container?.getCacheStore()) {
this._cache = new SimpleCache<Invoice>(config.cache);
}

// Initialize hooks
this._hooks = config.hooks ?? {};

Expand Down Expand Up @@ -1453,27 +1448,18 @@ export class StellarSplitClient {
args: any[],
fetcher: () => Promise<T>,
): Promise<T> {
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;
}

Expand Down Expand Up @@ -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<Record<string, unknown>>).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<Record<string, unknown>>).map((entry) => ({
creator: String(entry.creator ?? ""),
invoiceCount: Number(entry.invoice_count ?? 0),
totalVolume: BigInt(
(entry.total_volume as string | number | bigint) ?? 0,
),
}));
},
opts,
),
);
}

Expand Down
Loading