aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/scene_cache.go (587B)


      1 package main
      2 
      3 import "sync"
      4 
      5 // Immutable assets are decoded once per identity. Decoding happens outside the
      6 // map's synchronization, so unrelated assets can load independently.
      7 type sceneCache[T any] struct {
      8 	values sync.Map
      9 }
     10 
     11 func (cache *sceneCache[T]) get(identity uint32) (T, bool) {
     12 	if value, ok := cache.values.Load(identity); ok {
     13 		return value.(func() T)(), true
     14 	}
     15 	var zero T
     16 	return zero, false
     17 }
     18 
     19 func (cache *sceneCache[T]) load(identity uint32, decode func() T) T {
     20 	value, _ := cache.values.LoadOrStore(identity, sync.OnceValue(decode))
     21 	return value.(func() T)()
     22 }