Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A similar-to-Stackoverflow implementation is actually a lot simpler than you have described, based on bits of info dropped by the team every once in awhile.</p> <p>In the database, you simply store a collection of <code>BadgeID</code>-<code>UserID</code> pairs to track who has what (and a count or a rowID to allow multiple awards for some badges).</p> <p>In the application, there is a worker object for each badge type. The object is in cache, and when the cache expires, the worker runs its own logic for determining who should get the badge and making the updates, and then it re-inserts itself into the cache:</p> <pre><code>public abstract class BadgeJob { protected BadgeJob() { //start cycling on initialization Insert(); } //override to provide specific badge logic protected abstract void AwardBadges(); //how long to wait between iterations protected abstract TimeSpan Interval { get; } private void Callback(string key, object value, CacheItemRemovedReason reason) { if (reason == CacheItemRemovedReason.Expired) { this.AwardBadges(); this.Insert(); } } private void Insert() { HttpRuntime.Cache.Add(this.GetType().ToString(), this, null, Cache.NoAbsoluteExpiration, this.Interval, CacheItemPriority.Normal, this.Callback); } } </code></pre> <p>And a concrete implementation:</p> <pre><code>public class CommenterBadge : BadgeJob { public CommenterBadge() : base() { } protected override void AwardBadges() { //select all users who have more than x comments //and dont have the commenter badge //add badges } //run every 10 minutes protected override TimeSpan Interval { get { return new TimeSpan(0,10,0); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload