New reclaimable cache. Must provide the function to populate values. The maximum number of values can be optionally set (zero means unlimited).
(f func(TKey) TValue, maxLen int)
| 13 | // New reclaimable cache. Must provide the function to populate values. The maximum number of values |
| 14 | // can be optionally set (zero means unlimited). |
| 15 | func New[TKey comparable, TValue any](f func(TKey) TValue, maxLen int) Cache[TKey, TValue] { |
| 16 | return Cache[TKey, TValue]{ |
| 17 | max: maxLen, |
| 18 | f: f, |
| 19 | pool: &sync.Pool{ |
| 20 | New: func() any { |
| 21 | return make(map[TKey]TValue) |
| 22 | }, |
| 23 | }, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func (c Cache[TKey, TValue]) Get(key TKey) TValue { |
| 28 | m := c.pool.Get().(map[TKey]TValue) |