-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from yannh/fs-cache
Cache schemas downloaded over HTTP
- Loading branch information
Showing
12 changed files
with
172 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package cache | ||
|
||
type Cache interface { | ||
Get(resourceKind, resourceAPIVersion, k8sVersion string) (interface{}, error) | ||
Set(resourceKind, resourceAPIVersion, k8sVersion string, schema interface{}) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
// SchemaCache is a cache for downloaded schemas, so each file is only retrieved once | ||
// It is different from pkg/registry/http_cache.go in that: | ||
// - This cache caches the parsed Schemas | ||
type inMemory struct { | ||
sync.RWMutex | ||
schemas map[string]interface{} | ||
} | ||
|
||
// New creates a new cache for downloaded schemas | ||
func NewInMemoryCache() Cache { | ||
return &inMemory{ | ||
schemas: map[string]interface{}{}, | ||
} | ||
} | ||
|
||
func key(resourceKind, resourceAPIVersion, k8sVersion string) string { | ||
return fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion) | ||
} | ||
|
||
// Get retrieves the JSON schema given a resource signature | ||
func (c *inMemory) Get(resourceKind, resourceAPIVersion, k8sVersion string) (interface{}, error) { | ||
k := key(resourceKind, resourceAPIVersion, k8sVersion) | ||
c.RLock() | ||
defer c.RUnlock() | ||
schema, ok := c.schemas[k] | ||
|
||
if ok == false { | ||
return nil, fmt.Errorf("schema not found in in-memory cache") | ||
} | ||
|
||
return schema, nil | ||
} | ||
|
||
// Set adds a JSON schema to the schema cache | ||
func (c *inMemory) Set(resourceKind, resourceAPIVersion, k8sVersion string, schema interface{}) error { | ||
k := key(resourceKind, resourceAPIVersion, k8sVersion) | ||
c.Lock() | ||
defer c.Unlock() | ||
c.schemas[k] = schema | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cache | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"sync" | ||
) | ||
|
||
type onDisk struct { | ||
sync.RWMutex | ||
folder string | ||
} | ||
|
||
// New creates a new cache for downloaded schemas | ||
func NewOnDiskCache(cache string) Cache { | ||
return &onDisk{ | ||
folder: cache, | ||
} | ||
} | ||
|
||
func cachePath(folder, resourceKind, resourceAPIVersion, k8sVersion string) string { | ||
hash := md5.Sum([]byte(fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion))) | ||
return path.Join(folder, hex.EncodeToString(hash[:])) | ||
} | ||
|
||
// Get retrieves the JSON schema given a resource signature | ||
func (c *onDisk) Get(resourceKind, resourceAPIVersion, k8sVersion string) (interface{}, error) { | ||
c.RLock() | ||
defer c.RUnlock() | ||
|
||
f, err := os.Open(cachePath(c.folder, resourceKind, resourceAPIVersion, k8sVersion)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ioutil.ReadAll(f) | ||
} | ||
|
||
// Set adds a JSON schema to the schema cache | ||
func (c *onDisk) Set(resourceKind, resourceAPIVersion, k8sVersion string, schema interface{}) error { | ||
c.Lock() | ||
defer c.Unlock() | ||
return ioutil.WriteFile(cachePath(c.folder, resourceKind, resourceAPIVersion, k8sVersion), schema.([]byte), 0644) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters