国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c# 實現輪詢算法實例代碼

2019-10-29 21:18:11
字體:
來源:轉載
供稿:網友

c# 輪詢算法

  這兩天做東西,業務上有個特殊的需求,在用戶訪問頁面的時候,針對某一行代碼進行控制,按照概率來進行顯示,我做的是針對當前頁面的曝光進行處理,曝光代碼是第三方的,頁面上只要有這段代碼就算是執行了這段曝光代碼,所以才寫了這個輪詢的一個方法,這個方法可以根據自己的需求修改,下面我把這個方法全部帖出來:

CacheSlidingExpirationHour:時間,緩存時間2小時

CountdownCurrentIndexCacheName:緩存名稱

log:日志

m_objCountdownCurrentIndexLock::當前對象

m_snIntervalSecond:定義一個數組,可以視為概率值

說明:0,1,1,1  數據中存了4個數,我們設為總的概率為100%,每個代表25%,所以現在我設置的是當前的概率為75%

存如緩存的是數據的索引,取的時候也取的索引,方法返回索引,轉成int類型

 public class CountdownHelper  {     private const int CacheSlidingExpirationHour = 2;     private const string CountdownCurrentIndexCacheName = "OnlineMeetingCountdownCurrentIndex";     private static IAppLog log = AppLoggerManager.GetLogger(typeof(CountdownHelper));     private static Cache m_cache = HttpContext.Current.Cache;     private static object m_objCountdownCurrentIndexLock = new object();     private static int[] m_snIntervalSecond = new int[] { 0, 1 , 1 , 1}; //1顯示 0不顯示      public CountdownHelper()     {     }     public int GetCountdownAddedSecond()     {       lock (m_objCountdownCurrentIndexLock)       {         int nCountdownCurrentIndex = 0;          try         {           object objCountdownCurrentIndex = m_cache[CountdownCurrentIndexCacheName];           if (objCountdownCurrentIndex == null)           {             //如果需要加緩存的,就用下面的             //m_cache.Insert(CountdownCurrentIndexCacheName, 1, null, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(CacheSlidingExpirationHour), CacheItemPriority.NotRemovable, null);             //不用加緩存的用下面的             m_cache.Insert(CountdownCurrentIndexCacheName, 1, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);           }           else           {             nCountdownCurrentIndex = (int)objCountdownCurrentIndex;             if (nCountdownCurrentIndex == m_snIntervalSecond.Length - 1)             {               m_cache[CountdownCurrentIndexCacheName] = 0;             }             else             {               m_cache[CountdownCurrentIndexCacheName] = nCountdownCurrentIndex + 1;             }           }            return m_snIntervalSecond[nCountdownCurrentIndex];        }        catch (Exception __error)         {           //如果需要記錄錯誤日志的,可以記錄到這里,我這里沒有加           //log.Error("功能介紹GetCountdownAddedSecond:" + __error.Message);           if (nCountdownCurrentIndex > m_snIntervalSecond.Length - 1)           {             nCountdownCurrentIndex = m_snIntervalSecond.Length - 1;          }           return m_snIntervalSecond[nCountdownCurrentIndex];         }       }     }    }

  這個功能的需求是:業務部門需要監控當前頁面的曝光率,所以需要用概率去判斷當前的曝光代碼如何在頁面上交替顯示,起初是曝光率為50%,所以數組中直接就是new int[] { 0, 1},后來改成75%,就是上面的代碼,所以這樣既可以監控曝光,有可以控制曝光代碼。

前臺調用是用AJAX方式:

說明:等于1,將曝光代碼添加到頁面,否則不加

1 <div id="adver"></div>
<!--輪詢曝光-->   $.post("/Topic/GetCountdownAddedSecond", function (data) {    if (data) {       if (data.num == 1) {        var img_html = "<img src=/"https://d_directed_treatment =?/ment/" style=/"display:none;/">";         $("#adver").html(img_html);       }     }   }, "json");

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙南县| 鹤岗市| 法库县| 海口市| 柳河县| 绥江县| 铜梁县| 会泽县| 遂溪县| 隆昌县| 教育| 手机| 永宁县| 桂林市| 山西省| 七台河市| 从江县| 华阴市| 鄢陵县| 新闻| 汤阴县| 泊头市| 兴国县| 海口市| 涟源市| 越西县| 共和县| 南投县| 北京市| 抚州市| 苍山县| 玉林市| 惠水县| 休宁县| 行唐县| 南川市| 炉霍县| 正宁县| 健康| 从江县| 仁化县|