diff --git a/cache/no_cache.go b/cache/no_cache.go new file mode 100644 index 000000000..4d2002e8a --- /dev/null +++ b/cache/no_cache.go @@ -0,0 +1,27 @@ +// Copyright 2022 Harness Inc. All rights reserved. +// Use of this source code is governed by the Polyform Free Trial License +// that can be found in the LICENSE.md file for this repository. + +package cache + +import ( + "context" +) + +type NoCache[K any, V any] struct { + getter Getter[K, V] +} + +func NewNoCache[K any, V any](getter Getter[K, V]) NoCache[K, V] { + return NoCache[K, V]{ + getter: getter, + } +} + +func (c NoCache[K, V]) Stats() (int64, int64) { + return 0, 0 +} + +func (c NoCache[K, V]) Get(ctx context.Context, key K) (V, error) { + return c.getter.Find(ctx, key) +} diff --git a/gitrpc/internal/gitea/last_commit_cache.go b/gitrpc/internal/gitea/last_commit_cache.go index 23ffea3ba..0cdb484d9 100644 --- a/gitrpc/internal/gitea/last_commit_cache.go +++ b/gitrpc/internal/gitea/last_commit_cache.go @@ -51,6 +51,12 @@ func NewRedisLastCommitCache( cacheDuration) } +func NoLastCommitCache( + repoCache cache.Cache[string, *RepoEntryValue], +) cache.Cache[CommitEntryKey, *types.Commit] { + return cache.NewNoCache[CommitEntryKey, *types.Commit](commitEntryGetter{repoCache: repoCache}) +} + type CommitEntryKey string const commitEntryKeySeparator = "\x00" diff --git a/gitrpc/server/config.go b/gitrpc/server/config.go index 05273073d..0df77b296 100644 --- a/gitrpc/server/config.go +++ b/gitrpc/server/config.go @@ -9,6 +9,12 @@ import ( "time" ) +const ( + ModeInMemory = "inmemory" + ModeRedis = "redis" + ModeNone = "none" +) + // Config represents the configuration for the gitrpc server. type Config struct { // Bind specifies the addr used to bind the grpc server. @@ -26,8 +32,14 @@ type Config struct { MaxConnAge time.Duration `envconfig:"GITRPC_SERVER_MAX_CONN_AGE" default:"630720000s"` MaxConnAgeGrace time.Duration `envconfig:"GITRPC_SERVER_MAX_CONN_AGE_GRACE" default:"630720000s"` - // LastCommitCacheSeconds defines cache duration in seconds of last commit, default=12h - LastCommitCacheSeconds int `envconfig:"GITRPC_LAST_COMMIT_CACHE_SECONDS" default:"43200"` + // LastCommitCache holds configuration options for the last commit cache. + LastCommitCache struct { + // Mode determines where the cache will be. Valid values are "inmemory" (default), "redis" or "none". + Mode string `envconfig:"GITRPC_LAST_COMMIT_CACHE_MODE" default:"inmemory"` + + // DurationSeconds defines cache duration in seconds of last commit, default=12h. + DurationSeconds int `envconfig:"GITRPC_LAST_COMMIT_CACHE_SECONDS" default:"43200"` + } Redis struct { Endpoint string `envconfig:"GITRPC_REDIS_ENDPOINT" default:"localhost:6379"` @@ -59,6 +71,9 @@ func (c *Config) Validate() error { if c.MaxConnAgeGrace == 0 { return errors.New("config.MaxConnAgeGrace is required") } + if m := c.LastCommitCache.Mode; m != "" && m != ModeInMemory && m != ModeRedis && m != ModeNone { + return errors.New("config.LastCommitCache.Mode has unsupported value") + } return nil } diff --git a/gitrpc/server/wire.go b/gitrpc/server/wire.go index e50667606..2b315aefc 100644 --- a/gitrpc/server/wire.go +++ b/gitrpc/server/wire.go @@ -34,13 +34,17 @@ func ProvideLastCommitCache( redisClient redis.UniversalClient, repoCache cache.Cache[string, *gitea.RepoEntryValue], ) cache.Cache[gitea.CommitEntryKey, *types.Commit] { - cacheDuration := time.Duration(config.LastCommitCacheSeconds) * time.Second + cacheDuration := time.Duration(config.LastCommitCache.DurationSeconds) * time.Second - if redisClient == nil { - return gitea.NewInMemoryLastCommitCache(cacheDuration, repoCache) + if config.LastCommitCache.Mode == ModeNone || cacheDuration < time.Second { + return gitea.NoLastCommitCache(repoCache) } - return gitea.NewRedisLastCommitCache(redisClient, cacheDuration, repoCache) + if config.LastCommitCache.Mode == ModeRedis && redisClient != nil { + return gitea.NewRedisLastCommitCache(redisClient, cacheDuration, repoCache) + } + + return gitea.NewInMemoryLastCommitCache(cacheDuration, repoCache) } func ProvideGITAdapter(