diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-03-03 17:50:04 +0300 |
---|---|---|
committer | Alessio Caiazza <acaiazza@gitlab.com> | 2020-03-03 17:50:04 +0300 |
commit | 536bdde15bc09aab0bd1a15a5ac9f4b5133716bd (patch) | |
tree | 3c6691e7f821b6dc11f95c094d742cfafb92fd4f /internal/source/gitlab/cache/cache.go | |
parent | 3ae8b1c077bef83c480cba2695e3943ba152e00c (diff) |
Add default cache configuration and pass down to memstore and Entry.
Wrap global variables into one default struct.
Update test steps so that tests run every time so we don't get
the cached results.
Diffstat (limited to 'internal/source/gitlab/cache/cache.go')
-rw-r--r-- | internal/source/gitlab/cache/cache.go | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/internal/source/gitlab/cache/cache.go b/internal/source/gitlab/cache/cache.go index 9a4ab753..bb3567b4 100644 --- a/internal/source/gitlab/cache/cache.go +++ b/internal/source/gitlab/cache/cache.go @@ -2,22 +2,44 @@ package cache import ( "context" + "time" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" "gitlab.com/gitlab-org/gitlab-pages/metrics" ) +var defaultCacheConfig = cacheConfig{ + cacheExpiry: 10 * time.Minute, + entryRefreshTimeout: 30 * time.Second, + retrievalTimeout: 5 * time.Second, + maxRetrievalInterval: time.Second, + maxRetrievalRetries: 3, +} + // Cache is a short and long caching mechanism for GitLab source type Cache struct { - client api.Client - store Store + client api.Client + store Store + cacheConfig *cacheConfig +} + +type cacheConfig struct { + cacheExpiry time.Duration + entryRefreshTimeout time.Duration + retrievalTimeout time.Duration + maxRetrievalInterval time.Duration + maxRetrievalRetries int } // NewCache creates a new instance of Cache. -func NewCache(client api.Client) *Cache { +func NewCache(client api.Client, cc *cacheConfig) *Cache { + if cc == nil { + cc = &defaultCacheConfig + } + return &Cache{ client: client, - store: newMemStore(), + store: newMemStore(client, cc), } } @@ -25,8 +47,8 @@ func NewCache(client api.Client) *Cache { // algorithm works as follows: // - We first check if the cache entry exists, and if it is up-to-date. If it // is fresh we return the lookup entry from cache and it is a cache hit. -// - If entry is not up-to-date, what means that it has been created in a cache -// more than `shortCacheExpiry` duration ago, we schedule an asynchronous +// - If entry is not up-to-date, that means it has been created in a cache +// more than `entryRefreshTimeout` duration ago, we schedule an asynchronous // retrieval of the latest configuration we are going to obtain through the // API, and we immediately return an old value, to avoid blocking clients. In // this case it is also a cache hit. |