kubernetes源码release-1.15——Store & cache

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,
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值