77
88cache 实现了 ` Store `  和 ` Indexer `  接口,函数 ` NewIndexer() `  和 ` NewStore() `  均返回 cache 类型的对象。
99
10- cache 在很多地方都有应用 ,如各种 ` Informer `  和 ` DeltaFIFO `  使用它做对象缓存和索引 。
10+ cache 使用比较广泛 ,如各种 ` Informer `  和 ` DeltaFIFO `  用它做缓存和索引对象 。
1111
12- ## Store  
12+ ## 对象缓存  Store 
1313
1414Store 是 KV 类型的对象缓存:
1515
@@ -30,13 +30,13 @@ type Store interface {
3030}
3131``` 
3232
33- ` NewStore `  函数返回一个实现该接口的 ` struct cache `  类型对象(见后文分析)。
33+ ` NewStore()  `  函数返回一个实现该接口的 ` struct cache `  类型对象(见后文分析)。
3434
35- ` Queue `  接口是 ` Store `  的超级,所以实现 ` Queue `  接口的 ` FIFO `  类型(fifo.go) 、` DeltaFIFO `  类型(delta_file.go) 也实现了  ` Store ` 接口。(详见: [ 2.queue-fifo-delta_fifo.md] ( ./2.queue-fifo-delta_fifo.md ) )
35+ ` Queue `  接口是 ` Store `  的超级,所以实现 ` Queue `  接口的 ` FIFO `  类型、` DeltaFIFO `  类型也实现了  ` Store ` 接口。(详见: [ 2.queue-fifo-delta_fifo.md] ( ./2.queue-fifo-delta_fifo.md ) )
3636
37- ## 索引接口  Indexer 
37+ ## 对象索引  Indexer 
3838
39- Index  是在 Store 的基础上,添加了索引功能,方便后续快速获取 (一批)对象。
39+ Indexer  是在 Store 的基础上,添加了索引功能,方便快速获取 (一批)对象。
4040
4141```  go 
4242//  来源于 k8s.io/client-go/tools/cache/index.go
@@ -61,13 +61,13 @@ type Indexer interface {
6161}
6262``` 
6363
64- ` NewIndexer `  函数返回一个实现该接口的 ` cache `  类型对象(见后文分析)。
64+ ` NewIndexer()  `  函数返回一个实现该接口的 ` struct  cache`  类型对象(见后文分析)。
6565
6666## 为对象生成索引值列表的 IndexFunc 和命名的 IndexFunc 集合 Indexers  
6767
68- Indexer 在 Store 的基础上添加了对象索引功能。 对象的索引是一个字符串列表,由 IndexFunc 类型的函数生成,所以索引值和对象(用它的唯一表示 Key 表示)是一对多映射关系 。
68+ 对象的索引是一个字符串列表,由 IndexFunc 类型的函数生成,所以索引值和对象(用它的唯一表示 Key 表示)是 ** 一对多 ** 映射关系 。
6969
70- client-go package 中提供了名为  ` NamespaceIndex string = "namespace" `  的 IndexFunc 类型函数 ` MetaNamespaceIndexFunc ` ,它提取对象的 ` Namespace `  作为索引:
70+ client-go package 提供了名为  ` NamespaceIndex string = "namespace" `  的 IndexFunc 类型函数 ` MetaNamespaceIndexFunc ` ,它提取对象的 ` Namespace `  作为索引:
7171
7272```  go 
7373//  来源于 k8s.io/client-go/tools/cache/index.go
@@ -88,9 +88,11 @@ func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
8888type  Indexers  map [string ]IndexFunc
8989``` 
9090
91+ Indexer 接口的 ` AddIndexers() `  方法为对象添加索引函数,从而为对象生成** 不同类型** 的索引,后续按需查找。
92+ 9193类似于 IndexFunc 为对象生成索引值列表,` KeyFunc `  函数(见后文)为对象生成一个唯一的标识字符串,称为对象 Key。
9294
93- ## 对象索引  Index 和 Indices 
95+ ## 索引缓存  Index 和 Indices 
9496
9597前面说过,索引值和对象(用它的唯一表示 Key 表示)是一对多映射关系。
9698
@@ -106,13 +108,13 @@ type Index map[string]sets.String
106108type  Indices  map [string ]Index
107109``` 
108110
109- 可以调用实现 Indexer 接口的对象的 ` AddIndexers() `  方法,从而实现为对象生成** 不同类型** 的索引,方便后续按需查找。
110- 111- ## 多线程安全的、带有索引的缓存 ThreadSafeStore  
111+ ## 可并发访问的索引缓存 ThreadSafeStore  
112112
113113` ThreadSafeStore `  通过锁机制,实现多 goroutine 可以并发访问的、带有索引功能(` Indexer `  接口)的对象缓存(` Store `  接口)。
114114
115- ` ThreadSafeStore `  本身没有实现 ` Indexer `  和 ` Store `  接口,但是包含它们定义的同名方法,后文会介绍,` struct cache `  类型内部使用 ` ThreadSafeStore `  实现了 ` Indexer `  和 ` Store `  接口。
115+ ` ThreadSafeStore `  本身没有实现 ` Indexer `  和 ` Store `  接口,但包含同名方法。
116+ 117+ 后文会介绍,` struct cache `  类型内部使用 ` ThreadSafeStore `  实现了 ` Indexer `  和 ` Store `  接口。
116118
117119```  go 
118120//  来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -139,8 +141,6 @@ type ThreadSafeStore interface {
139141}
140142``` 
141143
142- 后文会具体分析这些方法的功能。
143- 144144` NewThreadSafeStore `  返回一个实现该接口的对象,该对象的类型是 ` threadSafeMap ` :
145145
146146```  go 
@@ -154,7 +154,7 @@ func NewThreadSafeStore(indexers Indexers, indices Indices) ThreadSafeStore {
154154}
155155``` 
156156
157- ` threadSafeMap `  使用内置的  ` items `  缓存所有对象:
157+ ` threadSafeMap `  使用 map  缓存所有对象:
158158
159159```  go 
160160//  来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -170,7 +170,7 @@ type threadSafeMap struct {
170170}
171171``` 
172172
173- 我们看看  ` threadSafeMap `  的方法实现 :
173+ ` Add/Update() `  方法 :
174174
175175```  go 
176176//  来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -191,9 +191,9 @@ func (c *threadSafeMap) Update(key string, obj interface{}) {
191191}
192192``` 
193193
194- 当 ` Add()/Update() `  一个 obj 时,先使用传入的 obj 更新缓存 ,然后用 ` updateIndices() `  方法更新索引。
194+ 当 ` Add()/Update() `  一个对象时,先将它加到缓存 ,然后用 ` updateIndices() `  方法更新索引。
195195
196- ` updateIndices() `  方法分别使用  ` c.indexers `  中的索引函数,为对象创建** 多种类型** 索引值列表,然后将这些索引及对象的 Key 更新到索引缓存中(` c.indices ` )。
196+ ` updateIndices() `  方法使用  ` c.indexers `  中的索引函数,为对象创建** 多种类型** 索引值列表,然后将这些索引及对象的 Key 更新到索引缓存中(` c.indices ` )。
197197
198198```  go 
199199//  来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -227,7 +227,7 @@ func (c *threadSafeMap) updateIndices(oldObj interface{}, newObj interface{}, ke
227227}
228228``` 
229229
230- ` Delete() `  方法先后从索引缓存和对象缓存中删除对象。` deleteFromIndices() `  方法遍历 ` c.indexers `  中的索引函数,为对象计算索引值列表,然后再从索引缓存和对象缓存中删除该对象:
230+ ` Delete() `  方法先后从索引缓存和对象缓存中删除对象。` deleteFromIndices() `  方法遍历 ` c.indexers `  中的索引函数,为对象计算索引值列表,然后再从索引缓存和对象缓存中删除该对象:
231231
232232```  go 
233233//  来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -243,7 +243,7 @@ func (c *threadSafeMap) Delete(key string) {
243243}
244244``` 
245245
246- 注意:因为一个索引值可能匹配多个对象 ,所以不能直接删除索引缓存中索引值对应的对象集合。
246+ 注意:一个索引值可能匹配多个对象 ,所以不能直接删除索引缓存中索引值对应的对象集合。
247247
248248` Replace() `  方法使用传入的对象列表替换内部缓存,然后重建索引:
249249
0 commit comments