沒啥說的,直接上代碼
1、緩存 CacheFactory 實現:
//----------------------------------------------------------------------- // <copyright file="CacheFactory.cs" company="FenSiShengHuo, Ltd."> // Copyright (c) 2018 , All rights reserved. // </copyright> //----------------------------------------------------------------------- using Senparc.CO2NET.Cache; using Senparc.Weixin.Cache; using System; namespace DotNet.WeChat.CommonService.Utilities { using DotNet.MVC.Infrastructure.Models; /// <summary> /// CacheFactory 基於 /// /// 修改記錄 /// /// 2018-04-20 版本:1.0 JiShiYu 創建文件。 /// /// <author> /// <name>JiShiYu</name> /// <date>2018-04-20</date> /// </author> /// </summary> public class CacheFactory { /// <summary> /// 緩存處理 /// </summary> /// <param name="cacheKey">緩存的Key</param> /// <param name="proc">處理函數</param> /// <param name="isCache">是否從緩存取</param> /// <param name="refreshCache">是否強制刷新緩存</param> /// <param name="expireAt">什么時候過期</param> /// <returns></returns> public static HashSetCacheModel<T> Cache<T>(string cacheKey, Func<HashSetCacheModel<T>> proc,bool isCache = false, bool refreshCache = false) { HashSetCacheModel<T> result; if (!isCache) { result = proc(); } else { IContainerCacheStrategy containerCache = LocalContainerCacheStrategy.Instance; IBaseObjectCacheStrategy baseCache = containerCache.BaseCacheStrategy(); // 緩存 if (baseCache.CheckExisted(cacheKey)) //判斷是否有緩存 { //已經緩存 if (refreshCache)//是否強制刷新緩存 { //強制刷新 result = proc(); baseCache.Update(cacheKey, result); } else { //不強制刷新 result = baseCache.Get<HashSetCacheModel<T>>(cacheKey); // 判斷是否過期了 if (result.ExpireAt < DateTime.Now) { result = proc(); baseCache.Update(cacheKey, result); } } } else { // 緩存中沒有數據 獲取一次存入緩存 result = proc(); baseCache.Set(cacheKey, result); } } return result; } } }
注意:Senparc 底層的Redis使用了HashSet數據結構
2、使用方法:注釋里是以前的寫法,使用CacheFactory后簡化了操作
/// <summary> /// 獲取微信用戶 /// </summary> /// <param name="weixinAppId"></param> /// <param name="openId"></param> /// <param name="refresh"></param> /// <returns></returns> protected WechatUserEntity GetWechatUser(string weixinAppId, string openId, bool refresh = false) { WechatUserEntity wechatUserEntity = null; // 進行緩存 try { string cacheKey = "WechatUser:" + weixinAppId + ":" + openId; HashSetCacheModel<WechatUserEntity> hashCacheModel= CacheFactory.Cache<WechatUserEntity>(cacheKey, () => { WechatUserManager wechatUserManager = new WechatUserManager(); wechatUserEntity = wechatUserManager.GetObjectByWechatAppIdByOpenId(weixinAppId, openId); var cacheModel = new HashSetCacheModel<WechatUserEntity>(); cacheModel.CreateOn = DateTime.Now; cacheModel.ExpireAt = DateTime.Now.AddMinutes(10); cacheModel.Value = wechatUserEntity; return cacheModel; }); return hashCacheModel.Value; //IContainerCacheStrategy containerCache = LocalContainerCacheStrategy.Instance; //IBaseObjectCacheStrategy baseCache = containerCache.BaseCacheStrategy(); //HashSetCacheModel<WechatUserEntity> cacheModel = null; //// 強制刷新緩存 //if (refresh) //{ // if (baseCache.CheckExisted(key)) // { // baseCache.RemoveFromCache(key); // } //} //if (baseCache.CheckExisted(key)) //{ // cacheModel = baseCache.Get<HashSetCacheModel<WechatUserEntity>>(key); // if (cacheModel.ExpireAt < DateTime.Now) // { // // 過期了,要更新一次 // WechatUserManager wechatUserManager = new WechatUserManager(); // wechatUserEntity = wechatUserManager.GetObjectByWechatAppIdByOpenId(weixinAppId, openId); // cacheModel = new HashSetCacheModel<WechatUserEntity>(); // cacheModel.CreateOn = DateTime.Now; // cacheModel.ExpireAt = DateTime.Now.AddMinutes(10); // cacheModel.Value = wechatUserEntity; // baseCache.Update(key, cacheModel); // } // wechatUserEntity = cacheModel.Value; //} //else //{ // WechatUserManager wechatUserManager = new WechatUserManager(); // wechatUserEntity = wechatUserManager.GetObjectByWechatAppIdByOpenId(weixinAppId, openId); // cacheModel = new HashSetCacheModel<WechatUserEntity>(); // cacheModel.CreateOn = DateTime.Now; // cacheModel.ExpireAt = DateTime.Now.AddMinutes(10); // cacheModel.Value = wechatUserEntity; // baseCache.Set(key, cacheModel); //} } catch (Exception ex) { NLogHelper.Warn(ex, "GetCurrentAutoreplyInfo,weixinAppId=" + weixinAppId); } return wechatUserEntity; }
3、HashSetCacheModel 實體,對Hset值的一次封裝
//----------------------------------------------------------------------- // <copyright file="WeChatMPController" company="FenSiShengHuo, Ltd."> // Copyright (c) 2018 , All rights reserved. // </copyright> //----------------------------------------------------------------------- using System; namespace DotNet.MVC.Infrastructure.Models { /// <summary> /// HashSetCacheModel<T> /// HashSet結構 /// /// 修改紀錄 /// /// 2018-07-02版本:1.0 JiShiYu 創建文件。 /// /// <author> /// <name>JiShiYu</name> /// <date>2018-07-02</date> /// </author> /// </summary> public class HashSetCacheModel<T> { /// <summary> /// 進入緩存的時間 /// </summary> public DateTime CreateOn { set; get; } = DateTime.Now; /// <summary> /// 緩存過期時間 /// </summary> public DateTime ExpireAt { set; get; } /// <summary> /// 緩存對象的值 /// </summary> public T Value { get; set; } } }