Hacker News new | ask | show | jobs
by bastawhiz 721 days ago
The HSET calls are not just unnecessary, they're a bug. It introduces the possibility of a race condition where a view or like happens between the HGET and HSET. HSETing the values to zero doesn't actually make anything faster or more correct, it just introduces the possibility of overwriting a value that came in moments ago.
1 comments

so how would you approach this?
Just do hincrby alone, if it doesn't exist it will automatically become 1
You are right. It just should be like so:

Get:

   const likes = (await kv.hget(postTitle, "likes")) || 0
Post:

   const likes = await kv.hincrby(postTitle, "likes", 1);