c Implementation Polling Algorithm Example Code

  • 2021-11-13 02:27:46
  • OfStack

c # polling algorithm

Doing things these two days, There is a special need in business, When the user accesses the page, Control for a 1-line code, According to the probability to display, what I do is to deal with the exposure of the current page, and the exposure code is the third party. As long as there is this code on the page, even if this exposure code is executed, I wrote a method of this polling. This method can be modified according to my own needs. I post all this method below:

CacheSlidingExpirationHour: Time, cache time 2 hours

CountdownCurrentIndexCacheName: Cache name

log: Log

m_objCountdownCurrentIndexLock:: Current object

m_snIntervalSecond: Defines an array that can be regarded as a probability value

Explanation: 0, 1, 1, 1 data stored in 4 numbers, we set the total probability is 100%, each represents 25%, so now I set the current probability is 75%

What is stored in the cache is the index of the data, and when it is taken, the index is also taken. The method returns the index and turns it into int type


 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 Display  0 Do 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, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(CacheSlidingExpirationHour), CacheItemPriority.NotRemovable, null);
             // Without caching, use the following 
             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)
         {
           // If you need to record the error log, you can record it here. I didn't add it here 
           //log.Error(" Function 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 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 is 50%, so new int [] {0, 1} is directly in the array, and then it is changed to 75%, which is the above code, so it can monitor the exposure and control the exposure code.

The foreground call is in AJAX mode:

Explanation: Equal to 1, add the exposure code to the page, otherwise do not add it


1 <div id="adver"></div>

<!-- Polling exposure -->
   $.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");

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: