add schema caching

This commit is contained in:
Yann Hamon 2020-05-30 06:10:58 +02:00
parent 79c9197f38
commit d3ed871833
2 changed files with 40 additions and 1 deletions

36
pkg/cache/main.go vendored Normal file
View file

@ -0,0 +1,36 @@
package cache
import (
"fmt"
"sync"
)
var mu sync.Mutex
var schemas map[string][]byte
func init () {
schemas = map[string][]byte {}
}
func WithCache(downloadSchema func(string, string, string) ([]byte, error)) (func (string, string, string) ([]byte, error)) {
return func(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
cacheKey := fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
mu.Lock()
cachedSchema, ok := schemas[cacheKey];
mu.Unlock()
if ok {
return cachedSchema, nil
}
schema, err := downloadSchema(resourceKind, resourceAPIVersion, k8sVersion)
if err != nil {
return schema, err
}
mu.Lock()
schemas[cacheKey] = schema
mu.Unlock()
return schema, nil
}
}