diff --git a/indexer/pchain/in_updater.go b/indexer/pchain/in_updater.go index 0fc00e7..16c7c6a 100644 --- a/indexer/pchain/in_updater.go +++ b/indexer/pchain/in_updater.go @@ -46,8 +46,8 @@ func (iu *pChainInputUpdater) updateFromDB( return nil, err } baseOuts := shared.NewOutputMap() - for _, out := range outs { - baseOuts.Add(shared.NewIdIndexKey(out.TxID, out.Index()), &out.TxOutput) + for i, out := range outs { + baseOuts.Add(shared.NewIdIndexKey(out.TxID, out.Index()), &outs[i].TxOutput) } return inputs.UpdateWithOutputs(baseOuts), nil } diff --git a/utils/cache.go b/utils/cache.go index 4f72ee1..26478ea 100644 --- a/utils/cache.go +++ b/utils/cache.go @@ -17,10 +17,10 @@ type Cache[K comparable, V any] interface { // Map object cache type cache[K comparable, V any] struct { - sync.RWMutex - cacheMap map[K]V accessed []K + + sync.RWMutex } func NewCache[K comparable, V any]() Cache[K, V] { @@ -31,24 +31,29 @@ func NewCache[K comparable, V any]() Cache[K, V] { } func (c *cache[K, V]) Add(k K, v V) { + c.Lock() + defer c.Unlock() + c.cacheMap[k] = v } func (c *cache[K, V]) Get(k K) (V, bool) { - c.RWMutex.Lock() + c.Lock() + defer c.Unlock() + v, ok := c.cacheMap[k] if ok { c.accessed = append(c.accessed, k) } - c.RWMutex.Unlock() return v, ok } func (c *cache[K, V]) RemoveAccessed() { - c.RWMutex.Lock() + c.Lock() + defer c.Unlock() + for _, k := range c.accessed { delete(c.cacheMap, k) } c.accessed = nil - c.RWMutex.Unlock() }