mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-16 15:20:38 +00:00
fix: use a cache file name with a fixed size. (#350)
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
This commit is contained in:
@@ -73,21 +73,15 @@ func (c *OpenAIClient) GetCompletion(ctx context.Context, prompt string) (string
|
|||||||
func (a *OpenAIClient) Parse(ctx context.Context, prompt []string, cache cache.ICache) (string, error) {
|
func (a *OpenAIClient) Parse(ctx context.Context, prompt []string, cache cache.ICache) (string, error) {
|
||||||
inputKey := strings.Join(prompt, " ")
|
inputKey := strings.Join(prompt, " ")
|
||||||
// Check for cached data
|
// Check for cached data
|
||||||
sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey))
|
cacheKey := util.GetCacheKey(a.GetName(), a.language, 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)
|
|
||||||
|
|
||||||
|
if !cache.IsCacheDisabled() && cache.Exists(cacheKey) {
|
||||||
|
response, err := cache.Load(cacheKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if response == "" {
|
if response != "" {
|
||||||
color.Red("error retrieving cached data")
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
output, err := base64.StdEncoding.DecodeString(response)
|
output, err := base64.StdEncoding.DecodeString(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Red("error decoding cached data: %v", err)
|
color.Red("error decoding cached data: %v", err)
|
||||||
@@ -95,6 +89,7 @@ func (a *OpenAIClient) Parse(ctx context.Context, prompt []string, cache cache.I
|
|||||||
}
|
}
|
||||||
return string(output), nil
|
return string(output), nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
response, err := a.GetCompletion(ctx, inputKey)
|
response, err := a.GetCompletion(ctx, inputKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
7
pkg/cache/cache.go
vendored
7
pkg/cache/cache.go
vendored
@@ -4,12 +4,11 @@ type ICache interface {
|
|||||||
Store(key string, data string) error
|
Store(key string, data string) error
|
||||||
Load(key string) (string, error)
|
Load(key string) (string, error)
|
||||||
Exists(key string) bool
|
Exists(key string) bool
|
||||||
|
IsCacheDisabled() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(noCache bool) ICache {
|
func New(noCache bool) ICache {
|
||||||
if noCache {
|
return &FileBasedCache{
|
||||||
return &NoopCache{}
|
noCache: noCache,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &FileBasedCache{}
|
|
||||||
}
|
}
|
||||||
|
8
pkg/cache/file_based.go
vendored
8
pkg/cache/file_based.go
vendored
@@ -11,7 +11,13 @@ import (
|
|||||||
|
|
||||||
var _ (ICache) = (*FileBasedCache)(nil)
|
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 {
|
func (*FileBasedCache) Exists(key string) bool {
|
||||||
path, err := xdg.CacheFile(filepath.Join("k8sgpt", key))
|
path, err := xdg.CacheFile(filepath.Join("k8sgpt", key))
|
||||||
|
17
pkg/cache/noop.go
vendored
17
pkg/cache/noop.go
vendored
@@ -1,17 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
var _ (ICache) = (*NoopCache)(nil)
|
|
||||||
|
|
||||||
type NoopCache struct{}
|
|
||||||
|
|
||||||
func (c *NoopCache) Store(key string, data string) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *NoopCache) Load(key string) (string, error) {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *NoopCache) Exists(key string) bool {
|
|
||||||
return false
|
|
||||||
}
|
|
@@ -15,7 +15,9 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@@ -148,7 +150,11 @@ func ReplaceIfMatch(text string, pattern string, replacement string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetCacheKey(provider string, language string, sEnc 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,
|
func GetPodListByLabels(client k.Interface,
|
||||||
|
Reference in New Issue
Block a user