This commit is contained in:
Yann Hamon 2025-05-10 23:56:10 +02:00
parent ecc042d7f9
commit c954a22d7d
6 changed files with 1392 additions and 300 deletions

4
pkg/cache/cache.go vendored
View file

@ -1,6 +1,6 @@
package cache
type Cache interface {
Get(key string) (interface{}, error)
Set(key string, schema interface{}) error
Get(key string) ([]byte, error)
Set(key string, schema []byte) error
}

View file

@ -10,18 +10,18 @@ import (
// - This cache caches the parsed Schemas
type inMemory struct {
sync.RWMutex
schemas map[string]interface{}
schemas map[string][]byte
}
// New creates a new cache for downloaded schemas
func NewInMemoryCache() Cache {
return &inMemory{
schemas: map[string]interface{}{},
schemas: make(map[string][]byte),
}
}
// Get retrieves the JSON schema given a resource signature
func (c *inMemory) Get(key string) (interface{}, error) {
func (c *inMemory) Get(key string) ([]byte, error) {
c.RLock()
defer c.RUnlock()
schema, ok := c.schemas[key]
@ -34,7 +34,7 @@ func (c *inMemory) Get(key string) (interface{}, error) {
}
// Set adds a JSON schema to the schema cache
func (c *inMemory) Set(key string, schema interface{}) error {
func (c *inMemory) Set(key string, schema []byte) error {
c.Lock()
defer c.Unlock()
c.schemas[key] = schema

10
pkg/cache/ondisk.go vendored
View file

@ -27,7 +27,7 @@ func cachePath(folder, key string) string {
}
// Get retrieves the JSON schema given a resource signature
func (c *onDisk) Get(key string) (interface{}, error) {
func (c *onDisk) Get(key string) ([]byte, error) {
c.RLock()
defer c.RUnlock()
@ -41,8 +41,12 @@ func (c *onDisk) Get(key string) (interface{}, error) {
}
// Set adds a JSON schema to the schema cache
func (c *onDisk) Set(key string, schema interface{}) error {
func (c *onDisk) Set(key string, schema []byte) error {
c.Lock()
defer c.Unlock()
return os.WriteFile(cachePath(c.folder, key), schema.([]byte), 0644)
if _, err := os.Stat(cachePath(c.folder, key)); os.IsNotExist(err) {
return os.WriteFile(cachePath(c.folder, key), schema, 0644)
}
return nil
}