diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-11-12 16:17:29 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-11-12 16:17:29 +0300 |
commit | b60ee3425b4b6d54154de1629075b52b8599d869 (patch) | |
tree | a96eae38f4427a45bdbe00af0dbc39656871e001 /internal/source/gitlab/cache/cache.go | |
parent | 4e0be9393f33d27ea381cae3e6a6aeda88032153 (diff) |
Refactor gitlab source cache to make it more thread safe
Diffstat (limited to 'internal/source/gitlab/cache/cache.go')
-rw-r--r-- | internal/source/gitlab/cache/cache.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/source/gitlab/cache/cache.go b/internal/source/gitlab/cache/cache.go new file mode 100644 index 00000000..4ffc6022 --- /dev/null +++ b/internal/source/gitlab/cache/cache.go @@ -0,0 +1,38 @@ +package cache + +import ( + "context" +) + +// Cache is a short and long caching mechanism for GitLab source +type Cache struct { + client Resolver + store Store +} + +// NewCache creates a new instance of Cache. +func NewCache(client Resolver) *Cache { + return &Cache{ + client: client, + store: newMemStore(), + } +} + +// Resolve is going to return a Lookup based on a domain name +func (c *Cache) Resolve(ctx context.Context, domain string) (*Lookup, int, error) { + entry := c.store.LoadOrCreate(ctx, domain) + + if entry.IsUpToDate() { + return entry.Lookup() + } + + if entry.NeedsRefresh() { + entry.Refresh(ctx, c.client, c.store) + + return entry.Lookup() + } + + <-entry.Retrieve(c.client) + + return c.Resolve(ctx, domain) +} |