mirror of
https://github.com/vee1e/gittuf.git
synced 2026-09-02 02:37:05 +00:00
69 lines
1.3 KiB
Go
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()
|
|
}
|