예제 #1
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();
        }
예제 #2
0
        static void Main(string[] args)
        {
            var loggerDebugFactory = new LoggerFactory();

            loggerDebugFactory.AddNLog(new NLogProviderOptions {
                CaptureMessageTemplates = true, CaptureMessageProperties = true
            });
            loggerDebugFactory.ConfigureNLog("nlog.config");
            ILogger <Program> _logger = loggerDebugFactory.CreateLogger <Program>();

            _logger.LogDebug(20, "Doing hard work! {Action}", "abc");
            var a = 10;
            var b = 0;

            try
            {
                a = a / b;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "this is a test");
            }

            var messagesReceived = 0;

            using (var client = new RedisClient("localhost", 6379, "12345^"))
            {
                // redis type string.
                client.Set("student1", new Student
                {
                    Id          = 1,
                    Name        = "Nghiep",
                    CreatedDate = DateTimeOffset.Now
                });
                var student1 = client.Get <Student>("student1");
                // redis type hash.

                client.StoreAsHash <Student>(new Student
                {
                    Id          = 4,
                    Name        = "Nghiep",
                    CreatedDate = DateTimeOffset.Now
                });

                var fromHash = client.GetFromHash <Student>(3);
                if (fromHash.Id != 0)
                {
                    //Exited in cache
                }

                // redis type list.
                var redis      = client.As <Student>();
                var studentKey = string.Format("student:id:{0}", 1);
                var student    = new Student {
                    Id = 1, Name = "Nghiep", CreatedDate = DateTimeOffset.Now
                };

                redis.SetValue(studentKey, student);
                studentKey = string.Format("student:id:{0}", 2);
                student    = new Student {
                    Id = 2, Name = "Nghiep", CreatedDate = DateTimeOffset.Now
                };
                redis.SetValue(studentKey, student);
                var obj = redis.GetValues(new System.Collections.Generic.List <string> {
                    "student:id:1", "student:id:2"
                });



                using (var subscription = client.CreateSubscription())
                {
                    subscription.OnSubscribe = channel =>
                    {
                        _logger.LogDebug(string.Format("Subscribed to '{0}'", channel));
                    };
                    subscription.OnUnSubscribe = channel =>
                    {
                        _logger.LogDebug(string.Format("UnSubscribed from '{0}'", channel));
                    };
                    subscription.OnMessage = (channel, msg) =>
                    {
                        _logger.LogDebug(string.Format("Received '{0}' from channel '{1}'", msg, channel));

                        //As soon as we've received all 5 messages, disconnect by unsubscribing to all channels
                        if (++messagesReceived == PublishMessageCount)
                        {
                            subscription.UnSubscribeFromAllChannels();
                        }
                    };

                    ThreadPool.QueueUserWorkItem(x =>
                    {
                        Thread.Sleep(200);
                        _logger.LogDebug("Begin publishing messages...");

                        using (var redisPublisher = new RedisClient("localhost", 6379, "12345^"))
                        {
                            for (var i = 1; i <= PublishMessageCount; i++)
                            {
                                var message = MessagePrefix + i;
                                _logger.LogDebug(string.Format("Publishing '{0}' to '{1}'", message, ChannelName));
                                redisPublisher.PublishMessage(ChannelName, message);
                                Thread.Sleep(2000);
                            }
                        }
                    });

                    _logger.LogDebug(string.Format("Started Listening On '{0}'", ChannelName));
                    subscription.SubscribeToChannels(ChannelName); //blocking
                }
            }
            Console.ReadLine();
        }