前面几篇分享了nosql只mongodb,今天简单分享另一个nosql神兵redis。

  主要模仿sso单点登录,将登录人信息写入redis。话不多说,直接上马,驾。

     /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public string Login(string name, string pass)
        {
            //******
            //name,pass登陆成功(^_^)
            //******
            //生成tokan
            var token = "tianshangbaiyujing";//Guid.NewGuid().ToString();
            //*****获取user信息
            var user = new User();
            user.Name = "白玉京";
            user.Sex = 0;
            user.Age = 20;
            user.Address = "杭州";
            //*****
            //*****将登录人信息信息redis缓存
            HashEntry[] entrys = new HashEntry[4];//数组对象长度一定要和下面SetValue赋值一一对应
            HashEntry entry = new HashEntry("Name", user.Name);
            entrys.SetValue(entry, 0);
            entry = new HashEntry("Sex", user.Sex);
            entrys.SetValue(entry, 1);
            entry = new HashEntry("Age", user.Age);
            entrys.SetValue(entry, 2);
            entry = new HashEntry("Address", user.Address);
            entrys.SetValue(entry, 3);
            TimeSpan ts = new TimeSpan(864000000000);//默认1天
            RedisHelper.AddHashKey(token, entrys, ts);
            //*****
            //*****将user信息存入userSession对象返回给前端
            var session = new UserSession() { Token = token, User = user };
            //*****
            return JsonConvert.SerializeObject(session);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public string TestUserToken(string token)
        {
            var arr = RedisHelper.GetHashALL(token);
            User user = new User();
            user.Name = arr[0].Value;
            user.Sex = (int)arr[1].Value;
            user.Age = (int)arr[2].Value;
            user.Address = arr[3].Value;
            return JsonConvert.SerializeObject(user);
        }
        /// <summary>
        /// UserSession
        /// </summary>
        public class UserSession
        {
            public string Token { get; set; }
            public User User { get; set; }
        }
        /// <summary>
        /// User
        /// </summary>
        public class User
        {
            /// <summary>
            /// 姓名
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 性别0:男,1:女
            /// </summary>
            public int Sex { get; set; }
            /// <summary>
            /// 年龄
            /// </summary>
            public int Age { get; set; }
            /// <summary>
            /// 地址
            /// </summary>
            public string Address { get; set; }
        }

  redisHelper类。

      static readonly ConnectionMultiplexer Redis = ConnectionMultiplexer.Connect("127.0.0.1:6379");
        static readonly IDatabase Db = Redis.GetDatabase();

       /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="exp"></param>
        public static void AddHashKey(string key, HashEntry[] hashField, TimeSpan? exp = null)
        {
            Db.HashSet(key, hashField);
            Db.KeyExpire(key, exp);
        }
       /// <summary>
        /// 获取hash表
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static HashEntry[] GetHashALL(string key)
        {
            return Db.HashGetAll(key);
        }
 

  两个方法运行结果

  

  写了半天,贴两分钟完事了,再贴几张redisServer启动吧,都是cmd命令,跟mongo很像,就不细说了。

  启动客户端看下刚才存的数据。

  这样登录完成后,每次请求接口带上token就可以获取登录信息了。