jellydator / ttlcache

An in-memory cache with item expiration and generics
MIT License
883 stars 115 forks source link

Added GetOrSetFunc #111

Closed owen5mith closed 10 months ago

owen5mith commented 10 months ago

Hello,

This PR adds a GetOrSetFunc method to the cache. The main motivation for this was to avoid allocations when using the cache to store slices. GetOrSetFunc mirrors GetOrSet, however takes a valueFunc rather than a value. This function is only called if the key was not found in the cache, avoiding an unnecessary allocation and executing quicker.

I'm conscious I don't have a corresponding issue for this, so feel free to decline if this isn't something you think should be part of this library.