Transactional Create with Validation in ServiceSta

2019-07-15 05:59发布

问题:

User has DisplayName and it is unique for Users.

I want to Create User but firstly I have to check display name (DisplayName could not be duplicated for Users)

I've checked ServiceStack examples and I could not see Transactional Insert/Update with validation check.

How can I perform it. I dont want to write "Validation Tasks" for redis db. I dont want inconsistency in db.

回答1:

The ServiceStack.Redis client does have support for Redis's WATCH and transactions where these Redis commands:

WATCH mykey
test = EXIST mykey
MULTI
SET mykey $val
EXEC

Can be accomplished with:

var redis = new RedisClient();
redis.Watch("mykey");
if (!redis.ContainsKey("mykey")) return;

using (var trans = redis.CreateTransaction()) {
    trans.QueueCommand(r => r.Set("mykey", "val"));
    trans.Commit();
}


回答2:

Is possible to perform redis transactions. More information here

WATCH mykey
test = EXIST mykey
MULTI
SET mykey $val
EXEC

Using PHP have um better example: here