StackExchange.Redis 实现SetNx
今天有同事指出如何在StackExchange.Redis 实现Redis的原生命令。
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(options); var db=redis.GetDatabase(); bool flag = db.StringSet("InvQty222", "111", TimeSpan.FromSeconds(900), When.NotExists, CommandFlags.None); Console.WriteLine(flag); Console.ReadKey(); }
部分源码如下:
if (expiry == null || expiry.Value == TimeSpan.MaxValue) { // no expiry switch (when) { case When.Always: return Message.Create(Database, flags, RedisCommand.SET, key, value); case When.NotExists: return Message.Create(Database, flags, RedisCommand.SETNX, key, value); case When.Exists: return Message.Create(Database, flags, RedisCommand.SET, key, value, RedisLiterals.XX); } }
终极目标:世界大同