Transactional Create with Validation in ServiceSta

2019-07-15 05:45发布

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.

2条回答
手持菜刀,她持情操
2楼-- · 2019-07-15 06:02

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

查看更多
祖国的老花朵
3楼-- · 2019-07-15 06:19

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();
}
查看更多
登录 后发表回答