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

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

(0)

相关推荐

  • C# Stream 和 byte[] 之间的转换

    /* - - - - - - - - - - - - - - - - - - - - - - - -   * Stream 和 byte[] 之间的转换  * - - - - - - - - - - - - - - - - - - - - - - - */ /// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] StreamToBytes(Stream stream) {     byte[] bytes 

  • C# DataGridView添加新行的2个方法

    可以静态绑定数据源,这样就自动为DataGridView控件添加 相应的行.假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方 法: 方法一: 复制代码 代码如下: int index=this.dataGridView1.Rows.Add();this.dataGridView1.Rows[index].Cells[0].Value = "1"; this.dataGridView1.Rows[inde

  • C#连接MySql数据库的方法

    1.要连接MySql数据库必须首先下载MySql官方的连接.net的文件,文件下载地址为http://dev.mysql.com/downloads/connector/net/6.6.html#downloads ,下载平台选择.Net&Mono,下载ZIP免安装版.2.解压缩刚才下载的mysql-connector-net-6.6.6-noinstall.zip文件,里面有几个版本选择,在这里我选V4, 选中这几个文件,然后添加到C#项目的引用中,然后就可以编写程序进行数据库的操作了. 3.

  • C#时间格式化(Datetime)用法详解

    Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符 关联属性/说明 d ShortDatePattern D LongDatePattern f 完整日期和时间(长日期和短时间) F FullDateTimePattern(长日期和长时间) g 常规(短日期和短时间) G 常规(短日期和长时间) m.M MonthDayPattern r.R RFC1123Pattern s 使用当地时间的 SortableDateTi

  • C#中HttpWebRequest的用法详解

    本文实例讲述了C#中HttpWebRequest的用法.分享给大家供大家参考.具体如下: HttpWebRequest类主要利用HTTP 协议和服务器交互,通常是通过 GET 和 POST 两种方式来对数据进行获取和提交.下面对这两种方式进行一下说明: GET 方式: GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 http://www.jb51.net/?hl=zh-CN 中,前面部分 http://www.jb51.net表示数据提交的网址,后面部分 hl=zh-CN 表示附

  • C#中List〈string〉和string[]数组之间的相互转换

    1,从System.String[]转到List<System.String> System.String[] str={"str","string","abc"}; List<System.String> listS=new List<System.String>(str); 2, 从List<System.String>转到System.String[] List<System.Strin

  • C# 一个WCF简单实例

    WCF实例(带步骤) 复制代码 代码如下: <xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" /> 本篇转自百度文档,自己试过,确实可以用. 以订票为例简单应用wcf 新建一个wcf服务应用程序 在IService1.cs定义服务契约 复制代码 代码如下: namespace WcfDemo { // 注意: 如果更改此处的接口名称 "IService

  • C# 16进制与字符串、字节数组之间的转换

    复制代码 代码如下: /// <summary> /// 字符串转16进制字节数组 /// </summary> /// <param name="hexString"></param> /// <returns></returns> private static byte[] strToToHexByte(string hexString) { hexString = hexString.Replace(&quo

  • C#几种截取字符串的方法小结

    1.根据单个分隔字符用split截取 例如 复制代码 代码如下: string st="GT123_1"; string[] sArray=st.split("_"); 即可得到sArray[0]="GT123",sArray[1]="1"; 2.利用多个字符来分隔字符串 例如 复制代码 代码如下: string str = "GTAZB_JiangjBen_123";string[] sArray = s

  • c#实现16进制和字符串之间转换的代码

    十六进制字符串与数值类型之间转换(C# 编程指南) 以下示例演示如何执行下列任务: 获取字符串中每个字符的十六进制值. 获取与十六进制字符串中的每个值对应的字符. 将十六进制 string 转换为整型. 将十六进制 string 转换为浮点型. 将字节数组转换为十六进制 string. 示例 此示例输出 string 中的每个字符的十六进制值.首先,它将 string 分析为字符数组,然后对每个字符调用 ToInt32(Char) 以获取相应的数字值.最后,在 string 中将数字的格式设置为

随机推荐