Transactional Create with Validation in ServiceStack Redis Client

别说谁变了你拦得住时间么 提交于 2019-12-11 00:57:23

问题


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



来源:https://stackoverflow.com/questions/10843311/transactional-create-with-validation-in-servicestack-redis-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!