feat: rework cache package - add gcs cache - add cache purge command (#750)

* feat: rework cache pkg

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: Completion of cache pkg rework. Added cache purge command.

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* doc: add purgin command note

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* fix: disable cache if noCache is set

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: improve GetCacheConfiguration lisibility & transform add method to addOrUpdate

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: transform server mode to work with new cache configuration

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* fix: use 'switch' instead 'if' to evaluate Cache from grpc

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: add mutually exclusive flags for command options

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* doc: update readme.md

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: return err on bucket creation failed

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: update dependencies

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
Signed-off-by: Matthis <matthish29@gmail.com>
This commit is contained in:
Matthis
2023-11-18 22:08:38 +01:00
committed by GitHub
parent beaa53251c
commit 12146bf356
14 changed files with 563 additions and 186 deletions

105
pkg/cache/s3_based.go vendored
View File

@@ -7,7 +7,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/spf13/viper"
)
// Generate ICache implementation
@@ -17,6 +16,45 @@ type S3Cache struct {
session *s3.S3
}
type S3CacheConfiguration struct {
Region string `mapstructure:"region" yaml:"region,omitempty"`
BucketName string `mapstructure:"bucketname" yaml:"bucketname,omitempty"`
}
func (s *S3Cache) Configure(cacheInfo CacheProvider) error {
if cacheInfo.S3.BucketName == "" {
log.Fatal("Bucket name not configured")
}
if cacheInfo.S3.Region == "" {
log.Fatal("Region not configured")
}
s.bucketName = cacheInfo.S3.BucketName
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{
Region: aws.String(cacheInfo.S3.Region),
},
}))
s3Client := s3.New(sess)
// Check if the bucket exists, if not create it
_, err := s3Client.HeadBucket(&s3.HeadBucketInput{
Bucket: aws.String(cacheInfo.S3.BucketName),
})
if err != nil {
_, err = s3Client.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(cacheInfo.S3.BucketName),
})
if err != nil {
return err
}
}
s.session = s3Client
return nil
}
func (s *S3Cache) Store(key string, data string) error {
// Store the object as a new file in the bucket with data as the content
_, err := s.session.PutObject(&s3.PutObjectInput{
@@ -28,6 +66,18 @@ func (s *S3Cache) Store(key string, data string) error {
}
func (s *S3Cache) Remove(key string) error {
_, err := s.session.DeleteObject(&s3.DeleteObjectInput{
Bucket: &s.bucketName,
Key: aws.String(key),
})
if err != nil {
return err
}
return nil
}
func (s *S3Cache) Load(key string) (string, error) {
// Retrieve the object from the bucket and load it into a string
@@ -45,7 +95,7 @@ func (s *S3Cache) Load(key string) (string, error) {
return buf.String(), nil
}
func (s *S3Cache) List() ([]string, error) {
func (s *S3Cache) List() ([]CacheObjectDetails, error) {
// List the files in the bucket
result, err := s.session.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(s.bucketName)})
@@ -53,9 +103,12 @@ func (s *S3Cache) List() ([]string, error) {
return nil, err
}
var keys []string
var keys []CacheObjectDetails
for _, item := range result.Contents {
keys = append(keys, *item.Key)
keys = append(keys, CacheObjectDetails{
Name: *item.Key,
UpdatedAt: *item.LastModified,
})
}
return keys, nil
@@ -75,42 +128,10 @@ func (s *S3Cache) IsCacheDisabled() bool {
return s.noCache
}
func NewS3Cache(nocache bool) ICache {
var cache CacheProvider
err := viper.UnmarshalKey("cache", &cache)
if err != nil {
log.Fatal(err)
}
if cache.BucketName == "" {
log.Fatal("Bucket name not configured")
}
if cache.Region == "" {
log.Fatal("Region not configured")
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{
Region: aws.String(cache.Region),
},
}))
s := s3.New(sess)
// Check if the bucket exists, if not create it
_, err = s.HeadBucket(&s3.HeadBucketInput{
Bucket: aws.String(cache.BucketName),
})
if err != nil {
_, _ = s.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(cache.BucketName),
})
}
return &S3Cache{
noCache: nocache,
session: s,
bucketName: cache.BucketName,
}
func (s *S3Cache) GetName() string {
return "s3"
}
func (s *S3Cache) DisableCache() {
s.noCache = true
}