Skip to content

Commit fa069d9

Browse files
committed
Add comprehensive cache statistics
The `CacheStats` class provides detailed metrics like hit/miss counts, load success/failure counts, total load time, and eviction counts. It also offers derived metrics such as hit/miss rates, load failure rate, and average load penalty. The design is inspired by Caffeine. `BasicClientSideCache` now uses a `StatsCounter` to accumulate these statistics, exposed via a new `stats()` method. The previous `cacheHits()` and `cacheMisses()` methods have been removed. A `recordStats` option (default: true) in `ClientSideCacheConfig` allows disabling statistics collection.
1 parent 95e2db1 commit fa069d9

File tree

4 files changed

+786
-110
lines changed

4 files changed

+786
-110
lines changed

docs/v5.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ There are two ways to implement client side caching:
104104

105105
```javascript
106106
const client = createClient({
107-
RESP: 3,
107+
RESP: 3,
108108
clientSideCache: {
109109
ttl: 0, // Time-to-live in milliseconds (0 = no expiration)
110110
maxEntries: 0, // Maximum entries to store (0 = unlimited)
@@ -142,8 +142,17 @@ cache.invalidate(key);
142142
cache.clear();
143143

144144
// Get cache metrics
145-
const hits = cache.cacheHits();
146-
const misses = cache.cacheMisses();
145+
// `cache.stats()` returns a `CacheStats` object with comprehensive statistics.
146+
const statistics = cache.stats();
147+
148+
// Key metrics:
149+
const hits = statistics.hitCount; // Number of cache hits
150+
const misses = statistics.missCount; // Number of cache misses
151+
const hitRate = statistics.hitRate(); // Cache hit rate (0.0 to 1.0)
152+
153+
// Many other metrics are available on the `statistics` object, e.g.:
154+
// statistics.missRate(), statistics.loadSuccessCount,
155+
// statistics.averageLoadPenalty(), statistics.requestCount()
147156
```
148157

149158
## Pooled Caching

0 commit comments

Comments
 (0)