C#操作REDIS例子
windows SEVER包:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload
windows仅用来测试,性能不如在linux下高.
c#使用:
引用的ServiceStackRedis:https://github.com/mythz/ServiceStack.Redis
- static void Main(string[] args)
- {
- var Redis = new RedisClient("127.0.0.1", 6379);//redis服务IP和端口
- #region =insert=
- //var storeMembers = new List<string> { "jj", "lihui", "cc" };
- //storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x));
- //注:也可直接使用AddRangeToList方法将一组数据装入如:
- //Redis.AddRangeToList("additemtolist", storeMembers);
- #endregion
- #region =get=
- //var members = Redis.GetAllItemsFromList("additemtolist");
- //members.ForEach(s => Console.WriteLine("additemtolist :" + s));
- //Console.WriteLine(Redis.GetItemFromList("additemtolist", 2));
- #endregion
- #region =delete=
- //var list = Redis.Lists["additemtolist"];
- //list.Clear();//清空
- //list.Remove("two");//移除指定键值
- //list.RemoveAt(2);//移除指定索引位置数据
- #endregion
- #region =object=
- //Redis.Set("userinfo", new UserInfo() { UserName = "李四", Age = 45 });
- //UserInfo userinfo = Redis.Get<UserInfo>("userinfo");
- //Console.WriteLine(userinfo.UserName);
- //Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);
- //Console.WriteLine(Redis.Get<int>("my_age"));
- #endregion
- var ser = new ObjectSerializer(); //位于namespace ServiceStack.Redis.Support;
- #region =序列化=
- //bool result = Redis.Set<byte[]>("userinfo", ser.Serialize(new UserInfo() { UserName = "张三", Age = 12 }));
- //UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>("userinfo")) as UserInfo;
- //Console.WriteLine(userinfo.UserName);
- #endregion
- //也支持列表
- //Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));
- #region =负载均衡=
- PooledRedisClientManager prcm = CreateManager(new string[] { "10.0.4.210:6379" }, new string[] { "10.0.4.210:6379" });
- List<UserInfo> userinfoList = new List<UserInfo>();
- userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 });
- userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 });
- // 从池中获取一个链接:
- using (IRedisClient Redis2 = prcm.GetClient())
- {
- Redis2.Set("userinfolist", userinfoList);
- List<UserInfo> userList = Redis2.Get<List<UserInfo>>("userinfolist");
- }
- #endregion
- Console.ReadLine();
- }
- public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
- {
- //支持读写分离,均衡负载
- return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
- {
- MaxWritePoolSize = 5,//“写”链接池链接数
- MaxReadPoolSize = 5,//“写”链接池链接数
- AutoStart = true,
- });
- }
- static void Main(string[] args)
- {
- var Redis = new RedisClient("127.0.0.1", 6379);//redis服务IP和端口
- #region =insert=
- //var storeMembers = new List<string> { "jj", "lihui", "cc" };
- //storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x));
- //注:也可直接使用AddRangeToList方法将一组数据装入如:
- //Redis.AddRangeToList("additemtolist", storeMembers);
- #endregion
- #region =get=
- //var members = Redis.GetAllItemsFromList("additemtolist");
- //members.ForEach(s => Console.WriteLine("additemtolist :" + s));
- //Console.WriteLine(Redis.GetItemFromList("additemtolist", 2));
- #endregion
- #region =delete=
- //var list = Redis.Lists["additemtolist"];
- //list.Clear();//清空
- //list.Remove("two");//移除指定键值
- //list.RemoveAt(2);//移除指定索引位置数据
- #endregion
- #region =object=
- //Redis.Set("userinfo", new UserInfo() { UserName = "李四", Age = 45 });
- //UserInfo userinfo = Redis.Get<UserInfo>("userinfo");
- //Console.WriteLine(userinfo.UserName);
- //Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);
- //Console.WriteLine(Redis.Get<int>("my_age"));
- #endregion
- var ser = new ObjectSerializer(); //位于namespace ServiceStack.Redis.Support;
- #region =序列化=
- //bool result = Redis.Set<byte[]>("userinfo", ser.Serialize(new UserInfo() { UserName = "张三", Age = 12 }));
- //UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>("userinfo")) as UserInfo;
- //Console.WriteLine(userinfo.UserName);
- #endregion
- //也支持列表
- //Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));
- #region =负载均衡=
- PooledRedisClientManager prcm = CreateManager(new string[] { "10.0.4.210:6379" }, new string[] { "10.0.4.210:6379" });
- List<UserInfo> userinfoList = new List<UserInfo>();
- userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 });
- userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 });
- // 从池中获取一个链接:
- using (IRedisClient Redis2 = prcm.GetClient())
- {
- Redis2.Set("userinfolist", userinfoList);
- List<UserInfo> userList = Redis2.Get<List<UserInfo>>("userinfolist");
- }
- #endregion
- Console.ReadLine();
- }
- public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
- {
- //支持读写分离,均衡负载
- return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
- {
- MaxWritePoolSize = 5,//“写”链接池链接数
- MaxReadPoolSize = 5,//“写”链接池链接数
- AutoStart = true,
- });
- }
从网上收集而来,仅作自己资料记录.
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · 一步一步使用mycat(四):Redis数据库缓存
- · 利用消息中间件和缓存实现简单的秒杀系统(一):理论
- · 如何处理短生命期数据
- · python删除redis所有数据
- · ubuntu安装配置redis
- · window下安装和配置Redis
- · python使用redis示例
- · Redis教程2
- · Redis教程1
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题