当前位置: 移动技术网 > IT编程>开发语言>c# > c# 实现轮询算法实例代码

c# 实现轮询算法实例代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
c# 轮询算法   这两天做东西,业务上有个特殊的需求,在用户访问页面的时候,针对某一行代码进行控制,按照概率来进行显示,我做的是针对当前页面的曝光进行处理,曝光代码是第

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");

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网