mirror of
https://github.com/harness/drone.git
synced 2025-05-31 11:43:15 +00:00
introduce last commit cache mode env var
This commit is contained in:
parent
bfc0db8e8e
commit
dc7b21fc0d
27
cache/no_cache.go
vendored
Normal file
27
cache/no_cache.go
vendored
Normal file
@ -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)
|
||||
}
|
@ -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"
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user