cache icon indicating copy to clipboard operation
cache copied to clipboard

Enhance the StatsCounter interface to support size tracking of the cache

Open kakkoyun opened this issue 2 years ago • 1 comments

Hey 👋 I'm a keen user of the library. Thanks a lot for the work. It's a fantastic package. Simple and functional.

I recently had a use case where I'd like to keep track of the cache size. I think it's doable with minimal change to the StatsCounter interface and stats.

I don't know if the size calculation should be left to the interface's implementer or handled by the caching layer. I'd love to hear your opinion on it @nqv

It could also be achieved if the withInsertionListener is public https://github.com/goburrow/cache/blob/f6da914dd6e3546dffa8802919dbca80cd33abe3/local.go#L575-L576

I would be happy to work if you're willing to accept my contribution.

type Stats struct {
	HitCount         uint64
	MissCount        uint64
	LoadSuccessCount uint64
	LoadErrorCount   uint64
	TotalLoadTime    time.Duration
	EvictionCount    uint64
+	Size uint64
}
// StatsCounter accumulates statistics of a cache.
type StatsCounter interface {
	// RecordHits records cache hits.
	RecordHits(count uint64)

	// RecordMisses records cache misses.
	RecordMisses(count uint64)

	// RecordLoadSuccess records successful load of a new entry.
	RecordLoadSuccess(loadTime time.Duration)

	// RecordLoadError records failed load of a new entry.
	RecordLoadError(loadTime time.Duration)

	// RecordInsertion records the insertion of an entry from the cache.
+	RecordInsertion(v Value) // or directly the size of the value

	// RecordEviction records eviction of an entry from the cache.
+	RecordEviction(v Value) // or directly the size of the value

	// Snapshot writes snapshot of this counter values to the given Stats pointer.
	Snapshot(*Stats)
}

kakkoyun avatar Apr 06 '23 13:04 kakkoyun

@nqv I could be creating an XY-problem https://xyproblem.info/

I want to reiterate. I want to expose a metric to show the current cache sizes of various caches we use. I would be glad if you have a better solution proposal.

kakkoyun avatar Apr 07 '23 06:04 kakkoyun