mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
Make azure cache general for all objects
This commit is contained in:
parent
2172a2a806
commit
259dbf8da7
@ -17,40 +17,62 @@ limitations under the License.
|
|||||||
package azure
|
package azure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type timedcacheEntry struct {
|
// getFunc defines a getter function for timedCache.
|
||||||
|
type getFunc func(key string) (interface{}, error)
|
||||||
|
|
||||||
|
// cacheEntry is the internal structure stores inside TTLStore.
|
||||||
|
type cacheEntry struct {
|
||||||
key string
|
key string
|
||||||
data interface{}
|
data interface{}
|
||||||
|
|
||||||
|
// The lock to ensure not updating same entry simultaneously.
|
||||||
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type timedcache struct {
|
// cacheKeyFunc defines the key function required in TTLStore.
|
||||||
store cache.Store
|
|
||||||
lock sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// ttl time.Duration
|
|
||||||
func newTimedcache(ttl time.Duration) timedcache {
|
|
||||||
return timedcache{
|
|
||||||
store: cache.NewTTLStore(cacheKeyFunc, ttl),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cacheKeyFunc(obj interface{}) (string, error) {
|
func cacheKeyFunc(obj interface{}) (string, error) {
|
||||||
return obj.(*timedcacheEntry).key, nil
|
if entry, ok := obj.(*cacheEntry); ok {
|
||||||
|
return entry.key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("obj %q is not an object of cacheEntry", obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *timedcache) GetOrCreate(key string, createFunc func() interface{}) (interface{}, error) {
|
// timedCache is a cache with TTL.
|
||||||
|
type timedCache struct {
|
||||||
|
store cache.Store
|
||||||
|
lock sync.Mutex
|
||||||
|
getter getFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// newTimedcache creates a new timedCache.
|
||||||
|
func newTimedcache(ttl time.Duration, getter getFunc) (*timedCache, error) {
|
||||||
|
if getter == nil {
|
||||||
|
return nil, fmt.Errorf("getter is not provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &timedCache{
|
||||||
|
getter: getter,
|
||||||
|
store: cache.NewTTLStore(cacheKeyFunc, ttl),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getInternal returns cacheEntry by key. If the key is not cached yet,
|
||||||
|
// it returns a cacheEntry with nil data.
|
||||||
|
func (t *timedCache) getInternal(key string) (*cacheEntry, error) {
|
||||||
entry, exists, err := t.store.GetByKey(key)
|
entry, exists, err := t.store.GetByKey(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
return (entry.(*timedcacheEntry)).data, nil
|
return entry.(*cacheEntry), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
@ -60,22 +82,53 @@ func (t *timedcache) GetOrCreate(key string, createFunc func() interface{}) (int
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
return (entry.(*timedcacheEntry)).data, nil
|
return entry.(*cacheEntry), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if createFunc == nil {
|
// Still not found, add new entry with nil data.
|
||||||
return nil, nil
|
// Note the data will be filled later by getter.
|
||||||
}
|
newEntry := &cacheEntry{
|
||||||
created := createFunc()
|
|
||||||
t.store.Add(&timedcacheEntry{
|
|
||||||
key: key,
|
key: key,
|
||||||
data: created,
|
data: nil,
|
||||||
})
|
}
|
||||||
return created, nil
|
t.store.Add(newEntry)
|
||||||
|
return newEntry, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *timedcache) Delete(key string) {
|
// Get returns the requested item by key.
|
||||||
_ = t.store.Delete(&timedcacheEntry{
|
func (t *timedCache) Get(key string) (interface{}, error) {
|
||||||
|
entry, err := t.getInternal(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data is still not cached yet, cache it by getter.
|
||||||
|
if entry.data == nil {
|
||||||
|
entry.lock.Lock()
|
||||||
|
defer entry.lock.Unlock()
|
||||||
|
|
||||||
|
data, err := t.getter(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
entry.data = data
|
||||||
|
}
|
||||||
|
|
||||||
|
return entry.data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update sets an item in the cache to its updated state.
|
||||||
|
func (t *timedCache) Update(key string, data interface{}) error {
|
||||||
|
return t.store.Update(&cacheEntry{
|
||||||
|
key: key,
|
||||||
|
data: data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete removes an item from the cache.
|
||||||
|
func (t *timedCache) Delete(key string) error {
|
||||||
|
return t.store.Delete(&cacheEntry{
|
||||||
key: key,
|
key: key,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user