Indexer是informer机制中的本地缓存(带索引index),可以通过读本地缓存的资源对象,减少对api-server的请求压力。
type Indexer interface {
// 可以直接理解成本地缓存。
Store
// 在本地缓存上,增加一些index的方法
Index(indexName string, obj interface{}) ([]interface{}, error)
IndexKeys(indexName, indexedValue string) ([]string, error)
ListIndexFuncValues(indexName string) []string
ByIndex(indexName, indexedValue string) ([]interface{}, error)
GetIndexers() Indexers
AddIndexers(newIndexers Indexers) error
}
type Index map[string]sets.String
type IndexFunc func(obj interface{}) ([]string, error)
type Indexers map[string]IndexFunc
type Indices map[string]Index
type cache struct {
// 一个接口方法
cacheStorage ThreadSafeStore
// 计算func
keyFunc KeyFunc
}
// threadSafeMap implements ThreadSafeStore
type threadSafeMap struct {
lock sync.RWMutex
// 本地缓存 是一个map
items map[string]interface{}
// 跟索引有关的属性
// indexers maps a name to an IndexFunc
indexers Indexers
// indices maps a name to an Index
indices Indices
}