• C# redis简单的使用


    1、项目一:用于在Redis中添加数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ServiceStack.Redis;
    
    
    namespace RedisTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var client = new RedisClient("172.16.1.114", 6379);
    
                //client.Set<string>("name", "老王");
    
                //string value = client.Get<string>("name");
    
                //Console.WriteLine(value);
    
             
                //string redis = client.Get<string>("redis");
    
                //Console.WriteLine(redis);
    
    
                //UserInfo userinfo = client.Get<UserInfo>("userinfo");
    
                //string info = userinfo.ToString();
                //Console.WriteLine(info);
    
    
                List<UserInfo> list = new List<UserInfo>();
    
                Random r = new Random();
    
                char[] sexs = new []{'',''};
    
                for (int i = 1; i < 101; i++)
                {
                    list.Add(new UserInfo() { Id = i, UserName = "张三" + r.Next(1, 100), Gender = sexs[r.Next(0, 2)], Age = r.Next(20, 100) });
                }
    
                client.Add<List<UserInfo>>("userInfoList",list);
    
                Console.WriteLine("ok");
    
                Console.ReadKey();
    
    
            }
        }
    
    
        public class UserInfo
        {
            public UserInfo()
            {
                //
                //TODO: 在此处添加构造函数逻辑
                //
            }
    
    
            public int Id { get; set; }
    
            public string UserName { get; set; }
    
            public char Gender { get; set; }
    
            public int Age { get; set; }
    
    
            public string ToString()
            {
                return "Id=" + Id + ",UserName=" + UserName + ",Gender=" + Gender + ",Age=" + Age;
            }
        }
    }

    2、项目二:用于访问项目一Redis中的数据

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using ServiceStack.Redis;
    using System.Collections.Generic;
    
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                var client = new RedisClient("172.16.1.114", 6379);
    
    
                List<UserInfo> list = client.Get<List<UserInfo>>("userInfoList");
    
                foreach (var item in list)
                {
                    Response.Write(item.ToString());
                    Response.Write("<br />");
                }
    
                //UserInfo userInfo = new UserInfo() { Id = 111, UserName = "李四", Age = 45, Gender = '男' };
    
                //var isAdd = client.Add<UserInfo>("userinfo", userInfo);
    
                //Response.Write("是否添加成功:" + isAdd);
    
                //Response.Write("<br />");
    
                //UserInfo userinfo = client.Get<UserInfo>("userinfo");
    
                //string info = userinfo.ToString();
    
                //Response.Write(info);
    
    
                //Response.Write("<br />");
    
    
                //client.Add<string>("redis", "redis");
    
                //string redis = client.Get<string>("redis");
    
    
                //Response.Write(redis);
    
    
            }
        }
    }
  • 相关阅读:
    文件下载(Servlet/Struts2)
    Spring学习(一)---依赖注入和控制反转
    MyBatis学习(三)---MyBatis和Spring整合
    MyBatis学习(二)---数据表之间关联
    MyBatis学习(一)---配置文件,Mapper接口和动态SQL
    转载:常见端口介绍
    CentOS7 yum提示:another app is currently holding the yum lock;waiting for it to exit
    批量删除文件,只保留目录下最新的几个文件,其他均删除
    转载:SQL Server 如何设置数据库的默认初始大小和自动增长大小
    阿里云ECS使用秘钥或者密码登录
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/4072936.html
Copyright © 2020-2023  润新知