• 如何使用Senparc.Weixin SDK 底层的Redis缓存并设置过期时间


    最近在微信第三方平台项目开发中,有一个需求,所有绑定的公众号的回复规则按照主公众号的关键词配置来处理,我的处理思路是获取主公众号配置的关键词回复规则,缓存10分钟,由于需要使用Redis缓存来存储一些数据,看来一下底层的实现,是使用HashSet的结构数据,而HashSet是不会过期的,通过在群里的交流,有一位管理员介绍说可以给值设置缓存过期时间,我按照思路实现了一下:

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

    该类实现的是将存储的值进行再一次包装,增加创建和过期的时间。

    在方法中使用,每次取值时判断一下是否过期,如果过期就重新获取一次,并更新缓存

            /// <summary>
            /// 获取主公众号自动回复规则设置
            /// </summary>
            /// <returns></returns>
            protected GetCurrentAutoreplyInfoResult GetCurrentAutoreplyInfo()
            {
                // 进行缓存
                GetCurrentAutoreplyInfoResult infoResult = null;
                try
                {
                    IContainerCacheStrategy containerCache = LocalContainerCacheStrategy.Instance;
                    IBaseObjectCacheStrategy baseCache = containerCache.BaseCacheStrategy();
                    string key = "AutoreplyInfo:" + appId;
                    HashSetCacheModel<GetCurrentAutoreplyInfoResult> cacheModel = null;
                    if (baseCache.CheckExisted(key))
                    {
                        cacheModel = baseCache.Get<HashSetCacheModel<GetCurrentAutoreplyInfoResult>>(key);
                        if (cacheModel.ExpireAt < DateTime.Now)
                        {
                            // 过期了,要更新一次
                            infoResult = AutoReplyApi.GetCurrentAutoreplyInfo(appId);
                            cacheModel = new HashSetCacheModel<GetCurrentAutoreplyInfoResult>();
                            cacheModel.CreateOn = DateTime.Now;
                            cacheModel.ExpireAt = DateTime.Now.AddMinutes(10);
                            cacheModel.Value = infoResult;
                            baseCache.Update(key, cacheModel);
                        }
                        infoResult = cacheModel.Value;
                    }
                    else
                    {
                        infoResult = AutoReplyApi.GetCurrentAutoreplyInfo(appId);
                        cacheModel = new HashSetCacheModel<GetCurrentAutoreplyInfoResult>();
                        cacheModel.CreateOn = DateTime.Now;
                        cacheModel.ExpireAt = DateTime.Now.AddMinutes(10);
                        cacheModel.Value = infoResult;
                        baseCache.Set(key, cacheModel);
                    }
                }
                catch (Exception ex)
                {
                    NLogHelper.Warn(ex, "GetCurrentAutoreplyInfo");
                }
    
                return infoResult;
            }

    大家看看怎样呢?

  • 相关阅读:
    图的深度优先遍历(邻接表,递归,非递归)
    图的深度优先遍历(邻接矩阵,递归,非递归)
    【转】C语言邻接表的实现
    图的存储
    堆排序_C实现
    快排_C实现
    交换二叉树中所有结点的左右子树的位置
    二叉树层次遍历_判断结点所属层次
    二叉树_非递归先中后序_递归非递归求深度
    二叉树非递归遍历(前、中、后)
  • 原文地址:https://www.cnblogs.com/hnsongbiao/p/9257087.html
Copyright © 2020-2023  润新知