mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-25 14:41:53 +00:00
Store key in TimestampedEntry
Kubernetes-commit: 69fb4e960f7359641b3503b734acc32af6b05e04
This commit is contained in:
parent
ba8f4e8713
commit
a865205fb0
11
tools/cache/expiration_cache.go
vendored
11
tools/cache/expiration_cache.go
vendored
@ -74,6 +74,7 @@ func (p *TTLPolicy) IsExpired(obj *TimestampedEntry) bool {
|
|||||||
type TimestampedEntry struct {
|
type TimestampedEntry struct {
|
||||||
Obj interface{}
|
Obj interface{}
|
||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
|
key string
|
||||||
}
|
}
|
||||||
|
|
||||||
// getTimestampedEntry returns the TimestampedEntry stored under the given key.
|
// getTimestampedEntry returns the TimestampedEntry stored under the given key.
|
||||||
@ -129,10 +130,8 @@ func (c *ExpirationCache) List() []interface{} {
|
|||||||
|
|
||||||
list := make([]interface{}, 0, len(items))
|
list := make([]interface{}, 0, len(items))
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
obj := item.(*TimestampedEntry).Obj
|
key := item.(*TimestampedEntry).key
|
||||||
if key, err := c.keyFunc(obj); err != nil {
|
if obj, exists := c.getOrExpire(key); exists {
|
||||||
list = append(list, obj)
|
|
||||||
} else if obj, exists := c.getOrExpire(key); exists {
|
|
||||||
list = append(list, obj)
|
list = append(list, obj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -154,7 +153,7 @@ func (c *ExpirationCache) Add(obj interface{}) error {
|
|||||||
c.expirationLock.Lock()
|
c.expirationLock.Lock()
|
||||||
defer c.expirationLock.Unlock()
|
defer c.expirationLock.Unlock()
|
||||||
|
|
||||||
c.cacheStorage.Add(key, &TimestampedEntry{obj, c.clock.Now()})
|
c.cacheStorage.Add(key, &TimestampedEntry{obj, c.clock.Now(), key})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +186,7 @@ func (c *ExpirationCache) Replace(list []interface{}, resourceVersion string) er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return KeyError{item, err}
|
return KeyError{item, err}
|
||||||
}
|
}
|
||||||
items[key] = &TimestampedEntry{item, ts}
|
items[key] = &TimestampedEntry{item, ts, key}
|
||||||
}
|
}
|
||||||
c.expirationLock.Lock()
|
c.expirationLock.Lock()
|
||||||
defer c.expirationLock.Unlock()
|
defer c.expirationLock.Unlock()
|
||||||
|
4
tools/cache/expiration_cache_test.go
vendored
4
tools/cache/expiration_cache_test.go
vendored
@ -168,7 +168,9 @@ func TestTTLPolicy(t *testing.T) {
|
|||||||
expiredTime := fakeTime.Add(-(ttl + 1))
|
expiredTime := fakeTime.Add(-(ttl + 1))
|
||||||
|
|
||||||
policy := TTLPolicy{ttl, clock.NewFakeClock(fakeTime)}
|
policy := TTLPolicy{ttl, clock.NewFakeClock(fakeTime)}
|
||||||
fakeTimestampedEntry := &TimestampedEntry{Obj: struct{}{}, Timestamp: exactlyOnTTL}
|
item := testStoreObject{id: "foo", val: "bar"}
|
||||||
|
itemkey, _ := testStoreKeyFunc(item)
|
||||||
|
fakeTimestampedEntry := &TimestampedEntry{Obj: item, Timestamp: exactlyOnTTL, key: itemkey}
|
||||||
if policy.IsExpired(fakeTimestampedEntry) {
|
if policy.IsExpired(fakeTimestampedEntry) {
|
||||||
t.Errorf("TTL cache should not expire entries exactly on ttl")
|
t.Errorf("TTL cache should not expire entries exactly on ttl")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user