go-cache icon indicating copy to clipboard operation
go-cache copied to clipboard

Question

Open nassiharel opened this issue 3 years ago • 1 comments

Is there any reason for not using defer when unlocking mutex?

func (c *cache) Get(k string) (interface{}, bool) {
	c.mu.RLock()
        defer c.mu.RUnlock()
	// "Inlining" of get and Expired
	item, found := c.items[k]
	if !found {
		return nil, false
	}
	if item.Expiration > 0 {
		if time.Now().UnixNano() > item.Expiration {
			return nil, false
		}
	}
	return item.Object, true
}

nassiharel avatar Mar 26 '22 07:03 nassiharel

It used to have a reasonably significant performance impact. That was improved a few Go versions ago, and now the performance impact is negligible (I benchmarked this for my fork).

Note this package doesn't seem maintained; I have a fork at https://github.com/arp242/zcache with some fixes.

arp242 avatar Mar 26 '22 07:03 arp242