eko / gocache

☔️ A complete Go cache library that brings you multiple ways of managing your caches
https://vincent.composieux.fr/article/i-wrote-gocache-a-complete-and-extensible-go-cache-library/
MIT License
2.46k stars 195 forks source link

about the ttl of ristretto and bigcache #135

Closed jinzhongjia closed 2 years ago

jinzhongjia commented 2 years ago

I found this problem when I set the validity period for the data.

i use store.Options to set the Expiration ,and the ttl I got is 0 when it based on ristretto or bigcache, but go-cache works normally. here is my code:

ristretto

    ctx := context.Background()
    ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
        NumCounters: 1000,
        MaxCost:     100,
        BufferItems: 64,
    })
    if err != nil {
        panic(err)
    }
    ristrettoStore := store.NewRistretto(ristrettoCache, nil)

    cacheManager := cache.New(ristrettoStore)
    err1 := cacheManager.Set(ctx, "my-key", []byte("my-value"), &store.Options{
        Expiration: time.Hour,
    })
    if err1 != nil {
        panic(err1)
    }

    _, tt, _ := cacheManager.GetWithTTL(ctx, "my-key")
    fmt.Println(tt)

bigcache

    ctx := context.Background()
    bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(5 * time.Minute))
    bigcacheStore := store.NewBigcache(bigcacheClient, nil) // No options provided (as second argument)

    cacheManager := cache.New(bigcacheStore)
    err1 := cacheManager.Set(ctx, "my-key", []byte("my-value"), &store.Options{
        Expiration: time.Hour,
    })
    if err1 != nil {
        panic(err1)
    }

    _, tt, _ := cacheManager.GetWithTTL(ctx, "my-key")
    fmt.Println(tt)
thejasn commented 2 years ago

Hey @yingyi666 thanks for filing the issue, Both ristretto and bigcache don't have complete implementations for GetWithTTL() as seen here and here. Checking upstream implementation,

jinzhongjia commented 2 years ago

ok,thanks,I understand.