gittuf/pkg/rsl/cache.go
Paulo Gomes 4a53c1b214
refactor: standardize on githash.Hash
Signed-off-by: Paulo Gomes <paulo@entire.io>
2026-08-03 21:27:52 +01:00

69 lines
1.3 KiB
Go

// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package rsl
import (
"sync"
"github.com/gittuf/gittuf/pkg/githash"
)
type rslCache struct {
entryCache map[string]Entry
parentCache map[string]string
entryCacheMutex sync.RWMutex
parentCacheMutex sync.RWMutex
}
func (r *rslCache) getEntry(id githash.Hash) (Entry, bool) {
r.entryCacheMutex.RLock()
defer r.entryCacheMutex.RUnlock()
entry, has := r.entryCache[id.String()]
return entry, has
}
func (r *rslCache) setEntry(id githash.Hash, entry Entry) {
r.entryCacheMutex.Lock()
defer r.entryCacheMutex.Unlock()
r.entryCache[id.String()] = entry
}
func (r *rslCache) getParent(id githash.Hash) (githash.Hash, bool, error) {
r.parentCacheMutex.RLock()
defer r.parentCacheMutex.RUnlock()
parentID, has := r.parentCache[id.String()]
if !has {
return nil, false, nil
}
parentIDHash, err := NewHash(parentID)
if err != nil {
return nil, false, err
}
return parentIDHash, true, nil
}
func (r *rslCache) setParent(id, parentID githash.Hash) {
r.parentCacheMutex.Lock()
defer r.parentCacheMutex.Unlock()
r.parentCache[id.String()] = parentID.String()
}
var cache *rslCache
func newRSLCache() {
cache = &rslCache{
entryCache: map[string]Entry{},
parentCache: map[string]string{},
}
}
func init() {
newRSLCache()
}