2019-09-27 21:51:53 +00:00
|
|
|
// +build !providerless
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
package cache
|
2019-09-27 21:51:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
)
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
// AzureCacheReadType defines the read type for cache data
|
|
|
|
type AzureCacheReadType int
|
2019-11-14 18:56:24 +00:00
|
|
|
|
|
|
|
const (
|
2020-03-26 21:07:15 +00:00
|
|
|
// CacheReadTypeDefault returns data from cache if cache entry not expired
|
2019-11-14 18:56:24 +00:00
|
|
|
// if cache entry expired, then it will refetch the data using getter
|
|
|
|
// save the entry in cache and then return
|
2020-03-26 21:07:15 +00:00
|
|
|
CacheReadTypeDefault AzureCacheReadType = iota
|
|
|
|
// CacheReadTypeUnsafe returns data from cache even if the cache entry is
|
2019-11-14 18:56:24 +00:00
|
|
|
// active/expired. If entry doesn't exist in cache, then data is fetched
|
|
|
|
// using getter, saved in cache and returned
|
2020-03-26 21:07:15 +00:00
|
|
|
CacheReadTypeUnsafe
|
|
|
|
// CacheReadTypeForceRefresh force refreshes the cache even if the cache entry
|
2020-02-14 00:18:16 +00:00
|
|
|
// is not expired
|
2020-03-26 21:07:15 +00:00
|
|
|
CacheReadTypeForceRefresh
|
2019-11-14 18:56:24 +00:00
|
|
|
)
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
// GetFunc defines a getter function for timedCache.
|
|
|
|
type GetFunc func(key string) (interface{}, error)
|
2019-09-27 21:51:53 +00:00
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
// AzureCacheEntry is the internal structure stores inside TTLStore.
|
|
|
|
type AzureCacheEntry struct {
|
|
|
|
Key string
|
|
|
|
Data interface{}
|
2019-09-27 21:51:53 +00:00
|
|
|
|
|
|
|
// The lock to ensure not updating same entry simultaneously.
|
2020-03-26 21:07:15 +00:00
|
|
|
Lock sync.Mutex
|
2019-11-14 18:56:24 +00:00
|
|
|
// time when entry was fetched and created
|
2020-03-26 21:07:15 +00:00
|
|
|
CreatedOn time.Time
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cacheKeyFunc defines the key function required in TTLStore.
|
|
|
|
func cacheKeyFunc(obj interface{}) (string, error) {
|
2020-03-26 21:07:15 +00:00
|
|
|
return obj.(*AzureCacheEntry).Key, nil
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
// TimedCache is a cache with TTL.
|
|
|
|
type TimedCache struct {
|
|
|
|
Store cache.Store
|
|
|
|
Lock sync.Mutex
|
|
|
|
Getter GetFunc
|
|
|
|
TTL time.Duration
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
// NewTimedcache creates a new TimedCache.
|
|
|
|
func NewTimedcache(ttl time.Duration, getter GetFunc) (*TimedCache, error) {
|
2019-09-27 21:51:53 +00:00
|
|
|
if getter == nil {
|
|
|
|
return nil, fmt.Errorf("getter is not provided")
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
return &TimedCache{
|
|
|
|
Getter: getter,
|
2019-11-14 18:56:24 +00:00
|
|
|
// switch to using NewStore instead of NewTTLStore so that we can
|
|
|
|
// reuse entries for calls that are fine with reading expired/stalled data.
|
|
|
|
// with NewTTLStore, entries are not returned if they have already expired.
|
2020-03-26 21:07:15 +00:00
|
|
|
Store: cache.NewStore(cacheKeyFunc),
|
|
|
|
TTL: ttl,
|
2019-09-27 21:51:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
// getInternal returns AzureCacheEntry by key. If the key is not cached yet,
|
|
|
|
// it returns a AzureCacheEntry with nil data.
|
|
|
|
func (t *TimedCache) getInternal(key string) (*AzureCacheEntry, error) {
|
|
|
|
entry, exists, err := t.Store.GetByKey(key)
|
2019-09-27 21:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-14 18:56:24 +00:00
|
|
|
// if entry exists, return the entry
|
2019-09-27 21:51:53 +00:00
|
|
|
if exists {
|
2020-03-26 21:07:15 +00:00
|
|
|
return entry.(*AzureCacheEntry), nil
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 18:56:24 +00:00
|
|
|
// lock here to ensure if entry doesn't exist, we add a new entry
|
|
|
|
// avoiding overwrites
|
2020-03-26 21:07:15 +00:00
|
|
|
t.Lock.Lock()
|
|
|
|
defer t.Lock.Unlock()
|
2019-09-27 21:51:53 +00:00
|
|
|
|
|
|
|
// Still not found, add new entry with nil data.
|
|
|
|
// Note the data will be filled later by getter.
|
2020-03-26 21:07:15 +00:00
|
|
|
newEntry := &AzureCacheEntry{
|
|
|
|
Key: key,
|
|
|
|
Data: nil,
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
t.Store.Add(newEntry)
|
2019-09-27 21:51:53 +00:00
|
|
|
return newEntry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the requested item by key.
|
2020-03-26 21:07:15 +00:00
|
|
|
func (t *TimedCache) Get(key string, crt AzureCacheReadType) (interface{}, error) {
|
2019-09-27 21:51:53 +00:00
|
|
|
entry, err := t.getInternal(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
entry.Lock.Lock()
|
|
|
|
defer entry.Lock.Unlock()
|
2019-09-27 21:51:53 +00:00
|
|
|
|
2020-02-14 00:18:16 +00:00
|
|
|
// entry exists and if cache is not force refreshed
|
2020-03-26 21:07:15 +00:00
|
|
|
if entry.Data != nil && crt != CacheReadTypeForceRefresh {
|
2019-11-14 18:56:24 +00:00
|
|
|
// allow unsafe read, so return data even if expired
|
2020-03-26 21:07:15 +00:00
|
|
|
if crt == CacheReadTypeUnsafe {
|
|
|
|
return entry.Data, nil
|
2019-11-14 18:56:24 +00:00
|
|
|
}
|
|
|
|
// if cached data is not expired, return cached data
|
2020-03-26 21:07:15 +00:00
|
|
|
if crt == CacheReadTypeDefault && time.Since(entry.CreatedOn) < t.TTL {
|
|
|
|
return entry.Data, nil
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-14 00:18:16 +00:00
|
|
|
// Data is not cached yet, cache data is expired or requested force refresh
|
|
|
|
// cache it by getter. entry is locked before getting to ensure concurrent
|
|
|
|
// gets don't result in multiple ARM calls.
|
2020-03-26 21:07:15 +00:00
|
|
|
data, err := t.Getter(key)
|
2019-11-14 18:56:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the data in cache and also set the last update time
|
|
|
|
// to now as the data was recently fetched
|
2020-03-26 21:07:15 +00:00
|
|
|
entry.Data = data
|
|
|
|
entry.CreatedOn = time.Now().UTC()
|
2019-09-27 21:51:53 +00:00
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
return entry.Data, nil
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes an item from the cache.
|
2020-03-26 21:07:15 +00:00
|
|
|
func (t *TimedCache) Delete(key string) error {
|
|
|
|
return t.Store.Delete(&AzureCacheEntry{
|
|
|
|
Key: key,
|
2019-09-27 21:51:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the data cache for the key.
|
|
|
|
// It is only used for testing.
|
2020-03-26 21:07:15 +00:00
|
|
|
func (t *TimedCache) Set(key string, data interface{}) {
|
|
|
|
t.Store.Add(&AzureCacheEntry{
|
|
|
|
Key: key,
|
|
|
|
Data: data,
|
|
|
|
CreatedOn: time.Now().UTC(),
|
2019-09-27 21:51:53 +00:00
|
|
|
})
|
|
|
|
}
|