diff options
author | Michael Muré <batolettre@gmail.com> | 2021-04-08 16:25:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-08 16:25:22 +0200 |
commit | bc5f618eba812859bf87ce2c31b278bd518d4555 (patch) | |
tree | 6429e6c47de93660b16debfa244427e79e7aed85 /identity/resolver.go | |
parent | a3e098d76314fbb197a6fbd85d2e1df02c9e00fe (diff) | |
parent | 554992523574684ecce36d38bf5310bff52c8c03 (diff) | |
download | git-bug-bc5f618eba812859bf87ce2c31b278bd518d4555.tar.gz |
Merge pull request #628 from MichaelMure/entity-fix
cache: many fixes following the dag entity migration
Diffstat (limited to 'identity/resolver.go')
-rw-r--r-- | identity/resolver.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/identity/resolver.go b/identity/resolver.go index ab380a12..8e066e9d 100644 --- a/identity/resolver.go +++ b/identity/resolver.go @@ -1,6 +1,8 @@ package identity import ( + "sync" + "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/repository" ) @@ -34,3 +36,36 @@ func NewStubResolver() *StubResolver { func (s *StubResolver) ResolveIdentity(id entity.Id) (Interface, error) { return &IdentityStub{id: id}, nil } + +// CachedResolver is a resolver ensuring that loading is done only once through another Resolver. +type CachedResolver struct { + mu sync.RWMutex + resolver Resolver + identities map[entity.Id]Interface +} + +func NewCachedResolver(resolver Resolver) *CachedResolver { + return &CachedResolver{ + resolver: resolver, + identities: make(map[entity.Id]Interface), + } +} + +func (c *CachedResolver) ResolveIdentity(id entity.Id) (Interface, error) { + c.mu.RLock() + if i, ok := c.identities[id]; ok { + c.mu.RUnlock() + return i, nil + } + c.mu.RUnlock() + + c.mu.Lock() + defer c.mu.Unlock() + + i, err := c.resolver.ResolveIdentity(id) + if err != nil { + return nil, err + } + c.identities[id] = i + return i, nil +} |