Question about the watch / unwatch

1,316 views
Skip to first unread message

Елена Исаева

unread,
Aug 17, 2012, 1:12:13 PM8/17/12
to redi...@googlegroups.com
Hello.
I use a watch to monitor the number of id in the key. That is simply adding to the key id, but if it has more than 30 of the id, then I remove and add a new one.
WATCH(post);
GET post;
......
MULTI()
.........
EXEC();
 But the problem is that this key can not be, if this key is not that redis will listen to him? And another question is how do I cancel wiretap a particular key, and not all the keys?

Елена Исаева

unread,
Aug 17, 2012, 1:56:26 PM8/17/12
to redi...@googlegroups.com
I found another solution, that if they prescribe the id in the list. And cut it ... But then a bunch of small lists as the maximum of the elements in it will be 15. And also have to make additional inquiry to this list, when viewing the recording. How better to do? Add to the list and cut it to 15 elements, or by using WATCH / MULTI

пятница, 17 августа 2012 г., 21:12:13 UTC+4 пользователь Елена Исаева написал:

Didier Spezia

unread,
Aug 17, 2012, 2:14:05 PM8/17/12
to redi...@googlegroups.com

>> But the problem is that this key can not be, if this key is not that redis will listen to him?

If I understand correctly (this is difficult), the question is whether Redis can watch for a non existing key.
The answer is yes. If a non existing key is watched and the key is added by an other connection,
the MULTI/EXEC block will be cancelled.

>> And another question is how do I cancel wiretap a particular key, and not all the keys?

I don't think it is possible.
The DISCARD and UNWATCH apply to all the watched keys.

>> How better to do? Add to the list and cut it to 15 elements, or by using WATCH / MULTI

The former.
Pipeline the following:

MULTI
LPUSH mylist myval
LTRIM mylist 0 14
EXEC

Regards,
Didier.

Елена Исаева

unread,
Aug 17, 2012, 2:17:34 PM8/17/12
to redi...@googlegroups.com
And another small question, what will happen if and execute commands LPUSH LTRIM MULTI without a say in the PIPELINE?

пятница, 17 августа 2012 г., 22:14:05 UTC+4 пользователь Didier Spezia написал:

Didier Spezia

unread,
Aug 17, 2012, 2:22:21 PM8/17/12
to redi...@googlegroups.com

>> And another small question, what will happen if and execute commands LPUSH LTRIM MULTI without a say in the PIPELINE?

You will generate 4 roundtrips to the server instead of 1.
The "transaction" will be enforced though (i.e. isolation is guaranteed) and executed atomically.

Regards,
Didier.

Елена Исаева

unread,
Aug 17, 2012, 10:27:54 PM8/17/12
to redi...@googlegroups.com
Hello
Following the example of creating a clone of Twitter, I send my tweet to your followers. That is, each user a unique list of tweets. But I need to send to update a row in a list. But unfortunately that command redis not. As an alternative to replace the lists uporyadochnomi sets (zset). But then have trouble with these limited sets. As well as significantly worse in the tape recording speed users, really bad. So I decided to add functionality redis'a adding a function that can replace the need for me to place in the list. But for this feature to work WATCH.
0. Amendments to keep track list (WATCH)
1. Opt for the entire list with LRANGE
2. Seeking need me a line, and calculate its index
3. Change this line by index (LSET)
But the problem is that the right lines may not be in Speke, because it is constantly cut to length. And if the desired line of found will not be the redis Amendments continue to monitor this list, that is not necessary.
But as you said that UNWATCH, can not cancel otslezhivnie particular key. But how can that be? If you cancel the tracking of all the keys, then perhaps there are problems, such as race, etc. (because of possible concurrent requests for tracking)
Tell me what to create one?

Didier Spezia

unread,
Aug 18, 2012, 6:17:23 AM8/18/12
to redi...@googlegroups.com

>>But as you said that UNWATCH, can not cancel otslezhivnie particular key. But how can that be? If you cancel the tracking of all the keys, then perhaps there are problems, such as race, etc. >>(because of possible concurrent requests for tracking)
>>Tell me what to create one?

Okay ... my faith in automatic translation tools is getting lower and lower.

My understanding is you need to find and update a specific item in a list
in a concurrent environment (atomicity and isolation required).

You can do it almost as you describe:

WATCH mylist
LRANGE mylist 0 -1
Find item in the list -> index
MULTI
If item has been found:
    LSET mylist index new_value
    EXEC
else:
    DISCARD

... and of course the client using these commands has to check the result
of EXEC and loop if needed.

Please note the DISCARD or UNWATCH commands apply to all keys ***for a given connection***.
In practice, you cannot handle several transactions on the same connection, so the DISCARD and
UNWATCH applies to the keys involved in the last transaction. If you need to run several transactions
in parallel, then you will need several connections as well.

Another solution is to rely on Redis 2.6 and a Lua script.
Here is an example in Python:

import redis

POOL = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=POOL)
  
SCRIPT = """
local n = redis.call('LLEN', KEYS[1]) - 1
for i=0,n do
   local val = redis.call( 'LINDEX', KEYS[1], i )
   if val == ARGV[1] then
      redis.call( 'LSET',KEYS[1], i, ARGV[2] )
      return i
   end
end
return -1
"""

r.flushall()

r.rpush( "mylist", 1, 2, 3, 4, 5, 6 )
print r.lrange("mylist",0,-1)

print r.execute_command( "EVAL", SCRIPT, 1, "mylist", 4, 10 )
print r.lrange("mylist",0,-1)

print r.execute_command( "EVAL", SCRIPT, 1, "mylist", 20, 30 )
print r.lrange("mylist",0,-1)

Lua scripts being executed atomically, there is no need of WATCH/MULTI/EXEC
blocks here.

Regards,
Didier.

Елена Исаева

unread,
Aug 18, 2012, 11:31:19 AM8/18/12
to redi...@googlegroups.com
Thanks for your help!
With Lua script, I do not know, but how will the stable version redis'a 2.6 will investigate. And I apologize for the technical translation.

суббота, 18 августа 2012 г., 14:17:23 UTC+4 пользователь Didier Spezia написал:
Reply all
Reply to author
Forward
0 new messages