sm
/
cache
1
0
Fork 0

Inline expiration checks manually for performance

This commit is contained in:
Patrick Mylund Nielsen 2015-11-30 14:12:45 -05:00
parent eb4f9f6b2f
commit 8084bd02b5
1 changed files with 6 additions and 4 deletions

View File

@ -112,9 +112,9 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
// whether the key was found. // whether the key was found.
func (c *cache) Get(k string) (interface{}, bool) { func (c *cache) Get(k string) (interface{}, bool) {
c.mu.RLock() c.mu.RLock()
// "Inlining" of get // "Inlining" of get and expired
item, found := c.items[k] item, found := c.items[k]
if !found || item.Expired() { if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) {
c.mu.RUnlock() c.mu.RUnlock()
return nil, false return nil, false
} }
@ -124,7 +124,8 @@ func (c *cache) Get(k string) (interface{}, bool) {
func (c *cache) get(k string) (interface{}, bool) { func (c *cache) get(k string) (interface{}, bool) {
item, found := c.items[k] item, found := c.items[k]
if !found || item.Expired() { // "Inlining" of expired
if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) {
return nil, false return nil, false
} }
return item.Object, true return item.Object, true
@ -884,7 +885,8 @@ func (c *cache) DeleteExpired() {
now := time.Now().UnixNano() now := time.Now().UnixNano()
c.mu.Lock() c.mu.Lock()
for k, v := range c.items { for k, v := range c.items {
if v.expired(now) { // "Inlining" of expired
if v.Expiration > 0 && now > v.Expiration {
ov, evicted := c.delete(k) ov, evicted := c.delete(k)
if evicted { if evicted {
evictedItems = append(evictedItems, keyAndValue{k, ov}) evictedItems = append(evictedItems, keyAndValue{k, ov})