golang-lru icon indicating copy to clipboard operation
golang-lru copied to clipboard

Example output is not matched with actual output

Open rahfar opened this issue 1 year ago • 4 comments

Hi, I tried this example (go version go1.21.1 linux/amd64) And instead of

value before expiration is found: true, value: "val1"
value after expiration is found: false, value: ""
Cache len: 1

got

value before expiration is found: true, value: "val1"
value after expiration is found: true, value: ""
Cache len: 2

Should I make pr to readme or lib is not supposed to work like this?

rahfar avatar Sep 22 '23 06:09 rahfar

Discovered that the problem is in waiting for expiration, changing time from 12ms to 100ms fixed the behavior. Can anybody explain such behavior - is it a bug specific to my machine or generally it is better to use bigger TTL values?

// wait for cache to expire
time.Sleep(time.Millisecond * 12)

rahfar avatar Sep 27 '23 21:09 rahfar

I think you've run into this bug: https://github.com/hashicorp/golang-lru/issues/150 which has been fixed and will be in the next release

irenarindos avatar Sep 29 '23 13:09 irenarindos

Tried with the latest version 2.0.7, but the behavior remains the same. With a TTL of 10 ms (as in the example) and a sleep time of 90 ms, I am getting different results from time to time:

$ go run main.go 
value before expiration is found: true, value: "val1"
value after expiration is found: false, value: ""
Cache len: 2

$ go run main.go 
value before expiration is found: true, value: "val1"
value after expiration is found: false, value: ""
Cache len: 2

$ go run main.go 
value before expiration is found: true, value: "val1"
value after expiration is found: false, value: ""
Cache len: 1

$ go run main.go 
value before expiration is found: true, value: "val1"
value after expiration is found: false, value: ""
Cache len: 2

$ go run main.go 
value before expiration is found: true, value: "val1"
value after expiration is found: false, value: ""
Cache len: 2

$ go run main.go 
value before expiration is found: true, value: "val1"
value after expiration is found: false, value: ""
Cache len: 1

With sleep time of 190 ms works as expected.

In the new version value is found appears to be functioning correctly, but the length still appears to be asynchronous

rahfar avatar Sep 30 '23 19:09 rahfar

I would propose to alter the example in the code and Readme to have values which produce consistent results.

paskal avatar Nov 30 '23 12:11 paskal