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


    没啥说的,直接上代码

    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; }
        }
    }


  • 相关阅读:
    Java提高篇(三四)-----fail-fast机制
    opencv提取surf特征点出现的错误
    Android开发_Gson解析
    关于权限管理设计文章整理,希望对大家有所帮助
    URAL
    JAVA编程心得-多态设计初步
    paip. mysql如何临时 暂时 禁用 关闭 触发器
    Citrix服务器虚拟化之三十 XenApp 6.5发布流式应用程序
    在TextView使用部分颜色文字
    微软面试题:求整数随机数构成的数组中找到长度大于=3的最长的等差数列
  • 原文地址:https://www.cnblogs.com/hnsongbiao/p/9299190.html
Copyright © 2020-2023  润新知