• .NET使用Redis基本操作实战


    需要的DLL

    using ServiceStack.Common.Extensions;
    using ServiceStack.Redis;
    using ServiceStack.Redis.Generic;
    using ServiceStack.Text;
    using ServiceStack.Redis.Support;

    声明一个客户端对象:

    protected RedisClient Redis = new RedisClient("127.0.0.1", 6379);//redis服务IP和端口

    一 .基本KEY/VALUE键值对操作:

    1. 添加/获取: 

    List<string> storeMembers =new List<string>();
    storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x));
    注:也可直接使用AddRangeToList方法将一组数据装入如:
    
    Redis.AddRangeToList("addarrangetolist", storeMembers);

    2. 获取数据:

    var members = Redis.GetAllItemsFromList("additemtolist");
    members.ForEach(s => Response.Write("<br/>additemtolist :"+ s));

    3. 获取指定索引位置数据:

    var item = Redis.GetItemFromList("addarrangetolist", 2);

    4. 移除:

    var list = Redis.Lists["addarrangetolist"];
    list.Clear();//清空
    list.Remove("two");//移除指定键值
    list.RemoveAt(2);//移除指定索引位置数据

    二.存储对象:

    publicclass UserInfo
    {
    publiclong Id { set; get; }
    publicstring UserName { get; set; }
    publicint Age { get; set; }
    }

    1.通常方式(底层使用json序列化):

    Redis.Set<UserInfo>("userinfo", new UserInfo() { UserName ="李四", Age =45 });
    UserInfo userinfo = Redis.Get<UserInfo>("userinfo");
    
    //注:当然上面方式也适合于基本类型,如:
    
    Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);
    int age = Redis.Get<int>("my_age");

    2.object序列化方式存储:

    var ser =new ObjectSerializer(); //位于namespace ServiceStack.Redis.Support;
    bool result = Redis.Set<byte[]>("userinfo", ser.Serialize(new UserInfo() { UserName ="张三", Age =12 }));
    UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>("userinfo")) as UserInfo;
    //也支持列表
    Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));
    List<UserInfo> userList = ser.Deserialize(Redis.Get<byte[]>("userinfolist_serialize")) as List<UserInfo>;

    三.存储表格对象,比如:

    using (var redisUsers = Redis.GetTypedClient<UserInfo>())
    {
    redisUsers.Store(new UserInfo { Id = redisUsers.GetNextSequence(), UserName ="daizhj", Age =12 });
    redisUsers.Store(new UserInfo { Id = redisUsers.GetNextSequence(), UserName ="daizhenjun", Age =13 });
    
    var allUsers = redisUsers.GetAll();//就像操作ado对象一样,可以进行CRUD等操作
    allUsers.ForEach(s => Response.Write("<br/>user :"+ s.UserName +" age:"+ s.Age));
    }

    四.使用客户端链接池模式提升链接速度:

    publicstatic PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
    {
    //支持读写分离,均衡负载
    returnnew PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
    {
    MaxWritePoolSize =5,//“写”链接池链接数
    MaxReadPoolSize =5,//“写”链接池链接数
    AutoStart =true,
    }); 
    }

    声明链接池对象(这里只使用一个redis服务端):

    PooledRedisClientManager prcm = CreateManager(newstring[] { "127.0.0.1:6379" }, newstring[] { "127.0.0.1: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 Redis = prcm.GetClient())
    { 
    Redis.Set("userinfolist", userinfoList);
    List<UserInfo> userList = Redis.Get<List<UserInfo>>("userinfolist");
    }
  • 相关阅读:
    初识你Swift【上篇】
    初识你Swift【下篇】
    单元测试基础
    时间都去哪了?
    iOS App上线的秘密
    mysql系列——DQL常见操作汇总(四)
    Get和Post请求有什么区别?
    SpringBoot2+WebSocket之聊天应用实战
    OCR识别
    mysql系列——子查询(非常重要)(八)
  • 原文地址:https://www.cnblogs.com/wd0730/p/3112748.html
Copyright © 2020-2023  润新知