zoukankan      html  css  js  c++  java
  • 项目中的分布式站点缓存刷新实现

    实现方式如下:

    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()
    
    
  • 相关阅读:
    自定义GridView分页控件
    ASP.NET中JSON的序列化和反序列化 C#的JSON数据格式转换方法
    C# 中 Struct 与 Class 的区别,以及两者的适用场合
    IEnumerable
    谈谈C#中的三个关键词new , virtual , override
    常见通信协议
    当base-package="controller.*"时,可见packageSearchPath为"classpath*:controller/*/**/*.class": 当base-package="controller.**"时,可见packageSearchPath为"classpath*:controller/**/**/*.class":
    DO,DTO和VO的使用
    mysql 字符
    mysql sql 分析
  • 原文地址:https://www.cnblogs.com/jangwewe/p/3076152.html
Copyright © 2011-2022 走看看