项目中的分布式站点缓存刷新实现

实现方式如下:

1 global.asax中设置了定时器定时刷新站点,然后获取缓存

  private void Application_Start(object sender, EventArgs e)
        {
            #region 定时器

            Timer cacheTimer = new Timer { Interval = 10000, AutoReset = true, Enabled = true };
            cacheTimer.Elapsed += new ElapsedEventHandler(this.CacheTime_Start);

            Timer onlineTimer = new Timer { Interval = FWConfig.SessionTimeOut * 60000, AutoReset = true, Enabled = true };
            onlineTimer.Elapsed += new ElapsedEventHandler(this.OnlineTimer_Start);

            #endregion 定时器

            #region 获取缓存

            ApplicationCache.SetOrgData();
            ApplicationCache.SetBasicData();
            ApplicationCache.SetRequestList();

            #endregion 获取缓存
        }
 private void CacheTime_Start(object sender, EventArgs e)
        {
            CacheHelper.RefreshCache();
        }

2 刷新代码如下:

 public static void RefreshCache()
        {
            IOaCacheQueue cacheQueue = new OaCacheQueueBLL();
            string siteId = FWConfig.GetAppSettingConfig("SiteId");
            string siteUrl = String.Empty;
            DataTable dtSite = new OAServerInfoBLL().GetServerList();
            DataRow[] drUrl = dtSite.Select(" MAC_ADDRESS ='" + siteId + "'");
            if (drUrl.Length == 1)
            {
                siteUrl = CommonFunc.ObjectToNullStr(drUrl[0]["SERVER_URL"]);
            }
            DataTable dt = cacheQueue.GetCacheQueue(siteId);
            if (dt.Rows.Count > 0)
            {
                //正在处理
                DataRow[] drElaspes = dt.Select(" ELAPSED ='Y'");
                if (drElaspes.Length == 0 && CommonFunc.IsNotNullString(siteUrl))
                {
                    DataRow[] drNew = dt.Select(" ELAPSED ='N' AND CACHE_TYPE ='P' ");

                    if (drNew.Length > 0)
                    {
                        try
                        {
                            cacheQueue.UpdateCacheQueue(siteId, "Y", "P");
                            var serverUrl = siteUrl.TrimEnd('/') + "/CacheManage/RefreshAllData.aspx?CacheType=P";
                            Uri url = new Uri(serverUrl);
                            WebRequest req = WebRequest.Create(url);
                            req.Credentials = new NetworkCredential(FWConfig.GetAppSettingConfig("Domain_User"), FWConfig.GetAppSettingConfig("Domain_Password"), "DOMAIN");
                            req.GetResponse();
                            cacheQueue.DelCacheQueue(siteId, "P");
                        }
                        catch (Exception ex)
                        {
                            cacheQueue.DelCacheQueue(siteId, "P");
                            LogInfoEntity entity = Utility.LogInfoEntity;
                            entity.Message = ex.Message;
                            entity.Exception = ex.ToString();
                            entity.AppName = "站点 :" + siteId + " 类型 :P";
                            LogUtil.Error(entity, ex);
                        }
                    }
}

其实很简单,就是请求刷新缓存的一个页面,然后在页面更新数据,重新设置页面缓存。

3 页面刷新的代码如下:

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string cacheType = CommonFunc.ObjectToNullStr(this.Request.QueryString["CacheType"]);
                switch (cacheType)
                {
                    case "G":
                        ApplicationCache.SetOrgData();
                        break;
                    case "B":
                        ApplicationCache.SetBasicData();
                        break;
                    case "U":
                        ApplicationCache.SetRequestList();
                        break;
                    case "P":
                        IUmOrganization org = new UmOrganizationBLL();
                        org.UpdateRootPathAndSortPath(org.GetRootID());
                        CacheHelper.AddCacheQueue("G");
                        break;
                }

                this.txtMacAddress.Text = CommonFunc.ObjectToNullStr(FWConfig.GetAppSettingConfig("SiteID"));
                this.txtUrl.Text = new OAServerInfoBLL().GetServerUrl(this.txtMacAddress.Text.Trim());
                this.BindData();
                this.BindServerList();
            }
        }
 /// <summary>
        /// 刷新部门信息
        /// </summary>

        #region public DataTable RefreshOrg()

        public static void SetOrgData()
        {
            WebCacheManager.Instance.SetApplicationCache("ORGANIZATION", new UmOrganizationBLL().GetCacheOrgTable());
            WebCacheManager.Instance.SetApplicationCache("STAFFALL", new UmOrganizationStaffBLL().GetAllStaff());
        }

        #endregion public DataTable RefreshOrg()

        /// <summary>
        /// 刷新基础数据信息
        /// </summary>

        #region public   RefreshBasicDataList()

        public static void SetBasicData()
        {
            WebCacheManager.Instance.SetApplicationCache("BasicDataList", new UmBasicdataBLL().GetBasicDataTableList());
        }

        #endregion public   RefreshBasicDataList()

原文地址:https://www.cnblogs.com/jangwewe/p/3076152.html