首页 技术 正文
技术 2022年11月15日
0 收藏 328 点赞 3,009 浏览 2410 个字

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

CacheSlidingExpirationHour:时间,缓存时间2小时
CountdownCurrentIndexCacheName:缓存名称
log:日志m_objCountdownCurrentIndexLock::当前对象m_snIntervalSecond:定义一个数组,可以视为概率值
说明:0,1,1,1 数据中存了4个数,我们设为总的概率为100%,每个代表25%,所以现在我设置的是当前的概率为75%存如缓存的是数据的索引,取的时候也取的索引,方法返回索引,转成int类型
 public class CountdownHelper
{
private const int CacheSlidingExpirationHour = ;
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[] { , , , }; //1显示 0不显示 public CountdownHelper()
{
} public int GetCountdownAddedSecond()
{
lock (m_objCountdownCurrentIndexLock)
{
int nCountdownCurrentIndex = ; 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, , null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
}
else
{
nCountdownCurrentIndex = (int)objCountdownCurrentIndex; if (nCountdownCurrentIndex == m_snIntervalSecond.Length - )
{
m_cache[CountdownCurrentIndexCacheName] = ;
}
else
{
m_cache[CountdownCurrentIndexCacheName] = nCountdownCurrentIndex + ;
}
} return m_snIntervalSecond[nCountdownCurrentIndex];
}
catch (Exception __error)
{
//如果需要记录错误日志的,可以记录到这里,我这里没有加
//log.Error("功能介绍GetCountdownAddedSecond:" + __error.Message);
if (nCountdownCurrentIndex > m_snIntervalSecond.Length - )
{
nCountdownCurrentIndex = m_snIntervalSecond.Length - ;
}
return m_snIntervalSecond[nCountdownCurrentIndex];
}
}
} }

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

前台调用是用AJAX方式:

说明:等于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");

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297