ananthakumaran/memcachex

Encoding issue on incr

Closed this issue · 2 comments

Hi

I'm using memcache's increment but it is not working after a get.

iex(3)> Memcache.incr(Worker, "my_key")
{:ok, 3}
iex(4)> Memcache.get(Worker, "my_key")
** (ArgumentError) argument error
    :erlang.binary_to_term("3")
    (memcachex) lib/memcache/coder/erlang.ex:9: Memcache.Coder.Erlang.decode/2
    (memcachex) lib/memcache.ex:488: Memcache.decode_response/2

Maybe the value is not encoded on incr and decr?

=== UPDATE ===
I'm using Erlang coder, when I change to JSON it works.

I don't think incr/decr etc will work if you use any coder except raw(which is the default). It's probably accidental that incr works with JSON encoder.

Memcache.incr means, we ask server to increment value by n. We don't send the incremented value. So server has to understand how the value is encoded to increment.

coder will only work with commands where you treat the value as opaque bytes. If server has to interpret the bytes to integer or something, then it's likely that it won't work.

check Memcache.cas if you want to perform atomic updates with non default coders.

Memcache.incr means, we ask server to increment value by n. We don't send the incremented value.

I didn't know that, it is clear for me now.

Thank you