栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 

   $.post("/Topic/GetCountdownAddedSecond", function (data) {
    if (data) {
if (data.num == 1) {
 var img_html = "";
  $("#adver").html(img_html);
}
     }
   }, "json");

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123878.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号