mirror of
https://github.com/harness/drone.git
synced 2025-05-05 14:00:12 +08:00
61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
// Copyright 2023 Harness, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package adapter
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/harness/gitness/cache"
|
|
"github.com/harness/gitness/git/types"
|
|
apptypes "github.com/harness/gitness/types"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/google/wire"
|
|
)
|
|
|
|
const (
|
|
ModeInMemory = "inmemory"
|
|
ModeRedis = "redis"
|
|
ModeNone = "none"
|
|
)
|
|
|
|
var WireSet = wire.NewSet(
|
|
ProvideGoGitRepoProvider,
|
|
ProvideLastCommitCache,
|
|
)
|
|
|
|
func ProvideGoGitRepoProvider() *GoGitRepoProvider {
|
|
const objectCacheSize = 16 << 20 // 16MiB
|
|
return NewGoGitRepoProvider(objectCacheSize, 15*time.Minute)
|
|
}
|
|
|
|
func ProvideLastCommitCache(
|
|
config *apptypes.Config,
|
|
redisClient redis.UniversalClient,
|
|
repoProvider *GoGitRepoProvider,
|
|
) cache.Cache[CommitEntryKey, *types.Commit] {
|
|
cacheDuration := time.Duration(config.Git.LastCommitCache.DurationSeconds) * time.Second
|
|
|
|
if config.Git.LastCommitCache.Mode == ModeNone || cacheDuration < time.Second {
|
|
return NoLastCommitCache(repoProvider)
|
|
}
|
|
|
|
if config.Git.LastCommitCache.Mode == ModeRedis && redisClient != nil {
|
|
return NewRedisLastCommitCache(redisClient, cacheDuration, repoProvider)
|
|
}
|
|
|
|
return NewInMemoryLastCommitCache(cacheDuration, repoProvider)
|
|
}
|