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

Feature Request: GetOrSetFunc

Open adrianosela opened this issue 6 months ago • 0 comments

Feature Request: GetOrSetFunc

Support for lazy initialization e.g.: for an IP based rate limiter implementation:

I want to be able to do this:

rateLimitForIP, _ := st.rateLimiters.GetOrSetFunc(
    ip,
    func() *rate.Limiter { return rate.NewLimiter(st.rateLimit, 1) },
    gcache.WithExpiration(itemTTL),
)

So that I can avoid doing this:

rateLimitForIP, _ := st.rateLimiters.GetOrSet(
    ip,
    rate.NewLimiter(st.rateLimit, 1),
    gcache.WithExpiration(itemTTL),
)

Because rate limiter object is heavy and will generate pressure on the garbage collector for not-set items.

adrianosela avatar May 15 '25 04:05 adrianosela