public async Task<long?> ManualIncr(RedisConnection connection, int db, string key)
        {
            var oldVal = await connection.Strings.GetInt64(db, key);
            var newVal = (oldVal ?? 0) + 1;
            using (var tran = connection.CreateTransaction())
            { // check hasn't changed

#pragma warning disable 4014
                tran.AddCondition(Condition.KeyEquals(db, key, oldVal));
                tran.Strings.Set(db, key, newVal);
#pragma warning restore 4014
                if (!await tran.Execute()) return null; // aborted
                return newVal;
            }    
        }
        public void TestMethod1()
        {
            using (RedisConnection connection = new RedisConnection("localhost"))
            {
                connection.Open();

                RedisTransaction transaction = connection.CreateTransaction();
                string key = DateTime.Now.ToString();
                transaction.Lists.AddFirst(1, key, "pankaj", createIfMissing: true);
                transaction.Execute();

                connection.Keys.Exists(1, key);

            }
        }
Example #3
0
        static async Task<int> Send(RedisConnection conn, string idKey, int db, string channel, string data)
        {
            int attempts = 0;
            bool success;
            do
            {
                var oldId = await conn.Strings.GetInt64(db, idKey).SafeAwaitable().ConfigureAwait(false); // important: let this be nullable;
                // means "doesn't exist"
                var newId = (oldId ?? 0) + 1;
                var payload = Pack(newId, data);

                using (var tran = conn.CreateTransaction())
                {
                    var x0 = tran.AddCondition(Condition.KeyEquals(db, idKey, oldId)).SafeAwaitable();
                    var x1 = tran.Strings.Increment(db, idKey).SafeAwaitable();
                    var x2 = tran.Publish(channel, payload).SafeAwaitable();
                    success = await tran.Execute().SafeAwaitable().ConfigureAwait(false);

                    if (success)
                    {
                        // still expect all of these to get answers
                        await Task.WhenAll(x0, x1, x2);

                        Assert.IsTrue(x0.Result, "condition passed");
                        Assert.AreEqual(newId, x1.Result);
                    }
                    else
                    {
                        // can't say much about x0; could have got past that
                        Assert.IsTrue(await IsCancelled(x1));
                        Assert.IsTrue(await IsCancelled(x2));
                    }

                    attempts++;
                }
            } while (!success);
            return attempts;
        }