Skip to content

Commit

Permalink
fix: use a cache file name with a fixed size. (#350)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed Apr 27, 2023
1 parent 3af3667 commit dee4235
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 53 deletions.
25 changes: 10 additions & 15 deletions pkg/ai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,22 @@ func (c *OpenAIClient) GetCompletion(ctx context.Context, prompt string) (string
func (a *OpenAIClient) Parse(ctx context.Context, prompt []string, cache cache.ICache) (string, error) {
inputKey := strings.Join(prompt, " ")
// Check for cached data
sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey))
cacheKey := util.GetCacheKey(a.GetName(), a.language, sEnc)
// find in viper cache
if cache.Exists(cacheKey) {
// retrieve data from cache
response, err := cache.Load(cacheKey)
cacheKey := util.GetCacheKey(a.GetName(), a.language, inputKey)

if !cache.IsCacheDisabled() && cache.Exists(cacheKey) {
response, err := cache.Load(cacheKey)
if err != nil {
return "", err
}

if response == "" {
color.Red("error retrieving cached data")
return "", nil
}
output, err := base64.StdEncoding.DecodeString(response)
if err != nil {
color.Red("error decoding cached data: %v", err)
return "", nil
if response != "" {
output, err := base64.StdEncoding.DecodeString(response)
if err != nil {
color.Red("error decoding cached data: %v", err)
return "", nil
}
return string(output), nil
}
return string(output), nil
}

response, err := a.GetCompletion(ctx, inputKey)
Expand Down
30 changes: 15 additions & 15 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import (
)

type Analysis struct {
Context context.Context
Filters []string
Client *kubernetes.Client
AIClient ai.IAI
Results []common.Result
Namespace string
Cache cache.ICache
Explain bool
Context context.Context
Filters []string
Client *kubernetes.Client
AIClient ai.IAI
Results []common.Result
Namespace string
Cache cache.ICache
Explain bool
MaxConcurrency int
}

Expand Down Expand Up @@ -102,13 +102,13 @@ func NewAnalysis(backend string, language string, filters []string, namespace st
}

return &Analysis{
Context: ctx,
Filters: filters,
Client: client,
AIClient: aiClient,
Namespace: namespace,
Cache: cache.New(noCache),
Explain: explain,
Context: ctx,
Filters: filters,
Client: client,
AIClient: aiClient,
Namespace: namespace,
Cache: cache.New(noCache),
Explain: explain,
MaxConcurrency: maxConcurrency,
}, nil
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ type ICache interface {
Store(key string, data string) error
Load(key string) (string, error)
Exists(key string) bool
IsCacheDisabled() bool
}

func New(noCache bool) ICache {
if noCache {
return &NoopCache{}
return &FileBasedCache{
noCache: noCache,
}

return &FileBasedCache{}
}
8 changes: 7 additions & 1 deletion pkg/cache/file_based.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import (

var _ (ICache) = (*FileBasedCache)(nil)

type FileBasedCache struct{}
type FileBasedCache struct {
noCache bool
}

func (f *FileBasedCache) IsCacheDisabled() bool {
return f.noCache
}

func (*FileBasedCache) Exists(key string) bool {
path, err := xdg.CacheFile(filepath.Join("k8sgpt", key))
Expand Down
17 changes: 0 additions & 17 deletions pkg/cache/noop.go

This file was deleted.

8 changes: 7 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ package util

import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"math/rand"
Expand Down Expand Up @@ -148,7 +150,11 @@ func ReplaceIfMatch(text string, pattern string, replacement string) string {
}

func GetCacheKey(provider string, language string, sEnc string) string {
return fmt.Sprintf("%s-%s-%s", provider, language, sEnc)
data := fmt.Sprintf("%s-%s-%s", provider, language, sEnc)

hash := sha256.Sum256([]byte(data))

return hex.EncodeToString(hash[:])
}

func GetPodListByLabels(client k.Interface,
Expand Down

0 comments on commit dee4235

Please sign in to comment.