You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We've been integrating fastcache into our project and @holiman found an interesting corner case that fastcache handles correctly internally, but fails to properly surface it to the user.
In our specific use case, values associated to keys can be empty. So, what we would like to do is insert key->nil mappings and be able to retrieve them. This works half way:
^ returns nil in both cases. You could even set []byte{} and both would still return nil.
Why is this an issue? Because we cannot differentiate between a value being inside-the-cache-and-empty, or a value missing-from-the-cache requiring database access. The current API only allows using Has-then-Get to cover this, but that's not thread safe any more.
One solution would be to change the append line so it special cases the scenario where both dst and the found value is nil, but that won't really work if the user does supply a non-nil dst.
Our best solution until now is to add a second Get method that returns not only the cached value, but also the flag whether it was found or not. This way we don't break the API.
Does this seem good to you? We can open a PR if you want.
The text was updated successfully, but these errors were encountered:
Our best solution until now is to add a second Get method that returns not only the cached value, but also the flag whether it was found or not. This way we don't break the API.
We've been integrating
fastcache
into our project and @holiman found an interesting corner case thatfastcache
handles correctly internally, but fails to properly surface it to the user.In our specific use case, values associated to keys can be empty. So, what we would like to do is insert
key->nil
mappings and be able to retrieve them. This works half way:^ correctly returns
false
and thentrue
.However, if I use
Get
, things don't look so well:^ returns
nil
in both cases. You could even set[]byte{}
and both would still returnnil
.Why is this an issue? Because we cannot differentiate between a value being inside-the-cache-and-empty, or a value missing-from-the-cache requiring database access. The current API only allows using
Has-then-Get
to cover this, but that's not thread safe any more.The fault is a combination of https://github.com/VictoriaMetrics/fastcache/blob/master/fastcache.go#L386, because Go's append will keep
dst
as nil if appending an empty slice; and https://github.com/VictoriaMetrics/fastcache/blob/master/fastcache.go#L161, because it discards thefound
flag and only returnsdst
.One solution would be to change the
append
line so it special cases the scenario where bothdst
and the found value isnil
, but that won't really work if the user does supply a non-nildst
.Our best solution until now is to add a second
Get
method that returns not only the cached value, but also the flag whether it was found or not. This way we don't break the API.Does this seem good to you? We can open a PR if you want.
The text was updated successfully, but these errors were encountered: