-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nebulex.Caching.SimpleKeyGenerator should produce a unique key #122
Comments
Hey 👋 ! Yeah, in version 2.1 that part changed a bit, the Let me check if maybe makes more sense to change it and make it similar to how it was before. In the meantime, you can also provide a very simple generator like: defmodule MyApp.Cache.KeyGenerator do
@behaviour Nebulex.Caching.KeyGenerator
@impl true
def generate(mod, fun, args), do: :erlang.phash2({mod, fun, args})
end On the other hand, regarding the suggestion about the Thanks 👍 !! |
Hi @nmbrone 👋
defmodule Example do
use Nebulex.Caching
@decorate cacheable(cache: Cache)
def get_something(key) do
# get logic
end
@decorate cache_evict(cache: Cache)
def del_something(key) do
# delete logic
end
end If the generator includes the module and function to compute the key, the code above wouldn't work, because you will get different keys for each function, so for the most basic and simplest scenarios, it wouldn't work. On the other hand, with the current implementation of The idea behind
Thanks. |
Hi @cabol, First of all, big thanks for your time. I really love your idea of adding the Given the previous default key implementation https://github.com/cabol/nebulex/blob/v2.0.0/lib/nebulex/caching.ex#L407-L412 key_var =
Keyword.get(
attrs,
:key,
quote(do: :erlang.phash2({unquote(context.module), unquote(context.name)}))
) I would expect defmodule Nebulex.Caching.SimpleKeyGenerator do
@behaviour Nebulex.Caching.KeyGenerator
@impl true
def generate(mod, fun, _args), do: :erlang.phash2({mod, fun})
end But since it's different, a blind update from v2.0 to v2.1 would have broken our app (and who knows how many others). It isn't something you would expect from a minor version. Obviously, it was easy to fix by providing our own key generator, but it was pretty annoying that we would have to provide it as the option to each Anyway, I hope this conversation will help someone save hours of debugging. Please, feel free to close the issue whenever you like. And once again, thanks for the great work! |
Hey 👋 !! First of all, thanks for your feedback, it has been very helpful. About the default generator, I agree with you in the sense the version Finally, thanks a lot for the suggestion about the key generator option at compile-time, I think it is very useful, especially for avoiding adding the generator on each cache annotation, which is very tedious ugly (since we end up adding the same option to all annotated functions). This feature will avoid that. PD: I will close the issue once I push some documentation about the key generation changes as I mentioned earlier, so it can help others, along with this conversation as you mentioned, to save up time debugging trying to the issue. Thanks. |
@nmbrone @cabol thanks for clarifying this - this really caught us out and would have caused security issues if we hadn't caught it in CI. We didn't expect to need to provide a key to |
@tomtaylor absolutely 💯 , this kind of breaking changes will be in a major version next time, thanks 👍 !! |
Hi there 👋
An example:
In version 2.0 the code above will result in having two entries in the cache with the (unique) keys
:erlang.phash2({MyApp.A, :get_something})
and:erlang.phash2({MyApp.B, :get_something})
,but version 2.1 will end up with the key
0
in both cases and one entry will overwrite another. This could lead to very unexpected results for the one who already did the upgrade.I guess the fix is pretty straightforward and changing
Nebulex.Caching.SimpleKeyGenerator
should be enough.Also, the option
:key_generator
would make so much sense here:The text was updated successfully, but these errors were encountered: