ServiceStack Redis 客户端中带有验证的事务性创建 [英] Transactional Create with Validation in ServiceStack Redis Client
问题描述
User 有 DisplayName,它对用户来说是唯一的.
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)
我检查了 ServiceStack 示例,但看不到带有验证检查的事务性插入/更新.
I've checked ServiceStack examples and I could not see Transactional Insert/Update with validation check.
我该如何执行.我不想为 redis db 编写验证任务".我不想在 db 中出现不一致.
How can I perform it. I dont want to write "Validation Tasks" for redis db. I dont want inconsistency in db.
推荐答案
ServiceStack.Redis 客户端确实支持 Redis 的 WATCH 和 transactions 其中这些 Redis 命令:
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();
}
这篇关于ServiceStack Redis 客户端中带有验证的事务性创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!