我学习Redis的第二步便是用C#实现对Redis数据库的操作,开始完全不知道怎么做,于是网上找了一段代码,测试了一下发现完全可以,因此把代码贴出来。供大家学习:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using ServiceStack.Redis; 6 using MyRedisSharpStudy.Models; 7 using ServiceStack.Redis.Support; 8 9 namespace MyRedisSharpStudy 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 var Redis = new RedisClient("127.0.0.1", 6379);//redis服务IP和端口 16 #region insert 17 var storeMembers = new List<string> { "jj", "lihui", "cc" }; 18 storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x)); 19 //注:也可直接使用AddRangeToList方法将一组数据装入如: 20 //Redis.AddRangeToList("additemtolist", storeMembers); 21 #endregion 22 #region get 23 var members = Redis.GetAllItemsFromList("additemtolist"); 24 members.ForEach(s => Console.WriteLine("additemtolist :" + s)); 25 Console.WriteLine(Redis.GetItemFromList("additemtolist", 2)); 26 #endregion 27 #region delete 28 //var list = Redis.Lists["additemtolist"]; 29 //list.Clear();//清空 30 //list.Remove("two");//移除指定键值 31 //list.RemoveAt(2);//移除指定索引位置数据 32 #endregion 33 #region object 34 //Redis.Set("userinfo", new UserModel() { UserName = "李四", Age = 45 }); 35 //UserModel userinfo = Redis.Get<UserModel>("userinfo"); 36 //Console.WriteLine(userinfo.UserName); 37 //Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12); 38 //Console.WriteLine(Redis.Get<int>("my_age")); 39 #endregion 40 var ser = new ObjectSerializer(); //位于namespace ServiceStack.Redis.Support; 41 #region 序列化 42 //bool result = Redis.Set<byte[]>("userinfo", ser.Serialize(new UserModel() { UserName = "张三", Age = 12 })); 43 //UserModel userinfo = ser.Deserialize(Redis.Get<byte[]>("userinfo")) as UserModel; 44 //Console.WriteLine(userinfo.UserName); 45 #endregion 46 //也支持列表 47 //Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList)); 48 #region 负载均衡 49 PooledRedisClientManager prcm = CreateManager(new string[] { "10.0.4.210:6379" }, new string[] { "10.0.4.210:6379" }); 50 List<UserModel> userinfoList = new List<UserModel>(); 51 userinfoList.Add(new UserModel() { UserName = "pool_daizhj", Age = 1 }); 52 userinfoList.Add(new UserModel() { UserName = "pool_daizhj1", Age = 2 }); 53 // 从池中获取一个链接: 54 using (IRedisClient Redis2 = prcm.GetClient()) 55 { 56 Redis2.Set("userinfolist", userinfoList); 57 List<UserModel> userList = Redis2.Get<List<UserModel>>("userinfolist"); 58 } 59 #endregion 60 Console.ReadLine(); 61 } 62 63 public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts) 64 { 65 //支持读写分离,均衡负载 66 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig 67 { 68 MaxWritePoolSize = 5,//“写”链接池链接数 69 MaxReadPoolSize = 5,//“写”链接池链接数 70 AutoStart = true, 71 }); 72 } 73 } 74 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyRedisSharpStudy.Models 7 { 8 public class UserModel 9 { 10 private long _id; 11 /// <summary> 12 /// 用户ID 13 /// </summary> 14 public long Id 15 { 16 get { return _id; } 17 set { _id = value; } 18 } 19 20 private string _userName; 21 /// <summary> 22 /// 用户名 23 /// </summary> 24 public string UserName 25 { 26 get { return _userName; } 27 set { _userName = value; } 28 } 29 30 private List<long> _blogsIDs; 31 /// <summary> 32 /// blogID 33 /// </summary> 34 public List<long> BlogsIDs 35 { 36 get { return _blogsIDs; } 37 set { _blogsIDs = value; } 38 } 39 40 private int _age; 41 /// <summary> 42 /// 年龄 43 /// </summary> 44 public int Age 45 { 46 get { return _age; } 47 set { _age = value; } 48 } 49 } 50 }
因为这不是我学习的重点,所以只是随便找了一个代码,知道在c#中该怎么用就行了。需要注意的是,在这里需要添加几个引用,如图:
这些DLL的下载地址是:http://servicestack.googlecode.com/files/ServiceStack-v1.63.zip
下一步是学习怎样把redis和其它的数据联合起来使用。