How to make a basic WATCH using StackExchange.Redis

I would like to make a base clock using StackExchange.Redis. Where, if the key is changed during a transaction, it fails.

StackExchange.Redis reacted very well to the "Condition" api, which supports the concepts of "Equal" and "Existing".

This is really nice, but I would just like to do something like "No change." I could have missed something, but at this point it is not clear to me how to do this.

Is it possible to do something like:

var transaction = redis.CreateTransaction();
transaction.AddCondition(Condition.StringUnchanged("key")); //the API here could maybe be simplified
var val = transaction.StringGet("key"); //notably, this is not async because you would have to get the result immediately - it would only work on watched keys
transaction.StringSetAsync("key", val + 1);
transaction.Execute();

Or even a possible better version (which will do the same):

var transaction = redis.CreateTransaction();
var val = transaction.Watch("key"); //this would return the value!
transaction.StringSetAsync("key", val + 1);
transaction.Execute();

Currently, the only way to figure this out is to do something like:

var val = redis.StringGet("key");
var transaction = redis.CreateTransaction();
transaction.AddCondition(Condition.StringEqual("key", val));
transaction.StringSetAsync("key", val + 1);
transaction.Execute();

Which of the attempts to read the SE.Redis code I understand to translate something like (not sure how accurate it is):

val = GET key
WATCH key
MULTI
val = val + 1
SET key $val
checkVal = GET key
(then if checkVal != val:) UNWATCH
(otherwise:) EXEC

Redis, , . , - ?:

WATCH key
MULTI
val = GET key
val = val + 1
SET key $val
EXEC

, SE.Redis?

+2
1

WATCH - , SE.Redis . , .

, "" , - - . , , . ?


; () redis - SE.Redis; GET a MULTI, EXEC, SET: .

, ( , SE.Redis):

WATCH key
val = GET key
MULTI
val = val + 1
SET key $val
EXEC

WATCH: , , , {key} (, , , ). , WATCH , SE.Redis , , , . ; , -. . .

0

Source: https://habr.com/ru/post/1625249/


All Articles