Skip to content

Cannot use go-cache as backend cache for cache.NewMetric #236

Open
@zclyne

Description

Hi, I was trying to use go-cache as a backend cache for a cache with prometheus metrics. I expose the prometheus metrics via http localhost:8080/metrics but this endpoint returns empty response, i.e. no metrics.

Steps for Reproduction

ctx := context.Background()

// initialize backend go-cache store
gocacheClient := gocache.New(5*time.Minute, 10*time.Minute)
gocacheStore := gocache_store.NewGoCache(gocacheClient)

// initialize prometheus metrics store
registry := prometheus.NewRegistry()
promMetrics := metrics.NewPrometheus("my-test-app", metrics.WithRegisterer(registry))

// initialize cache with metrics
cacheManager := cache.NewMetric[any](promMetrics, gocacheStore)
err := cacheManager.Set(ctx, "my-key", []byte("my-value"))

value, err := cacheManager.Get(ctx, "my-key")
fmt.Printf("%s", value)

After inspecting the code I found that gocacheStore doesn't implement the SetterCacheInterface interface, which causes switch...case... miss defined in the function below

func (c *MetricCache[T]) updateMetrics(cache CacheInterface[T]) {
switch current := cache.(type) {
case *ChainCache[T]:
for _, cache := range current.GetCaches() {
c.updateMetrics(cache)
}
case SetterCacheInterface[T]:
c.metrics.RecordFromCodec(current.GetCodec())
}
}

Expected behavior: metrics should be updated and collected by the prometheus registry

Actual behavior: no metrics collected

Platforms:

Go: 1.20
Linux: 3.10.0-1160.105.1.el7.x86_64

Versions:

github.com/eko/gocache/lib/v4 v4.1.5
github.com/eko/gocache/store/go_cache/v4 v4.2.1

If fixing this issue will be on the roadmap, I can help contribute. Thanks!

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions