代码:
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_instance = new Lazy (() => 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类型操作 /// /// 根据key获取对应的对象T /// ////// /// public T GetObj (string key) { T result; try { using (var redis = this.getRedisClient()) { result = redis.Get (key); } } catch (Exception) { result = default(T); } return result; } /// /// 根据key存储T对象 /// ////// /// /// /// public bool SetObj (string key, T val, DateTime dateTime) { bool result = false; try { using (var redis = this.getRedisClient()) { result = redis.Set (key, val, dateTime); } } catch { result = false; } return result; } /// /// 根据key更新T /// ////// /// /// public bool UpdateObj (string key, T t) { bool result = false; using (var redis = this.getRedisClient()) { var value = JsonConvert.SerializeObject(t); result = redis.Set (key, value); } return result; } /// /// 删除对应key的value /// /// ///public bool RemoveObj(string key) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.Remove(key); } return result; } #endregion #region hash类型操作 /// /// 从hash表获取数据 /// public T Get(string hashId, string key) { using (var redis = this.getRedisClient()) { string value = redis.GetValueFromHash(hashId, key); return JsonConvert.DeserializeObject (value); } } /// /// 获取整个hash的数据 /// public ListGetAll (string hashId) { using (var redis = this.getRedisClient()) { var result = new List (); var list = redis.GetHashValues(hashId); if (list != null && list.Count > 0) { list.ForEach(x => { var value = JsonConvert.DeserializeObject (x); result.Add(value); }); } return result; } } /// /// 判断某个数据是否已经被缓存 /// public bool Exist(string hashId, string key) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.HashContainsEntry(hashId, key); } return result; } /// /// 存储数据到hash表 /// public bool Set(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; } /// /// 移除hash中的某值 /// 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; } ////// 移除整个hash /// public bool RemoveAll(string hashId) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.Remove(hashId); } return result; } ////// 设置缓存过期 /// public void SetExpire(string hashId, DateTime datetime) { using (var redis = this.getRedisClient()) { redis.ExpireEntryAt(hashId, datetime); } } #endregion #region 保存到硬盘 ////// 保存数据DB文件到硬盘 /// public void Save() { using (var redis = this.getRedisClient()) { redis.Save(); } } ////// 异步保存数据DB文件到硬盘 /// 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 { ////// 设置对象过期时间 /// private static readonly int interval = Convert.ToInt32(ConfigurationManager.AppSettings["Redis_TimeInterval"]); ////// 存储对象val,获取对应的token /// /// ///public static RedisOpearteResult getToken(Manage_UserInfo val) { string tokenID = Guid.NewGuid().ToString(); RedisOpearteResult result = new RedisOpearteResult { isok = RedisCommon.getInstance.SetObj (tokenID, val, DateTime.Now.AddMinutes(interval)), token = tokenID, result = JsonConvert.SerializeObject(val) }; return result; } /// /// 根据tokenID更新用户对象 /// /// /// ///public static RedisOpearteResult RefreshLoginTokenData(String tokenID, Manage_UserInfo val) { RedisOpearteResult result = new RedisOpearteResult { isok = RedisCommon.getInstance.SetObj (tokenID, val, DateTime.Now.AddMinutes(interval)), token = tokenID, result = JsonConvert.SerializeObject(val) }; return result; } /// /// 刷新用户token /// /// public static RedisOpearteResult RefreshUserToken(string tokenID) { var obj = RedisCommon.getInstance.GetObj(tokenID); var isExist = obj != null; RedisOpearteResult result = new RedisOpearteResult { isok = isExist, token = tokenID, result = "Token过期" }; if (isExist) { result.result = "成功延迟"; RedisCommon.getInstance.SetObj (tokenID, obj, DateTime.Now.AddMinutes(interval)); } return result; } /// /// 退出 /// /// ///public static RedisOpearteResult LoginOff(string tokenID) { var obj = RedisCommon.getInstance.GetObj (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; } /// /// 通过token 获取用户信息 /// /// tokenID ///public static bool getUserByToken(string token, out Manage_UserInfo user) { bool isok = false; user = null; if (!string.IsNullOrEmpty(token) && RedisCommon.getInstance.GetObj (token)!=null) { user = RedisCommon.getInstance.GetObj (token); isok = true; } return isok; } }}