using Edu.CacheManager.Base;
using Edu.Common.Enum;
using Edu.Model.CacheModel;
using Edu.Repository.User;
using System;
using System.Linq;
namespace Edu.Cache.User
{
///
/// redis缓存
///
public class AppStudentReidsCache
{
///
/// 使用redis第几号库
///
public static readonly int REDIS_DB3 = 3;
static readonly RedisHelper redis = new RedisHelper(REDIS_DB3);
///
/// 设置缓存
///
///
public static void UserInfoSet(string cacheKey, AppStudentInfo model, int JwtExpirTime)
{
try
{
TimeSpan ts = GetExpirTime(JwtExpirTime);
redis.StringSet(cacheKey, model, ts);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "UserInfoSet缓存设置失败");
}
}
///
/// 获取缓存时长
///
///
///
private static TimeSpan GetExpirTime(int JwtExpirTime)
{
DateTime dt = DateTime.Now;
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt.AddSeconds(JwtExpirTime) - dt2;
return ts;
}
///
/// 判断key是否存在
///
///
///
public static bool Exists(string cacheKey)
{
return redis.KeyExists(cacheKey);
}
///
/// 设置缓存
///
///
///
///
public static void Set(string cacheKey, object Data, int JwtExpirTime)
{
try
{
TimeSpan ts = GetExpirTime(JwtExpirTime);
redis.StringSet(cacheKey, Data, ts);
}
catch (Exception)
{
}
}
///
/// 账号仓储层对象
///
private static readonly RB_AccountRepository accountRepository = new RB_AccountRepository();
///
/// 获取用户登录信息
///
/// 账号Id
/// 请求来源
///
public static AppStudentInfo GetUserLoginInfo(object Id, ApiRequestFromEnum apiRequestFromEnum = ApiRequestFromEnum.AppStudent)
{
AppStudentInfo userInfo = null;
if (Id != null)
{
string cacheKey = Cache.CacheKey.AppStudent_Login_Key + Id.ToString();
try
{
userInfo = redis.StringGet(cacheKey);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "GetUserLoginInfo");
}
if (userInfo == null)
{
Int32.TryParse(Id.ToString(), out int NewId);
if (NewId > 0)
{
string token = "";
var model = accountRepository.GetAccountListExtRepository(new Model.ViewModel.User.RB_Account_ViewModel()
{
Id = NewId
})?.FirstOrDefault();
if (model != null)
{
userInfo = new AppStudentInfo
{
Id = model.Id,
Group_Id = model.Group_Id,
School_Id = model.School_Id,
AccountName = model.AccountName,
GroupName = model.GroupName,
SchoolName = model.SchoolName,
Token = token,
ApiRequestFromEnum = apiRequestFromEnum
};
UserInfoSet(Cache.CacheKey.AppStudent_Login_Key + Id.ToString(), userInfo, Common.Config.JwtExpirTime);
}
}
}
}
else
{
userInfo = new AppStudentInfo();
}
return userInfo;
}
#region 账户密码登录的时候连续5次密码错误冻结账户10分钟
///
/// 设置缓存
///
///
public static void StudentFrozenSet(string cacheKey, AppStudentInfo model, int JwtExpirTime)
{
try
{
TimeSpan ts = GetExpirTime(JwtExpirTime);
redis.StringSet(cacheKey, model, ts);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "StudentFrozenSet缓存设置失败");
}
}
///
/// 获取用户登录信息
///
/// 账号Id
/// 请求来源
///
public static AppStudentInfo GetStudentFrozen(object Id, ApiRequestFromEnum apiRequestFromEnum = ApiRequestFromEnum.AppStudent)
{
AppStudentInfo userInfo = null;
if (Id != null)
{
string cacheKey = Cache.CacheKey.Student_Frozen_Key + Id.ToString();
try
{
userInfo = redis.StringGet(cacheKey);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "GetStudentFrozen");
}
if (userInfo == null)
{
userInfo = new AppStudentInfo();
}
}
else
{
userInfo = new AppStudentInfo();
}
return userInfo;
}
///
/// 设置缓存
///
///
public static void StudentErrorLoginSet(string cacheKey, AppStudentLoginError model, int JwtExpirTime)
{
try
{
TimeSpan ts = GetExpirTime(JwtExpirTime);
redis.StringSet(cacheKey, model, ts);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "StudentErrorLoginSet缓存设置失败");
}
}
///
/// 获取用户登录信息
///
/// 账号Id
/// 请求来源
///
public static AppStudentLoginError GetStudentErrorLogin(object Id, ApiRequestFromEnum apiRequestFromEnum = ApiRequestFromEnum.AppStudent)
{
AppStudentLoginError userInfo = null;
if (Id != null)
{
string cacheKey = Cache.CacheKey.Student_ErrorLogin_Key + Id.ToString();
try
{
userInfo = redis.StringGet(cacheKey);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "GetStudentErrorLogin");
}
if (userInfo == null)
{
userInfo = new AppStudentLoginError();
}
}
else
{
userInfo = new AppStudentLoginError();
}
return userInfo;
}
#endregion
}
}