hackpar / redis

Automatically exported from code.google.com/p/redis
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

IncrBy Float Value #485

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What version of Redis you are using, in what kind of Operating System?
2.2
What is the problem you are experiencing?
Increment by Float value not working. 
What steps will reproduce the problem?
Incr foo 0.3
Do you have an INFO output? Please past it here.

---
If it is a crash, can you please paste the stack trace that you can find in
the log file or on standard output? This is really useful for us!

---
Please provide any additional information below.

---

Original issue reported on code.google.com by vai.gol...@gmail.com on 15 Mar 2011 at 10:58

GoogleCodeExporter commented 8 years ago
incr and incrby only work on 64 bit signed integers not floats, see 
http://redis.io/commands/incrby and http://redis.io/commands/incr

Original comment by rowan%wo...@gtempaccount.com on 15 Mar 2011 at 12:27

GoogleCodeExporter commented 8 years ago
is there any function available for increment by float just like increment by 
integer value?

Original comment by vai.gol...@gmail.com on 15 Mar 2011 at 12:29

GoogleCodeExporter commented 8 years ago
No, if you need an atomic float increment you're best off using something like
WATCH key
$var = GET key
MULTI
SET key $var+0.3
EXEC

Original comment by rowan%wo...@gtempaccount.com on 15 Mar 2011 at 12:33

GoogleCodeExporter commented 8 years ago
Another approach would be to use a multiplying factor to convert floats to 
ints, as big as the needed precision you want. I use 10000.

Original comment by dvir...@gmail.com on 15 Mar 2011 at 12:37

GoogleCodeExporter commented 8 years ago
Th problem is using step by step Get and Set is, By the time script set the 
value It might have been changed from what is fetched in get. Specially in 
multi process environment we might end up setting wrong or old value. 

A new IncrFloat would be best or getset accept mathematical operator like 
getset +0.3

Original comment by vai.gol...@gmail.com on 15 Mar 2011 at 12:38

GoogleCodeExporter commented 8 years ago
Yeah Dvirsky, We are using a similar approach of x1000. but having some 
incrbyfloat would be best if possible.

Original comment by vai.gol...@gmail.com on 15 Mar 2011 at 12:40

GoogleCodeExporter commented 8 years ago
Thats why you use WATCH

Here is some pseudo code 

$success = null
while($success == null)
{
  WATCH key
  $var = GET key
  MULTI
  SET key $var+0.3
  $success = EXEC
}

It will keep getting/setting until it sets successfully, the WATCH statement 
will cause the MULTI EXEC statement to return null if key is changed by another 
process. 

Original comment by rowan%wo...@gtempaccount.com on 15 Mar 2011 at 12:49

GoogleCodeExporter commented 8 years ago
Watch make sense.. Thanks Rowan!!!

Original comment by vai.gol...@gmail.com on 15 Mar 2011 at 12:56

GoogleCodeExporter commented 8 years ago
Just a random thought, but you may be able to treat the float value as a 64bit 
int in your redisclient (i.e. by coercing the 64bit int into a 64bit float) and 
then work out the incremental difference on the redisclient then send that 
value to Redis with INCRBY? 

Original comment by demis.be...@gmail.com on 15 Mar 2011 at 12:57

GoogleCodeExporter commented 8 years ago
I agree that this feature request would be handy, and would round out the incr* 
operators nicely.  Especially useful for keeping tracks of stats and money.

Original comment by findch...@gmail.com on 13 Sep 2011 at 7:04

GoogleCodeExporter commented 8 years ago
Hello, this feature is accepted. We'll have INCRBYFLOAT for 2.6.
Maybe we'll back port the feature into 2.4 at some point, I'm not sure.

Thanks for the input!
Salvatore

Original comment by anti...@gmail.com on 15 Sep 2011 at 3:06