mirror of
https://github.com/hanzoai/traces.git
synced 2026-10-30 18:20:41 +00:00
* Add new concurrency config options and validate * Reduce allocations of IDmap when replaying wal blocks since we know the number of traces * Generator read from kafka concurrency, add shared ingest lag metric, option to uniqify strings * Memoize spanmetrics sanitizelabelname, move to better location * Moved to shared queue for localblocks wal completion, allow concurrency within a tenant * Honor max live traces in non-flushing local blocks processor * Add metric for enqueue time * Add missing mutex lock for enqueuing on replay * Fix mutex lock while reloading blocks * Increase default concurrency * Simplify local blocks complete queue * Remove uniqify, fix test * Switch completequeue to reference counting and shut it down when last local blocks processor is stopped * cache rename/cleanup * Cleanup/denoising PR * Lint/cleanup * lint * Update config manifest * fix race condition * cleanup * review feedback * Update config manifest * changelog
42 lines
927 B
Go
42 lines
927 B
Go
package reclaimable
|
|
|
|
import "sync"
|
|
|
|
// Cache is an in-memory key/value cache that is best-effort. It does not
|
|
// reserve dedicated space for values and can be reclaimed by the garbage collector as needed.
|
|
type Cache[TKey comparable, TValue any] struct {
|
|
max int
|
|
f func(TKey) TValue
|
|
pool *sync.Pool
|
|
}
|
|
|
|
// New reclaimable cache. Must provide the function to populate values. The maximum number of values
|
|
// can be optionally set (zero means unlimited).
|
|
func New[TKey comparable, TValue any](f func(TKey) TValue, maxLen int) Cache[TKey, TValue] {
|
|
return Cache[TKey, TValue]{
|
|
max: maxLen,
|
|
f: f,
|
|
pool: &sync.Pool{
|
|
New: func() any {
|
|
return make(map[TKey]TValue)
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c Cache[TKey, TValue]) Get(key TKey) TValue {
|
|
m := c.pool.Get().(map[TKey]TValue)
|
|
defer c.pool.Put(m)
|
|
|
|
if v, ok := m[key]; ok {
|
|
return v
|
|
}
|
|
|
|
v := c.f(key)
|
|
|
|
if c.max > 0 && len(m) < c.max {
|
|
m[key] = v
|
|
}
|
|
return v
|
|
}
|