/// <summary> /// 从Redis取Session /// </summary> /// <typeparam name="T"></typeparam> /// <param name="_"></param> /// <param name="key">键</param> /// <param name="expire">过期时间,默认20分钟</param> /// <returns></returns> public static T GetByRedis <T>(this HttpSessionState _, string key, int expire = 20) where T : class { if (HttpContext.Current is null) { throw new Exception("请确保此方法调用是在同步线程中执行!"); } var sessionKey = HttpContext.Current.Request.Cookies["SessionID"]?.Value; if (string.IsNullOrEmpty(sessionKey)) { return(default(T)); } T obj = _.Get <T>(key); try { sessionKey = "Session:" + sessionKey; using var redisHelper = RedisHelper.GetInstance(1); if (redisHelper.KeyExists(sessionKey) && redisHelper.HashExists(sessionKey, key)) { redisHelper.Expire(sessionKey, TimeSpan.FromMinutes(expire)); return(redisHelper.GetHash <T>(sessionKey, key)); } return(default(T)); } catch { return(default(T)); } }
public static T Ensure <T>(this HttpSessionState state, string key) where T : class, new() { var value = state.Get <T>(key); if (value == null) { value = new T(); state.Set(key, value); } return(value); }
public static T Get <T>(this HttpSessionState state, string key) { return(state.Get <T>(key, default(T))); }
public static ENLogin GetCurrentUser(this HttpSessionState Session) { var user = (ENLogin)Session.Get(GlobalKey.CurrentUser); return(user); }