feat: add memory cache backend (#7048)

Signed-off-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
Teppei Fukuda
2024-06-28 13:42:02 +04:00
committed by GitHub
parent 14d71ba63c
commit 55ccd06df4
16 changed files with 577 additions and 42 deletions

8
pkg/cache/client.go vendored
View File

@@ -5,12 +5,15 @@ import (
"time"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/log"
)
const (
TypeUnknown Type = "unknown"
TypeFS Type = "fs"
TypeRedis Type = "redis"
TypeMemory Type = "memory"
)
type Type string
@@ -33,6 +36,8 @@ func NewType(backend string) Type {
return TypeRedis
case backend == "fs", backend == "":
return TypeFS
case backend == "memory":
return TypeMemory
default:
return TypeUnknown
}
@@ -44,6 +49,7 @@ func New(opts Options) (Cache, func(), error) {
var cache Cache
t := NewType(opts.Backend)
log.Debug("Initializing scan cache...", log.String("type", string(t)))
switch t {
case TypeRedis:
redisCache, err := NewRedisCache(opts.Backend, opts.RedisCACert, opts.RedisCert, opts.RedisKey, opts.RedisTLS, opts.TTL)
@@ -58,6 +64,8 @@ func New(opts Options) (Cache, func(), error) {
return nil, cleanup, xerrors.Errorf("unable to initialize fs cache: %w", err)
}
cache = fsCache
case TypeMemory:
cache = NewMemoryCache()
default:
return nil, cleanup, xerrors.Errorf("unknown cache type: %s", t)
}