diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-08-12 16:43:37 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-08-13 11:44:20 +0200 |
commit | b64617fe4f90da030bcf4a9c5a4913393ce96b14 (patch) | |
tree | 07240dbf51bb4afef9ea063f2310c1617be6bb0a /resources/resource_cache.go | |
parent | 17ca8f0c4c636752fb9da2ad551679275dc03dd3 (diff) |
Add resources.Match and resources.GetMatch
Fix #6190
Diffstat (limited to 'resources/resource_cache.go')
-rw-r--r-- | resources/resource_cache.go | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/resources/resource_cache.go b/resources/resource_cache.go index 8ff63beb0..656d4f826 100644 --- a/resources/resource_cache.go +++ b/resources/resource_cache.go @@ -37,7 +37,9 @@ type ResourceCache struct { rs *Spec sync.RWMutex - cache map[string]resource.Resource + + // Either resource.Resource or resource.Resources. + cache map[string]interface{} fileCache *filecache.Cache @@ -61,7 +63,7 @@ func newResourceCache(rs *Spec) *ResourceCache { return &ResourceCache{ rs: rs, fileCache: rs.FileCaches.AssetsCache(), - cache: make(map[string]resource.Resource), + cache: make(map[string]interface{}), nlocker: locker.NewLocker(), } } @@ -70,7 +72,7 @@ func (c *ResourceCache) clear() { c.Lock() defer c.Unlock() - c.cache = make(map[string]resource.Resource) + c.cache = make(map[string]interface{}) c.nlocker = locker.NewLocker() } @@ -84,7 +86,7 @@ func (c *ResourceCache) cleanKey(key string) string { return strings.TrimPrefix(path.Clean(key), "/") } -func (c *ResourceCache) get(key string) (resource.Resource, bool) { +func (c *ResourceCache) get(key string) (interface{}, bool) { c.RLock() defer c.RUnlock() r, found := c.cache[key] @@ -92,6 +94,22 @@ func (c *ResourceCache) get(key string) (resource.Resource, bool) { } func (c *ResourceCache) GetOrCreate(partition, key string, f func() (resource.Resource, error)) (resource.Resource, error) { + r, err := c.getOrCreate(partition, key, func() (interface{}, error) { return f() }) + if r == nil || err != nil { + return nil, err + } + return r.(resource.Resource), nil +} + +func (c *ResourceCache) GetOrCreateResources(partition, key string, f func() (resource.Resources, error)) (resource.Resources, error) { + r, err := c.getOrCreate(partition, key, func() (interface{}, error) { return f() }) + if r == nil || err != nil { + return nil, err + } + return r.(resource.Resources), nil +} + +func (c *ResourceCache) getOrCreate(partition, key string, f func() (interface{}, error)) (interface{}, error) { key = c.cleanKey(path.Join(partition, key)) // First check in-memory cache. r, found := c.get(key) @@ -174,7 +192,7 @@ func (c *ResourceCache) writeMeta(key string, meta transformedResourceMetadata) } -func (c *ResourceCache) set(key string, r resource.Resource) { +func (c *ResourceCache) set(key string, r interface{}) { c.Lock() defer c.Unlock() c.cache[key] = r |