There's nothing to say, just the code.
1, Cache Cachefactory implementation:
//-----------------------------------------------------------------------//<copyright file= "CacheFactory.cs" company= "Fensishenghuo, Ltd." >//Copyright (c) 2018, All rights reserved.//</copyright>//-----------------------------------------------------------------------usingSenparc.CO2NET.Cache;usingSenparc.Weixin.Cache;usingSystem;namespacedotnet.wechat.commonservice.utilities{usingDotNet.MVC.Infrastructure.Models; /// <summary> ///Cachefactory based on/// ///Modify a record/// ///2018-04-20 Version: 1.0 jishiyu Create a file. /// /// <author> /// <name>Jishiyu</name> /// <date>2018-04-20</date> /// </author> /// </summary> Public classCachefactory {/// <summary> ///Cache processing Redis/// </summary> /// <param name= "CacheKey" >the cached key</param> /// <param name= "proc" >processing Functions</param> /// <param name= "Iscache" >whether to fetch from cache</param> /// <param name= "RefreshCache" >whether to force the cache to flush</param> /// <param name= "Expireat" >when does it expire?</param> /// <returns></returns> Public StaticHashsetcachemodel<t> cache<t> (stringCacheKey, FuncBOOLIscache =false,BOOLRefreshCache =false) {Hashsetcachemodel<T>result; if(!Iscache) {Result=proc (); } Else{icontainercachestrategy Containercache=localcontainercachestrategy.instance; Ibaseobjectcachestrategy Basecache=Containercache.basecachestrategy (); //Cache if(basecache.checkexisted (CacheKey))//determine if there is a cache { //has been cached if(RefreshCache)//whether to force the cache to flush { //Force Refreshresult =proc (); Basecache.update (CacheKey, result); } Else { //do not force refreshresult = Basecache.get(CacheKey); //Judging if it's expired if(result.) Expireat <datetime.now) {result=proc (); Basecache.update (CacheKey, result); } } } Else { //There is no data in the cache to get cached onceresult =proc (); Basecache.set (CacheKey, result); } } returnresult; } }}
Note: Redis at the bottom of the SENPARC uses the HASHSET data structure
2. How to use: note is the previous wording, the use of cachefactory after simplifying the operation
/// <summary> ///Get user/// </summary> /// <param name= "Weixinappid" ></param> /// <param name= "OpenId" ></param> /// <param name= "Refresh" ></param> /// <returns></returns> protectedWechatuserentity Getwechatuser (stringWeixinappid,stringOpenidBOOLRefresh =false) {wechatuserentity wechatuserentity=NULL; //to Cache Try { stringCacheKey ="Wechatuser:"+ Weixinappid +":"+openId; Hashsetcachemodel<WechatUserEntity> hashcachemodel= cachefactory.cache<wechatuserentity> (CacheKey, () ={Wechatusermanager Wechatusermanager=NewWechatusermanager (); Wechatuserentity=Wechatusermanager.getobjectbywechatappidbyopenid (Weixinappid, openId); varCachemodel =NewHashsetcachemodel<wechatuserentity>(); Cachemodel.createon=DateTime.Now; Cachemodel.expireat= DateTime.Now.AddMinutes (Ten); Cachemodel.value=wechatuserentity; returnCachemodel; }); returnHashcachemodel.value; //icontainercachestrategy containercache = localcontainercachestrategy.instance; //ibaseobjectcachestrategy Basecache = Containercache.basecachestrategy (); //hashsetcachemodel<wechatuserentity> Cachemodel = null; ////Force Flush Cache //if (refresh)//{ //if (basecache.checkexisted (key))// { //Basecache.removefromcache (key); // } //} //if (basecache.checkexisted (key))//{ //Cachemodel = basecache.get//if (Cachemodel.expireat < DateTime.Now)// { // //expired, to be updated once//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); } returnwechatuserentity; }
Cache usage method based on senparc.co2net cache policy extension