@@ -48,7 +48,7 @@ type ExpirationCache struct {
48
48
// ExpirationPolicy dictates when an object expires. Currently only abstracted out
49
49
// so unittests don't rely on the system clock.
50
50
type ExpirationPolicy interface {
51
- IsExpired (obj * timestampedEntry ) bool
51
+ IsExpired (obj * TimestampedEntry ) bool
52
52
}
53
53
54
54
// TTLPolicy implements a ttl based ExpirationPolicy.
@@ -63,26 +63,29 @@ type TTLPolicy struct {
63
63
64
64
// IsExpired returns true if the given object is older than the ttl, or it can't
65
65
// determine its age.
66
- func (p * TTLPolicy ) IsExpired (obj * timestampedEntry ) bool {
67
- return p .Ttl > 0 && p .Clock .Since (obj .timestamp ) > p .Ttl
66
+ func (p * TTLPolicy ) IsExpired (obj * TimestampedEntry ) bool {
67
+ return p .Ttl > 0 && p .Clock .Since (obj .Timestamp ) > p .Ttl
68
68
}
69
69
70
- // timestampedEntry is the only type allowed in a ExpirationCache.
71
- type timestampedEntry struct {
72
- obj interface {}
73
- timestamp time.Time
70
+ // TimestampedEntry is the only type allowed in a ExpirationCache.
71
+ // Keep in mind that it is not safe to share timestamps between computers.
72
+ // Behavior may be inconsistent if you get a timestamp from the API Server and
73
+ // use it on the client machine as part of your ExpirationCache.
74
+ type TimestampedEntry struct {
75
+ Obj interface {}
76
+ Timestamp time.Time
74
77
}
75
78
76
- // getTimestampedEntry returns the timestampedEntry stored under the given key.
77
- func (c * ExpirationCache ) getTimestampedEntry (key string ) (* timestampedEntry , bool ) {
79
+ // getTimestampedEntry returns the TimestampedEntry stored under the given key.
80
+ func (c * ExpirationCache ) getTimestampedEntry (key string ) (* TimestampedEntry , bool ) {
78
81
item , _ := c .cacheStorage .Get (key )
79
- if tsEntry , ok := item .(* timestampedEntry ); ok {
82
+ if tsEntry , ok := item .(* TimestampedEntry ); ok {
80
83
return tsEntry , true
81
84
}
82
85
return nil , false
83
86
}
84
87
85
- // getOrExpire retrieves the object from the timestampedEntry if and only if it hasn't
88
+ // getOrExpire retrieves the object from the TimestampedEntry if and only if it hasn't
86
89
// already expired. It holds a write lock across deletion.
87
90
func (c * ExpirationCache ) getOrExpire (key string ) (interface {}, bool ) {
88
91
// Prevent all inserts from the time we deem an item as "expired" to when we
@@ -95,11 +98,11 @@ func (c *ExpirationCache) getOrExpire(key string) (interface{}, bool) {
95
98
return nil , false
96
99
}
97
100
if c .expirationPolicy .IsExpired (timestampedItem ) {
98
- klog .V (4 ).Infof ("Entry %v: %+v has expired" , key , timestampedItem .obj )
101
+ klog .V (4 ).Infof ("Entry %v: %+v has expired" , key , timestampedItem .Obj )
99
102
c .cacheStorage .Delete (key )
100
103
return nil , false
101
104
}
102
- return timestampedItem .obj , true
105
+ return timestampedItem .Obj , true
103
106
}
104
107
105
108
// GetByKey returns the item stored under the key, or sets exists=false.
@@ -126,7 +129,7 @@ func (c *ExpirationCache) List() []interface{} {
126
129
127
130
list := make ([]interface {}, 0 , len (items ))
128
131
for _ , item := range items {
129
- obj := item .(* timestampedEntry ). obj
132
+ obj := item .(* TimestampedEntry ). Obj
130
133
if key , err := c .keyFunc (obj ); err != nil {
131
134
list = append (list , obj )
132
135
} else if obj , exists := c .getOrExpire (key ); exists {
@@ -151,7 +154,7 @@ func (c *ExpirationCache) Add(obj interface{}) error {
151
154
c .expirationLock .Lock ()
152
155
defer c .expirationLock .Unlock ()
153
156
154
- c .cacheStorage .Add (key , & timestampedEntry {obj , c .clock .Now ()})
157
+ c .cacheStorage .Add (key , & TimestampedEntry {obj , c .clock .Now ()})
155
158
return nil
156
159
}
157
160
@@ -184,7 +187,7 @@ func (c *ExpirationCache) Replace(list []interface{}, resourceVersion string) er
184
187
if err != nil {
185
188
return KeyError {item , err }
186
189
}
187
- items [key ] = & timestampedEntry {item , ts }
190
+ items [key ] = & TimestampedEntry {item , ts }
188
191
}
189
192
c .expirationLock .Lock ()
190
193
defer c .expirationLock .Unlock ()
@@ -199,10 +202,15 @@ func (c *ExpirationCache) Resync() error {
199
202
200
203
// NewTTLStore creates and returns a ExpirationCache with a TTLPolicy
201
204
func NewTTLStore (keyFunc KeyFunc , ttl time.Duration ) Store {
205
+ return NewExpirationStore (keyFunc , & TTLPolicy {ttl , clock.RealClock {}})
206
+ }
207
+
208
+ // NewExpirationStore creates and returns a ExpirationCache for a given policy
209
+ func NewExpirationStore (keyFunc KeyFunc , expirationPolicy ExpirationPolicy ) Store {
202
210
return & ExpirationCache {
203
211
cacheStorage : NewThreadSafeStore (Indexers {}, Indices {}),
204
212
keyFunc : keyFunc ,
205
213
clock : clock.RealClock {},
206
- expirationPolicy : & TTLPolicy { ttl , clock. RealClock {}} ,
214
+ expirationPolicy : expirationPolicy ,
207
215
}
208
216
}
0 commit comments