golang-lru
golang-lru copied to clipboard
Example output is not matched with actual output
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?
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)
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
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
I would propose to alter the example in the code and Readme to have values which produce consistent results.