代码:
RedisCommon
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ServiceStack.Redis; using System.Configuration; using ServiceStack.Redis.Generic; using Newtonsoft.Json; namespace Rongzi.BZone.Common.Util { public class RedisCommon { private static readonly Lazy<RedisCommon> _instance = new Lazy<RedisCommon>(() => new RedisCommon()); private static readonly string redisUrl = ConfigurationManager.AppSettings["Redis_Server"]; private static readonly string redisPort = ConfigurationManager.AppSettings["Redis_Port"]; private RedisCommon() { } public static RedisCommon getInstance { get { return _instance.Value; } } public RedisClient getRedisClient() { return new RedisClient(redisUrl, int.Parse(redisPort)); } #region string类型操作 /// <summary> /// 根据key获取对应的对象T /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public T GetObj<T>(string key) { T result; try { using (var redis = this.getRedisClient()) { result = redis.Get<T>(key); } } catch (Exception) { result = default(T); } return result; } /// <summary> /// 根据key存储T对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="val"></param> /// <param name="dateTime"></param> /// <returns></returns> public bool SetObj<T>(string key, T val, DateTime dateTime) { bool result = false; try { using (var redis = this.getRedisClient()) { result = redis.Set<T>(key, val, dateTime); } } catch { result = false; } return result; } /// <summary> /// 根据key更新T /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="t"></param> /// <returns></returns> public bool UpdateObj<T>(string key, T t) { bool result = false; using (var redis = this.getRedisClient()) { var value = JsonConvert.SerializeObject(t); result = redis.Set<string>(key, value); } return result; } /// <summary> /// 删除对应key的value /// </summary> /// <param name="key"></param> /// <returns></returns> public bool RemoveObj(string key) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.Remove(key); } return result; } #endregion #region hash类型操作 /// <summary> /// 从hash表获取数据 /// </summary> public T Get<T>(string hashId, string key) { using (var redis = this.getRedisClient()) { string value = redis.GetValueFromHash(hashId, key); return JsonConvert.DeserializeObject<T>(value); } } /// <summary> /// 获取整个hash的数据 /// </summary> public List<T> GetAll<T>(string hashId) { using (var redis = this.getRedisClient()) { var result = new List<T>(); var list = redis.GetHashValues(hashId); if (list != null && list.Count > 0) { list.ForEach(x => { var value = JsonConvert.DeserializeObject<T>(x); result.Add(value); }); } return result; } } /// <summary> /// 判断某个数据是否已经被缓存 /// </summary> public bool Exist<T>(string hashId, string key) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.HashContainsEntry(hashId, key); } return result; } /// <summary> /// 存储数据到hash表 /// </summary> public bool Set<T>(string hashId, string key, T t) { bool result = false; try { using (var redis = this.getRedisClient()) { var value = JsonConvert.SerializeObject(t); result = redis.SetEntryInHash(hashId, key, value); } } catch { result = false; } return result; } /// <summary> /// 移除hash中的某值 /// </summary> public bool Remove(string hashId, string key) { bool result = false; try { using (var redis = this.getRedisClient()) { result = redis.RemoveEntryFromHash(hashId, key); } } catch { result = false; } return result; } /// <summary> /// 移除整个hash /// </summary> public bool RemoveAll(string hashId) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.Remove(hashId); } return result; } /// <summary> /// 设置缓存过期 /// </summary> public void SetExpire(string hashId, DateTime datetime) { using (var redis = this.getRedisClient()) { redis.ExpireEntryAt(hashId, datetime); } } #endregion #region 保存到硬盘 /// <summary> /// 保存数据DB文件到硬盘 /// </summary> public void Save() { using (var redis = this.getRedisClient()) { redis.Save(); } } /// <summary> /// 异步保存数据DB文件到硬盘 /// </summary> public void SaveAsync() { using (var redis = this.getRedisClient()) { redis.SaveAsync(); } } #endregion } }
TokenManager
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Rongzi.BZone.Admin.Models; using Rongzi.BZone.Common.Util; using System.Configuration; namespace Rongzi.BZone.Admin.Functions { public class TokenManager { /// <summary> /// 设置对象过期时间 /// </summary> private static readonly int interval = Convert.ToInt32(ConfigurationManager.AppSettings["Redis_TimeInterval"]); /// <summary> /// 存储对象val,获取对应的token /// </summary> /// <param name="val"></param> /// <returns></returns> public static RedisOpearteResult getToken(Manage_UserInfo val) { string tokenID = Guid.NewGuid().ToString(); RedisOpearteResult result = new RedisOpearteResult { isok = RedisCommon.getInstance.SetObj<Manage_UserInfo>(tokenID, val, DateTime.Now.AddMinutes(interval)), token = tokenID, result = JsonConvert.SerializeObject(val) }; return result; } /// <summary> /// 根据tokenID更新用户对象 /// </summary> /// <param name="tokenID"></param> /// <param name="val"></param> /// <returns></returns> public static RedisOpearteResult RefreshLoginTokenData(String tokenID, Manage_UserInfo val) { RedisOpearteResult result = new RedisOpearteResult { isok = RedisCommon.getInstance.SetObj<Manage_UserInfo>(tokenID, val, DateTime.Now.AddMinutes(interval)), token = tokenID, result = JsonConvert.SerializeObject(val) }; return result; } /// <summary> /// 刷新用户token /// </summary> /// <param name="tokenID"></param> public static RedisOpearteResult RefreshUserToken(string tokenID) { var obj = RedisCommon.getInstance.GetObj<Manage_UserInfo>(tokenID); var isExist = obj != null; RedisOpearteResult result = new RedisOpearteResult { isok = isExist, token = tokenID, result = "Token过期" }; if (isExist) { result.result = "成功延迟"; RedisCommon.getInstance.SetObj<Manage_UserInfo>(tokenID, obj, DateTime.Now.AddMinutes(interval)); } return result; } /// <summary> /// 退出 /// </summary> /// <param name="tokenID"></param> /// <returns></returns> public static RedisOpearteResult LoginOff(string tokenID) { var obj = RedisCommon.getInstance.GetObj<Manage_UserInfo>(tokenID); var isExist = obj != null; RedisOpearteResult result = new RedisOpearteResult { isok = isExist, token = tokenID, result = "Token过期" }; if (isExist) { result.result = "退出成功"; RedisCommon.getInstance.RemoveObj(tokenID); } return result; } /// <summary> /// 通过token 获取用户信息 /// </summary> /// <param name="token">tokenID</param> /// <returns></returns> public static bool getUserByToken(string token, out Manage_UserInfo user) { bool isok = false; user = null; if (!string.IsNullOrEmpty(token) && RedisCommon.getInstance.GetObj<Manage_UserInfo>(token)!=null) { user = RedisCommon.getInstance.GetObj<Manage_UserInfo>(token); isok = true; } return isok; } } }
时间: 2024-10-10 23:06:57