C #-round robin algorithm,
During the past two days, the business has a special requirement. When a user accesses the page, the user can control a line of code and display it according to the probability, I am dealing with the exposure of the current page. The exposure code is from a third party. If there is any code on the page, this exposure code is executed, so I wrote a method for this round robin. This method can be modified as needed. I will post all the methods below:
CacheSlidingExpirationHour: time, cache time 2 hours
CountdownCurrentIndexCacheName: cache name
Log: log
M_objCountdownCurrentIndexLock: Current object
M_snIntervalSecond: defines an array, which can be considered as a probability value.
Note: There are 4 numbers in the 100%, 25% data, and we set the total probability to 75%, each representing. So now I set the current probability.
If the cache is a data index, the index is also obtained. The method returns the index and converts it to the int type.
1 public class CountdownHelper 2 {3 private const int CacheSlidingExpirationHour = 2; 4 private const string CountdownCurrentIndexCacheName = "OnlineMeetingCountdownCurrentIndex"; 5 private static IAppLog log = AppLoggerManager. getLogger (typeof (CountdownHelper); 6 private static Cache m_cache = HttpContext. current. cache; 7 private static object m_objCountdownCurrentIndexLock = new object (); 8 private static int [] m_snIntervalSecond = new int [] {0, 1, 1, 1 }; // 1 show 0 do not show 9 10 public CountdownHelper () 11 {12} 13 14 public int GetCountdownAddedSecond () 15 {16 lock (m_objCountdownCurrentIndexLock) 17 {18 int nCountdownCurrentIndex = 0; 19 20 try21 {22 object objCountdownCurrentIndex = m_cache [CountdownCurrentIndexCacheName]; 23 if (objCountdownCurrentIndex = null) 24 {25 // if cache is required, use the following 26 // m_cache.Insert (CountdownCurrentIndexCacheName, 1, null, Cache. noAbsoluteExpiration, TimeSpan. fromHours (CacheSlidingExpirationHour), CacheItemPriority. notRemovable, null); 27 // use the following 28 m_cache.Insert (CountdownCurrentIndexCacheName, 1, null, Cache without caching. noAbsoluteExpiration, Cache. noSlidingExpiration, CacheItemPriority. notRemovable, null); 29} 30 else31 {32 nCountdownCurrentIndex = (int) then; 33 34 if (nCountdownCurrentIndex = m_snIntervalSecond.Length-1) 35 {36 m_cache [CountdownCurrentIndexCacheName] = 0; 37} 38 else39 {40 m_cache [CountdownCurrentIndexCacheName] = nCountdownCurrentIndex + 1; 41} 42} 43 44 return m_snIntervalSecond [nCountdownCurrentIndex]; 45} 46 catch (Exception _ error) 47 {48 // if you need to record the error log, you can record it here. I did not add 49 // log here. error ("function introduction GetCountdownAddedSecond:" + _ error. message); 50 if (nCountdownCurrentIndex> callback-1) 51 {52 nCountdownCurrentIndex = m_snIntervalSecond.Length-1; 53} 54 return m_snIntervalSecond [nCountdownCurrentIndex]; 55} 56} 57} 58 59}
The demand for this function is: business departments need to monitor the impression of the current page, so we need to use probability to determine how the current exposure code is displayed on the page alternately, at first the impression rate is 50%, therefore, the new int [] {0, 1} is directly in the array, and then changed to 75%, which is the code above. Therefore, you can monitor the exposure and control the exposure code.
The front-end call uses AJAX:
Note: equal to 1: add the exposure code to the page; otherwise, no
1 <div id="adver"></div>
1 <! -- Round-robin exposure --> 2 $. post ("/Topic/GetCountdownAddedSecond", function (data) {3 if (data) {4 if (data. num = 1) {5 var img_html = "
Copyright statement: Welcome to reprint, but for my hard work, please indicate the source: http://www.cnblogs.com/zhangpengnike/p/5546046.html (not allowed for commercial purposes !)