c# polling algorithm
There is a special business requirement in the past two days when doing things. When the user accesses the page, he controls a certain line of code and displays it according to probability. What I do is to process the exposure of the current page. The exposure code is a third party. As long as there is this code on the page, it is considered that this exposure code is executed. Therefore, I wrote a method of polling. This method can be modified according to my own needs. I will post all this method below:
CacheSlidingExpirationHour: time, cache time 2 hours
CountdownCurrentIndexCacheName: Cache Name
log: log
m_objCountdownCurrentIndexLock:: Current object
m_snIntervalSecond: Define an array, which can be regarded as a probability value
Description: 0,1,1,1 There are 4 numbers stored in the data. The probability we set to be 100%, each representing 25%, so now I set the current probability to be 75%.
If the cached index is the data index, the index is also taken when fetching. The method returns the index and converts it to the int type
public class CountdownHelper { private const int CacheSlidingExpirationHour = 2; private const string CountdownCurrentIndexCacheName = "OnlineMeetingCountdownCurrentIndex"; private static IAppLog log = (typeof(CountdownHelper)); private static Cache m_cache = ; private static object m_objCountdownCurrentIndexLock = new object(); private static int[] m_snIntervalSecond = new int[] { 0, 1 , 1 , 1}; //1 display 0 not display public CountdownHelper() { } public int GetCountdownAddedSecond() { lock (m_objCountdownCurrentIndexLock) { int nCountdownCurrentIndex = 0; try { object objCountdownCurrentIndex = m_cache[CountdownCurrentIndexCacheName]; if (objCountdownCurrentIndex == null) { //If you need to add cache, use the following //m_cache.Insert(CountdownCurrentIndexCacheName, 1, null, , (CacheSlidingExpirationHour), , null); //Use the following without adding cache m_cache.Insert(CountdownCurrentIndexCacheName, 1, null, , , , 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) { //If you need to record the error log, you can record it here, I have not added it here //("Function introduction GetCountdownAddedSecond:" + __error.Message); if (nCountdownCurrentIndex > m_snIntervalSecond.Length - 1) { nCountdownCurrentIndex = m_snIntervalSecond.Length - 1; } return m_snIntervalSecond[nCountdownCurrentIndex]; } } } }
The requirement of this function is: the business department needs to monitor the exposure rate of the current page, so it needs to use probability to judge how the current exposure code is displayed alternately on the page. At first, the exposure rate was 50%, so the array is directly new int[] { 0, 1}, and later it is changed to 75%, which is the above code. So this way can monitor exposure and control the exposure code.
The front desk call is in AJAX mode:
Description: equal to 1, add the exposure code to the page, otherwise it will not be added
1 <div ></div>
<!--Polling exposure--> $.post("/Topic/GetCountdownAddedSecond", function (data) { if (data) { if ( == 1) { var img_html = "<img src=\"https://d_directed_treatment =?\ment\" style=\"display:none;\">"; $("#adver").html(img_html); } } }, "json");
Thank you for reading, I hope it can help you. Thank you for your support for this site!