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

why struct Cache contains pointer to cache?

Open chengxwcq opened this issue 4 years ago • 2 comments

it looks like struct cache is unnecessary in the code. I notice the comment // If this is confusing, see the comment at the bottom of New(), but i didn't find any explaination

chengxwcq avatar Oct 12 '21 08:10 chengxwcq

https://github.com/patrickmn/go-cache/blob/46f407853014144407b6c2ec7ccc76bf67958d93/cache.go#L1115

thediveo avatar Feb 15 '22 21:02 thediveo

IMO, Since, the janitor is working in a go routine on cache , c object. It will never be available for garbage collection. Hence, Cache struct is designed to have cache as a field. If Cache struct is garbage collected, stopJanitor is called using the runtime.setFinalizer. runtime.setFinalizer is used to call a function, here stopJanitor, when the first operand , here C, is garbage collected.

john-nash-rs avatar Mar 19 '23 10:03 john-nash-rs