C # Polling algorithm
These two days to do things, the business has a special demand, when the user visits the page, for a line of code control, according to the probability of display, I do is for the current page exposure to deal with the exposure code is a third party, the page as long as this code even if the implementation of this exposure code, So I wrote this poll of a method, this method can be modified according to their own needs, the following I put this method all posts:
Cacheslidingexpirationhour: Time, Cache time 2 hours
Countdowncurrentindexcachename: Cache Name
LOG: Logs
M_objcountdowncurrentindexlock:: Current Object
M_snintervalsecond: Defines an array that can be treated as a probability value
Description: 0,1,1,1 Data saved 4 number, we set the probability of 100%, each representing 25%, so now I set the current probability of 75%
What is cached is the index of the data, the index of the fetch, the method returns the index, and the type of 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 Show 0 does not show public Countdownhelper () {} public int getcountdownaddedsecond () {Lock (m_
Objcountdowncurrentindexlock) {int ncountdowncurrentindex = 0;
try {Object objcountdowncurrentindex = M_cache[countdowncurrentindexcachename]; if (Objcountdowncurrentindex = null) {//If a cache is needed, use the following//m_cache. Insert (countdowncurrentindexcachename, 1, NULL, cache.noabsoluteexpiration, TIMESPAN.FRomhours (Cacheslidingexpirationhour), cacheitempriority.notremovable, NULL); Use the following m_cache without caching. Insert (countdowncurrentindexcachename, 1, NULL, cache.noabsoluteexpiration, Cache.noslidingexpiration,
Cacheitempriority.notremovable, NULL);
else {ncountdowncurrentindex = (int) Objcountdowncurrentindex; if (Ncountdowncurrentindex = = m_snintervalsecond.length-1) {M_cache[countdowncurrentindexca
Chename] = 0;
else {M_cache[countdowncurrentindexcachename] = Ncountdowncurrentindex + 1;
} return M_snintervalsecond[ncountdowncurrentindex]; catch (Exception __error) {//If you need to record the error log, you can record here, I do not add//log here. Error ("Feature introduction Getcountdownaddedsecond:" + __error.
message);
if (Ncountdowncurrentindex > M_snintervalsecond.length-1) {ncountdowncurrentindex = m_snintervalsecond.length-1;
return M_snintervalsecond[ncountdowncurrentindex]; }
}
}
}
The requirements for this feature are: The business unit needs to monitor the current page's exposure rate, so you need to use the probability to determine how the current exposure code on the page alternating display, the initial exposure rate is 50%, so the array is directly new int[] {0, 1}, and then changed to 75%, is the above code, So you can monitor the exposure and have control of the exposure code.
The foreground call is in AJAX mode:
Note: equal to 1, the exposure code to add to the page, otherwise do not add
1 <div id= "Adver" ></div>
<!--polling exposure-->
$.post ("/topic/getcountdownaddedsecond", function (data) {
if (data) {
if (Data.num = 1 {
var img_html = "
Thank you for reading, I hope to help you, thank you for your support for this site!