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(); }
static void Main(string[] args) { //1.0开启Redis的客户端,引入 using ServiceStack.Redis;命名空间 (连接服务器) using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //client.Password = "******"; //如果连接Redis的redis.windows.conf文件中的配置了访问密码,则在这设置将Password设为它的访问密码 //1.0---------------------------------- //将数据存储到了Redis服务器中 client.Set <string>("name", "zhangshang"); //获取数据 string name = client.Get <string>("name"); //删除key为蜀国的数据 client.Remove("蜀国"); //client.RemoveAllFromList("蜀国");或者移除key为蜀国的集合数据。 //2.0--------------------------------- //将数据存入到同一个key中,以list集合的方式存储。(看它如何在同一个key中添加不同的数据) client.AddItemToList("蜀国", "刘备"); client.AddItemToList("蜀国", "关羽"); client.AddItemToList("蜀国", "张飞"); client.AddItemToList("蜀国", "张飞"); //第一种获取数据的方式 (这里是获取key=“蜀国”的数据; 注意:这个key有两个值为“张飞”的数据) List <string> list = client.GetAllItemsFromList("蜀国"); list.ForEach(r => Console.WriteLine(r)); //输出:刘备 关羽 张飞 张飞 //第二种获取数据的方式 int listCount = (int)client.GetListCount("蜀国"); //获取key=“蜀国”的数据总条数 4 for (int i = 0; i < listCount; i++) { Console.WriteLine(client.GetItemFromList("蜀国", i)); } //3.0----------------------------------Set(消重) //用消重的方式,将数据存储到Redis服务器中 client.AddItemToSet("魏国", "曹操"); client.AddItemToSet("魏国", "曹操"); client.AddItemToSet("魏国", "曹植"); //获取数据 HashSet <string> ha = client.GetAllItemsFromSet("魏国"); //它返回的是一个HashSet的集合 List <string> list1 = ha.ToList(); //转成List list1.ForEach(r => Console.WriteLine(r)); //输出:曹操 曹植 (注意:因为我们写入了两个曹操,但是这里使用了Set去重,数以只输出了一个曹操) //4.0----------------------------------队列(队列的特点:先进先出) client.EnqueueItemOnList("吴国", "孙坚"); client.EnqueueItemOnList("吴国", "孙策"); client.EnqueueItemOnList("吴国", "周瑜"); int clistCount = (int)client.GetListCount("吴国"); //获取key=“吴国”的数据总条数 3 for (int i = 0; i < clistCount; i++) { //出队列(出了对列的数据项,都会被删除,所以如果一个数据项出了对列后,那么Redis里面就会被删除) Console.WriteLine(client.DequeueItemFromList("吴国")); } //为了测试出队列的数据项是否被删除,我们来做一个检测 if (client.GetAllItemsFromList("吴国").Any() == false) { Console.WriteLine("已经全部出队了,没有数据了"); } List <UserInfo> u = new List <UserInfo>() { new UserInfo() { Name = "1", Age = 1 }, new UserInfo() { Name = "2", Age = 11 }, new UserInfo() { Name = "3", Age = 12 }, }; //存储 client.Set <List <UserInfo> >("test", u); //获取 var q = client.Get <List <UserInfo> >("test"); foreach (var item in q) { Console.WriteLine(item.Name); } Console.ReadLine(); Console.ReadKey(); } }
static void SampleLogin() { string userName = ""; string password = ""; string sessionId = ""; // Define static list of users List <User> users = new List <User>() { new User { UserId = 1, UserName = "******", Password = "******", Email = "*****@*****.**" }, new User { UserId = 2, UserName = "******", Password = "******", Email = "*****@*****.**" }, new User { UserId = 3, UserName = "******", Password = "******", Email = "*****@*****.**" }, }; // Read username and password from console. Console.Write("Enter username: "******"Enter password: "******"Your user {0} have successfully login!", userName); } // Use logoff Console.WriteLine("Press enter to logoff username {0} : ", userName); var line = Console.ReadLine(); if (String.IsNullOrEmpty(line)) { Logout(sessionId); Environment.Exit(0); } } else { //User enter incorrect username/password, try login again... Console.WriteLine("Login failed! Invalid username/password."); SampleLogin(); } }