zoukankan      html  css  js  c++  java
  • c#-轮询算法

      这两天做东西,业务上有个特殊的需求,在用户访问页面的时候,针对某一行代码进行控制,按照概率来进行显示,我做的是针对当前页面的曝光进行处理,曝光代码是第三方的,页面上只要有这段代码就算是执行了这段曝光代码,所以才写了这个轮询的一个方法,这个方法可以根据自己的需求修改,下面我把这个方法全部帖出来:

    CacheSlidingExpirationHour:时间,缓存时间2小时
    CountdownCurrentIndexCacheName:缓存名称
    log:日志

    m_objCountdownCurrentIndexLock::当前对象

    m_snIntervalSecond:定义一个数组,可以视为概率值
    说明:0,1,1,1 数据中存了4个数,我们设为总的概率为100%,每个代表25%,所以现在我设置的是当前的概率为75%

    存如缓存的是数据的索引,取的时候也取的索引,方法返回索引,转成int类型

     1 public class CountdownHelper
     2     {
     3         private const int CacheSlidingExpirationHour = 2;
     4         private const string CountdownCurrentIndexCacheName = "OnlineMeetingCountdownCurrentIndex";
     5         private static IAppLog log = AppLoggerManager.GetLogger(typeof(CountdownHelper));
     6         private static Cache m_cache = HttpContext.Current.Cache;
     7         private static object m_objCountdownCurrentIndexLock = new object();
     8         private static int[] m_snIntervalSecond = new int[] { 0, 1 , 1 , 1}; //1显示  0不显示
     9 
    10         public CountdownHelper()
    11         {
    12         }
    13 
    14         public int GetCountdownAddedSecond()
    15         {
    16             lock (m_objCountdownCurrentIndexLock)
    17             {
    18                 int nCountdownCurrentIndex = 0;
    19 
    20                 try
    21                 {
    22                     object objCountdownCurrentIndex = m_cache[CountdownCurrentIndexCacheName];
    23                     if (objCountdownCurrentIndex == null)
    24                     {
    25                         //如果需要加缓存的,就用下面的
    26                         //m_cache.Insert(CountdownCurrentIndexCacheName, 1, null, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(CacheSlidingExpirationHour), CacheItemPriority.NotRemovable, null);
    27                         //不用加缓存的用下面的
    28                         m_cache.Insert(CountdownCurrentIndexCacheName, 1, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
    29                     }
    30                     else
    31                     {
    32                         nCountdownCurrentIndex = (int)objCountdownCurrentIndex;
    33 
    34                         if (nCountdownCurrentIndex == m_snIntervalSecond.Length - 1)
    35                         {
    36                             m_cache[CountdownCurrentIndexCacheName] = 0;
    37                         }
    38                         else
    39                         {
    40                             m_cache[CountdownCurrentIndexCacheName] = nCountdownCurrentIndex + 1;
    41                         }
    42                     }
    43 
    44                     return m_snIntervalSecond[nCountdownCurrentIndex];
    45                 }
    46                 catch (Exception __error)
    47                 {
    48                     //如果需要记录错误日志的,可以记录到这里,我这里没有加
    49                     //log.Error("功能介绍GetCountdownAddedSecond:" + __error.Message);
    50                     if (nCountdownCurrentIndex > m_snIntervalSecond.Length - 1)
    51                     {
    52                         nCountdownCurrentIndex = m_snIntervalSecond.Length - 1;
    53                     }
    54                     return m_snIntervalSecond[nCountdownCurrentIndex];
    55                 }
    56             }
    57         }
    58 
    59     }

      这个功能的需求是:业务部门需要监控当前页面的曝光率,所以需要用概率去判断当前的曝光代码如何在页面上交替显示,起初是曝光率为50%,所以数组中直接就是new int[] { 0, 1},后来改成75%,就是上面的代码,所以这样既可以监控曝光,有可以控制曝光代码。

    前台调用是用AJAX方式:

    说明:等于1,将曝光代码添加到页面,否则不加

    1 <div id="adver"></div>
    1      <!--轮询曝光-->
    2      $.post("/Topic/GetCountdownAddedSecond", function (data) {
    3         if (data) {
    4              if (data.num == 1) {
    5                 var img_html = "<img src="https://d_directed_treatment =?ment" style="display:none;">";
    6                  $("#adver").html(img_html);
    7              }
    8          }
    9      }, "json");

    版权声明:欢迎转载,但是看在我辛勤劳动的份上,请注明来源:http://www.cnblogs.com/zhangpengnike/p/5546046.html (未经允许严禁用于商业用途!)

  • 相关阅读:
    第十五天-linux系统文件权限详细讲解
    GB28181实现H265 H264摄像头 Web端无插件直播
    海康8700等联网网关通过GB28181接入LiveGBS流媒体服务实现web端无插件直播
    LiveNVR如何对接LiveQing云平台
    Onvif/RTSP视频流对接云平台-实现高性能云端直播及录像存储方案
    Onvif/RTSP摄像头实现按需直播-降低带宽流量使用
    LiveQing
    使用LiveGBS将GB28181流转成RTSP流上大屏
    实现国标GB28181流媒体服务解决方案
    LiveGBS GB28181流媒体服务-产品介绍及相关资源
  • 原文地址:https://www.cnblogs.com/zhangpengnike/p/5546046.html
Copyright © 2011-2022 走看看