Skip to content

Commit 8084bd0

Browse files
committed
Inline expiration checks manually for performance
1 parent eb4f9f6 commit 8084bd0

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

cache.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
112112
// whether the key was found.
113113
func (c *cache) Get(k string) (interface{}, bool) {
114114
c.mu.RLock()
115-
// "Inlining" of get
115+
// "Inlining" of get and expired
116116
item, found := c.items[k]
117-
if !found || item.Expired() {
117+
if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) {
118118
c.mu.RUnlock()
119119
return nil, false
120120
}
@@ -124,7 +124,8 @@ func (c *cache) Get(k string) (interface{}, bool) {
124124

125125
func (c *cache) get(k string) (interface{}, bool) {
126126
item, found := c.items[k]
127-
if !found || item.Expired() {
127+
// "Inlining" of expired
128+
if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) {
128129
return nil, false
129130
}
130131
return item.Object, true
@@ -884,7 +885,8 @@ func (c *cache) DeleteExpired() {
884885
now := time.Now().UnixNano()
885886
c.mu.Lock()
886887
for k, v := range c.items {
887-
if v.expired(now) {
888+
// "Inlining" of expired
889+
if v.Expiration > 0 && now > v.Expiration {
888890
ov, evicted := c.delete(k)
889891
if evicted {
890892
evictedItems = append(evictedItems, keyAndValue{k, ov})

0 commit comments

Comments
 (0)