Skip to content

Commit 69fb4e9

Browse files
tedyuyutedz
authored andcommitted
Store key in TimestampedEntry
1 parent 78915a1 commit 69fb4e9

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

staging/src/k8s.io/client-go/tools/cache/expiration_cache.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func (p *TTLPolicy) IsExpired(obj *TimestampedEntry) bool {
7474
type TimestampedEntry struct {
7575
Obj interface{}
7676
Timestamp time.Time
77+
key string
7778
}
7879

7980
// getTimestampedEntry returns the TimestampedEntry stored under the given key.
@@ -129,10 +130,8 @@ func (c *ExpirationCache) List() []interface{} {
129130

130131
list := make([]interface{}, 0, len(items))
131132
for _, item := range items {
132-
obj := item.(*TimestampedEntry).Obj
133-
if key, err := c.keyFunc(obj); err != nil {
134-
list = append(list, obj)
135-
} else if obj, exists := c.getOrExpire(key); exists {
133+
key := item.(*TimestampedEntry).key
134+
if obj, exists := c.getOrExpire(key); exists {
136135
list = append(list, obj)
137136
}
138137
}
@@ -154,7 +153,7 @@ func (c *ExpirationCache) Add(obj interface{}) error {
154153
c.expirationLock.Lock()
155154
defer c.expirationLock.Unlock()
156155

157-
c.cacheStorage.Add(key, &TimestampedEntry{obj, c.clock.Now()})
156+
c.cacheStorage.Add(key, &TimestampedEntry{obj, c.clock.Now(), key})
158157
return nil
159158
}
160159

@@ -187,7 +186,7 @@ func (c *ExpirationCache) Replace(list []interface{}, resourceVersion string) er
187186
if err != nil {
188187
return KeyError{item, err}
189188
}
190-
items[key] = &TimestampedEntry{item, ts}
189+
items[key] = &TimestampedEntry{item, ts, key}
191190
}
192191
c.expirationLock.Lock()
193192
defer c.expirationLock.Unlock()

staging/src/k8s.io/client-go/tools/cache/expiration_cache_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ func TestTTLPolicy(t *testing.T) {
168168
expiredTime := fakeTime.Add(-(ttl + 1))
169169

170170
policy := TTLPolicy{ttl, clock.NewFakeClock(fakeTime)}
171-
fakeTimestampedEntry := &TimestampedEntry{Obj: struct{}{}, Timestamp: exactlyOnTTL}
171+
item := testStoreObject{id: "foo", val: "bar"}
172+
itemkey, _ := testStoreKeyFunc(item)
173+
fakeTimestampedEntry := &TimestampedEntry{Obj: item, Timestamp: exactlyOnTTL, key: itemkey}
172174
if policy.IsExpired(fakeTimestampedEntry) {
173175
t.Errorf("TTL cache should not expire entries exactly on ttl")
174176
}

0 commit comments

Comments
 (0)