akshitkrnagpal / akshitkrnagpal/keyv-dataloader

Add optional performance metrics tracking

Ouverte
#8 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
enhancement
Langage dominant
TypeScript
Étoiles
1
Forks
0
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

Currently, there's no way to measure the performance of the cache layer versus the database layer. Adding metrics would help users optimize their applications.

### Proposed Solution
1. Add optional metrics tracking to the KeyvDataLoader:

```typescript
export interface KeyvDataLoaderOptions {
// ... existing options

/**
* Optional metrics configuration
*/
metrics?: {
/**
* Function to call when cache hit occurs
*/
onCacheHit?: (key: K, duration: number) => void;

/**
* Function to call when cache miss occurs
*/
onCacheMiss?: (key: K) => void;

/**
* Function to call when batch loading occurs
*/
onBatchLoad?: (keys: ReadonlyArray, duration: number) => void;

/**
* Function to call when cache error occurs
*/
onCacheError?: (error: Error, operation: string) => void;
};
}
```

2. Implement the metrics tracking in the load method:

```typescript
public async load(key: K): Promise {
const cacheKey = this.cacheKeyFn(key);

// Measure cache access time
const cacheStartTime = performance.now();
try {
const cachedValue = await this.cache.get(cacheKey);
const cacheDuration = performance.now() - cacheStartTime;

if (cachedValue !== undefined) {
// Cache hit
if (this.options.metrics?.onCacheHit) {
this.options.metrics.onCacheHit(key, cacheDuration);
}

// Handle error values
if (cachedValue instanceof Error) {
throw cachedValue;
}

return cachedValue as V;
} else {
// Cache miss
if (this.options.metrics?.onCacheMiss) {
this.options.metrics.onCacheMiss(key);
}
}
} catch (error) {
// Cache error
if (this.options.metrics?.onCacheError) {
this.options.metrics.onCacheError(error as Error, 'get');
}
}

// Load from batch function
const batchStartTime = performance.now();
const value = await this.dataloader.load(key);
const batchDuration = performance.now() - batchStartTime;

if (this.options.metrics?.onBatchLoad) {
this.options.metrics.onBatchLoad([key], batchDuration);
}

return value;
}
```

3. Add usage examples to the documentation:

```typescript
// Example usage with metrics
const loader = new KeyvDataLoader({
batchLoadFn: fetchDataFromDB,
ttl: 60 * 1000,
metrics: {
onCacheHit: (key, duration) => {
console.log(`Cache hit for ${key}: ${duration.toFixed(2)}ms`);
},
onCacheMiss: (key) => {
console.log(`Cache miss for ${key}`);
},
onBatchLoad: (keys, duration) => {
console.log(`Batch loaded ${keys.length} keys in ${duration.toFixed(2)}ms`);
},
onCacheError: (error, operation) => {
console.error(`Cache error during ${operation}:`, error);
}
}
});
```

This feature would help users monitor performance and identify bottlenecks in their cache configuration.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.