7
7
8
8
cache 实现了 ` Store ` 和 ` Indexer ` 接口,函数 ` NewIndexer() ` 和 ` NewStore() ` 均返回 cache 类型的对象。
9
9
10
- cache 在很多地方都有应用 ,如各种 ` Informer ` 和 ` DeltaFIFO ` 使用它做对象缓存和索引 。
10
+ cache 使用比较广泛 ,如各种 ` Informer ` 和 ` DeltaFIFO ` 用它做缓存和索引对象 。
11
11
12
- ## Store
12
+ ## 对象缓存 Store
13
13
14
14
Store 是 KV 类型的对象缓存:
15
15
@@ -30,13 +30,13 @@ type Store interface {
30
30
}
31
31
```
32
32
33
- ` NewStore ` 函数返回一个实现该接口的 ` struct cache ` 类型对象(见后文分析)。
33
+ ` NewStore() ` 函数返回一个实现该接口的 ` struct cache ` 类型对象(见后文分析)。
34
34
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 ) )
36
36
37
- ## 索引接口 Indexer
37
+ ## 对象索引 Indexer
38
38
39
- Index 是在 Store 的基础上,添加了索引功能,方便后续快速获取 (一批)对象。
39
+ Indexer 是在 Store 的基础上,添加了索引功能,方便快速获取 (一批)对象。
40
40
41
41
``` go
42
42
// 来源于 k8s.io/client-go/tools/cache/index.go
@@ -61,13 +61,13 @@ type Indexer interface {
61
61
}
62
62
```
63
63
64
- ` NewIndexer ` 函数返回一个实现该接口的 ` cache ` 类型对象(见后文分析)。
64
+ ` NewIndexer() ` 函数返回一个实现该接口的 ` struct cache` 类型对象(见后文分析)。
65
65
66
66
## 为对象生成索引值列表的 IndexFunc 和命名的 IndexFunc 集合 Indexers
67
67
68
- Indexer 在 Store 的基础上添加了对象索引功能。 对象的索引是一个字符串列表,由 IndexFunc 类型的函数生成,所以索引值和对象(用它的唯一表示 Key 表示)是一对多映射关系 。
68
+ 对象的索引是一个字符串列表,由 IndexFunc 类型的函数生成,所以索引值和对象(用它的唯一表示 Key 表示)是 ** 一对多 ** 映射关系 。
69
69
70
- client-go package 中提供了名为 ` NamespaceIndex string = "namespace" ` 的 IndexFunc 类型函数 ` MetaNamespaceIndexFunc ` ,它提取对象的 ` Namespace ` 作为索引:
70
+ client-go package 提供了名为 ` NamespaceIndex string = "namespace" ` 的 IndexFunc 类型函数 ` MetaNamespaceIndexFunc ` ,它提取对象的 ` Namespace ` 作为索引:
71
71
72
72
``` go
73
73
// 来源于 k8s.io/client-go/tools/cache/index.go
@@ -88,9 +88,11 @@ func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
88
88
type Indexers map [string ]IndexFunc
89
89
```
90
90
91
+ Indexer 接口的 ` AddIndexers() ` 方法为对象添加索引函数,从而为对象生成** 不同类型** 的索引,后续按需查找。
92
+
91
93
类似于 IndexFunc 为对象生成索引值列表,` KeyFunc ` 函数(见后文)为对象生成一个唯一的标识字符串,称为对象 Key。
92
94
93
- ## 对象索引 Index 和 Indices
95
+ ## 索引缓存 Index 和 Indices
94
96
95
97
前面说过,索引值和对象(用它的唯一表示 Key 表示)是一对多映射关系。
96
98
@@ -106,13 +108,13 @@ type Index map[string]sets.String
106
108
type Indices map [string ]Index
107
109
```
108
110
109
- 可以调用实现 Indexer 接口的对象的 ` AddIndexers() ` 方法,从而实现为对象生成** 不同类型** 的索引,方便后续按需查找。
110
-
111
- ## 多线程安全的、带有索引的缓存 ThreadSafeStore
111
+ ## 可并发访问的索引缓存 ThreadSafeStore
112
112
113
113
` ThreadSafeStore ` 通过锁机制,实现多 goroutine 可以并发访问的、带有索引功能(` Indexer ` 接口)的对象缓存(` Store ` 接口)。
114
114
115
- ` ThreadSafeStore ` 本身没有实现 ` Indexer ` 和 ` Store ` 接口,但是包含它们定义的同名方法,后文会介绍,` struct cache ` 类型内部使用 ` ThreadSafeStore ` 实现了 ` Indexer ` 和 ` Store ` 接口。
115
+ ` ThreadSafeStore ` 本身没有实现 ` Indexer ` 和 ` Store ` 接口,但包含同名方法。
116
+
117
+ 后文会介绍,` struct cache ` 类型内部使用 ` ThreadSafeStore ` 实现了 ` Indexer ` 和 ` Store ` 接口。
116
118
117
119
``` go
118
120
// 来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -139,8 +141,6 @@ type ThreadSafeStore interface {
139
141
}
140
142
```
141
143
142
- 后文会具体分析这些方法的功能。
143
-
144
144
` NewThreadSafeStore ` 返回一个实现该接口的对象,该对象的类型是 ` threadSafeMap ` :
145
145
146
146
``` go
@@ -154,7 +154,7 @@ func NewThreadSafeStore(indexers Indexers, indices Indices) ThreadSafeStore {
154
154
}
155
155
```
156
156
157
- ` threadSafeMap ` 使用内置的 ` items ` 缓存所有对象:
157
+ ` threadSafeMap ` 使用 map 缓存所有对象:
158
158
159
159
``` go
160
160
// 来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -170,7 +170,7 @@ type threadSafeMap struct {
170
170
}
171
171
```
172
172
173
- 我们看看 ` threadSafeMap ` 的方法实现 :
173
+ ` Add/Update() ` 方法 :
174
174
175
175
``` go
176
176
// 来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -191,9 +191,9 @@ func (c *threadSafeMap) Update(key string, obj interface{}) {
191
191
}
192
192
```
193
193
194
- 当 ` Add()/Update() ` 一个 obj 时,先使用传入的 obj 更新缓存 ,然后用 ` updateIndices() ` 方法更新索引。
194
+ 当 ` Add()/Update() ` 一个对象时,先将它加到缓存 ,然后用 ` updateIndices() ` 方法更新索引。
195
195
196
- ` updateIndices() ` 方法分别使用 ` c.indexers ` 中的索引函数,为对象创建** 多种类型** 索引值列表,然后将这些索引及对象的 Key 更新到索引缓存中(` c.indices ` )。
196
+ ` updateIndices() ` 方法使用 ` c.indexers ` 中的索引函数,为对象创建** 多种类型** 索引值列表,然后将这些索引及对象的 Key 更新到索引缓存中(` c.indices ` )。
197
197
198
198
``` go
199
199
// 来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -227,7 +227,7 @@ func (c *threadSafeMap) updateIndices(oldObj interface{}, newObj interface{}, ke
227
227
}
228
228
```
229
229
230
- ` Delete() ` 方法先后从索引缓存和对象缓存中删除对象。 ` deleteFromIndices() ` 方法遍历 ` c.indexers ` 中的索引函数,为对象计算索引值列表,然后再从索引缓存和对象缓存中删除该对象:
230
+ ` Delete() ` 方法先后从索引缓存和对象缓存中删除对象。` deleteFromIndices() ` 方法遍历 ` c.indexers ` 中的索引函数,为对象计算索引值列表,然后再从索引缓存和对象缓存中删除该对象:
231
231
232
232
``` go
233
233
// 来源于 k8s.io/client-go/tools/cache/thread_safe_store.go
@@ -243,7 +243,7 @@ func (c *threadSafeMap) Delete(key string) {
243
243
}
244
244
```
245
245
246
- 注意:因为一个索引值可能匹配多个对象 ,所以不能直接删除索引缓存中索引值对应的对象集合。
246
+ 注意:一个索引值可能匹配多个对象 ,所以不能直接删除索引缓存中索引值对应的对象集合。
247
247
248
248
` Replace() ` 方法使用传入的对象列表替换内部缓存,然后重建索引:
249
249
0 commit comments