分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

基于Senparc.CO2NET 缓存策略扩展的缓存使用方法

发布时间:2023-09-06 02:03责任编辑:苏小强关键词:缓存

没啥说的,直接上代码

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> ???????/// 缓存处理 Redis ???????/// </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; ???????}



基于Senparc.CO2NET 缓存策略扩展的缓存使用方法

原文地址:https://www.cnblogs.com/hnsongbiao/p/9299190.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved