在asp.net mvc中安排徽章更新任务流程

时间:2015-02-01 15:00:49

标签: c# asp.net-mvc asp.net-mvc-4

我找到了实施批量更新的代码,如下所示。但是,我不知道如何在asp.net MVC中实现它。据我所知,实现应该在MVC中的global.asax文件中完成。我应该如何在global.asax文件中触发它?

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);
    }
}

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); }
    }
}

1 个答案:

答案 0 :(得分:0)

恕我直言和经验,从网络环境中执行批处理作业或任务是不可能和可取的。始终建议在Web上下文中对数据库进行输入。下一步是使用Windows服务来获取数据和进程,您也可以参考Quartz.net来完成此任务。