如何使用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;
        }

大家看看怎样呢?

猜你喜欢

转载自www.cnblogs.com/hnsongbiao/p/9257087.html