static void RedisTransactions()
 {
     using (IRedisClient client = new RedisClient())
     {
         var transaction = client.CreateTransaction();
         transaction.QueueCommand(c => client.Set("abc", 1));
         transaction.QueueCommand(c => client.Increment("abc", 1));
         transaction.Commit();
         var result = client.Get <int>("abc");
         Console.WriteLine(result);
     }
 }
Example #2
0
        private static void DemoRedisTransaction()
        {
            using (IRedisClient redisClient = new RedisClient())
            {
                const string      key         = "abc";
                IRedisTransaction transaction = redisClient.CreateTransaction();
                transaction.QueueCommand(c => c.Set(key, 1));
                transaction.QueueCommand(c => c.Increment(key, 1));
                transaction.Commit();

                int result = redisClient.Get <int>(key);
                Console.WriteLine($"Result = {result}");
            }
        }
Example #3
0
        /// <summary>
        /// ServiceStack.Redis
        /// Redis 有事务,但是没有回滚的操作;如果在事务过程中,有地方改了监控key的值,则整个事务会提交失败;
        /// </summary>
        public static void TransationDemo()
        {
            bool flag = false;

            using (RedisClient redisClient = new RedisClient("127.0.0.1", 6379))
            {
                redisClient.Set("a", "1");
                redisClient.Set("b", "1");
                redisClient.Set("c", "1");

                redisClient.Watch("a", "b", "c");//要把这些key放入到监控中
                using (var trans = redisClient.CreateTransaction())
                {
                    trans.QueueCommand(p => p.Set("a", "3"));//在这可以加个断点,然后用工具去改a的值为2
                    trans.QueueCommand(p => p.Set("b", "3"));
                    trans.QueueCommand(p => p.Set("c", "3"));

                    flag = trans.Commit();
                }
                Console.WriteLine($"提交结果:{flag},获取值a:{redisClient.Get<string>("a")},b:{redisClient.Get<string>("b")},c:{redisClient.Get<string>("c")}");
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            using (IRedisNativeClient client = new RedisClient())
            {
                client.Set("urn:messages:1", Encoding.UTF8.GetBytes("Hello C# World!"));
            }

            using (IRedisNativeClient client = new RedisClient())
            {
                var result = Encoding.UTF8.GetString(client.Get("urn:messages:1"));
                Console.WriteLine("Message: {0}", result);
            }

            using (IRedisClient client = new RedisClient())
            {
                var customerNames = client.Lists["urn:customernames"];
                customerNames.Clear();
                customerNames.Add("Joe");
                customerNames.Add("Mary");
                customerNames.Add("Bob");
            }

            using (IRedisClient client = new RedisClient())
            {
                var customerNames = client.Lists["urn:customernames"];
                foreach (var customerName in customerNames)
                {
                    Console.WriteLine("Customer: {0}", customerName);
                }
            }

            long lastId = 0;

            using (IRedisClient client = new RedisClient())
            {
                var customerClient = client.GetTypedClient <Customer>();
                var customer       = new Customer()
                {
                    Id      = customerClient.GetNextSequence(),
                    Address = "123 Main St",
                    Name    = "Bob Green",
                    Orders  =
                        new List <Order>
                    {
                        new Order {
                            OrderNumber = "AB123"
                        },
                        new Order {
                            OrderNumber = "AB124"
                        }
                    }
                };
                var storedCustomer = customerClient.Store(customer);
                lastId = storedCustomer.Id;
            }

            using (IRedisClient client = new RedisClient())
            {
                var customerClient = client.GetTypedClient <Customer>();
                var customer       = customerClient.GetById(lastId);
                Console.WriteLine("Got customer {0}, with name {1}", customer.Id, customer.Name);
            }

            using (IRedisClient client = new RedisClient())
            {
                var transaction = client.CreateTransaction();
                transaction.QueueCommand(c => c.Set("abc", 1));
                transaction.QueueCommand(c => c.Increment("abc", 1));
                transaction.Commit();
                var result = client.Get <int>("abc");
                Console.WriteLine(result);
            }

            using (IRedisClient client = new RedisClient())
            {
                //client.PublishMessage("debug", "Hello C#!");
                var sub = client.CreateSubscription();
                sub.OnMessage = (c, m) => Console.WriteLine("Got message: {0}, from channel {1}", m, c);
                sub.SubscribeToChannels("news");
            }

            Console.ReadLine();
        }