go-generics-cache icon indicating copy to clipboard operation
go-generics-cache copied to clipboard

Len() method missing from common cache interface

Open norri opened this issue 1 year ago • 4 comments

It would be useful to have Len() method available in common interface. I would need it for getting metrics for cache usage.

It is available if the cache is created with exact policy lru.NewCache()but if you want to use context cache.NewContext() then common interface is returned. len(c.Keys())could be used but it's still not the same.

Example:

ctx := context.Background()
var myCache *cache.Cache[string, int]
myCache = cache.NewContext(ctx, cache.AsLRU[string, int](lru.WithCapacity(10)))
// myCache is LRU cache but myCache.Len() is not available

norri avatar Jun 09 '23 06:06 norri

@norri Hi 👋 I'm not sure what do you mean this?

len(c.Keys()) could be used but it's still not the same.

I thought this meant len(c.Keys()) != c.Len() right?

Code-Hex avatar Jun 09 '23 07:06 Code-Hex

The result is the same. I just thought that there is no need to go through all the keys to get the lenght. Nevermind this my comment.

norri avatar Jun 09 '23 11:06 norri

@norri I commented your PR. If you don't kneed the changes, please close PR and this issues 🙏

Code-Hex avatar Jun 11 '23 06:06 Code-Hex

I still need this. Added new comments to PR.

norri avatar Jun 12 '23 08:06 norri