Store(staging/src/k8s.io/client-go/tools/cache/store.go:34)是一个通用的对象存储接口
Reflector中会包含Store,监听server的变化,并更新Store。Store提供了常见的存储接口:
type Store interface {
Add(obj interface{}) error
Update(obj interface{}) error
Delete(obj interface{}) error
List() []interface{}
ListKeys() []string
Get(obj interface{}) (item interface{}, exists bool, err error)
GetByKey(key string) (item interface{}, exists bool, err error)
// Replace will delete the contents of the store, using instead the
// given list. Store takes ownership of the list, you should not reference
// it after calling this function.
Replace([]interface{}, string) error
Resync() error
}
一个通用的存储对象,cache
// cache responsibilities are limited to:
// 1. Computing keys for objects via keyFunc
// 2. Invoking methods of a ThreadSafeStorage interface
type cache struct {
// cacheStorage bears the burden of thread safety for the cache
//线程安全的基于Map的内存存储
cacheStorage ThreadSafeStore
// keyFunc is used to make the key for objects stored in and retrieved from items, and
// should be deterministic.
//类似一个hash函数,输入一个对象,输出该对象的key,该key用于map中检索对象。
keyFunc KeyFunc
}
//golang中的一个用法,检查cache是否实现了Store接口
var _ Store = &cache{}
cache其他的一些函数:
// Index returns a list of items that match on the index function
// Index is thread-safe so long as you treat all items as immutable
// indexName: 特征提取函数,上一篇有介绍
// 该函数返回 指定特征提取函数下,与obj有相同特征的所有对象
func (c *cache) Index(indexName string, obj interface{}) ([]interface{}, error) {
return c.cacheStorage.Index(indexName, obj)
}
对外的函数:
// NewStore returns a Store implemented simply with a map and a lock.
//构造一个通用的存储对象cache
func NewStore(keyFunc KeyFunc) Store {
return &cache{
cacheStorage: NewThreadSafeStore(Indexers{}, Indices{}),
keyFunc: keyFunc,
}
}