SORU
29 AĞUSTOS 2008, Cuma


ASP.NET MVC uygulamak isteği azaltma için en iyi yol?

Çeşitli yollar ile bir kullanıcı eylemleri için gaz deney yapacağızsüre verildi:

  • Sınırı/cevap mesajları soru
  • Sınırı düzenler
  • Yem alımları sınırı

Şimdilik, Önbellek basit bir kullanıcı etkinliği kayıt eklemek için kullanıyoruz - eğer bu kaydı kullanıcı aynı etkinlik yok bile varsa, biz gaz.

Önbelleği otomatik olarak bize eski veri ve kullanıcı etkinliği windows temizleme sürgülü verir, ama ölçek nasıl kullanarak bir sorun olabilir.

Ne kullanıcı işlemleri ve isteklerini etkili bir şekilde kısıtlanabilir sağlamanın başka bir yolu (istikrar üzerine vurgu)?

CEVAP
23 AĞUSTOS 2009, Pazar


İşte geçen yıl için Yığın Taşması kullandığımız ne genel bir sürümü:

/// <summary>
/// Decorates any MVC route that needs to have client requests limited by time.
/// </summary>
/// <remarks>
/// Uses the current System.Web.Caching.Cache to store each client request to the decorated route.
/// </remarks>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ThrottleAttribute : ActionFilterAttribute
{
    /// <summary>
    /// A unique name for this Throttle.
    /// </summary>
    /// <remarks>
    /// We'll be inserting a Cache record based on this name and client IP, e.g. "Name-192.168.0.1"
    /// </remarks>
    public string Name { get; set; }

    /// <summary>
    /// The number of seconds clients must wait before executing this decorated route again.
    /// </summary>
    public int Seconds { get; set; }

    /// <summary>
    /// A text message that will be sent to the client upon throttling.  You can include the token {n} to
    /// show this.Seconds in the message, e.g. "Wait {n} seconds before trying again".
    /// </summary>
    public string Message { get; set; }

    public override void OnActionExecuting(ActionExecutingContext c)
    {
        var key = string.Concat(Name, "-", c.HttpContext.Request.UserHostAddress);
        var allowExecute = false;

        if (HttpRuntime.Cache[key] == null)
        {
            HttpRuntime.Cache.Add(key,
                true, // is this the smallest data we can have?
                null, // no dependencies
                DateTime.Now.AddSeconds(Seconds), // absolute expiration
                Cache.NoSlidingExpiration,
                CacheItemPriority.Low,
                null); // no callback

            allowExecute = true;
        }

        if (!allowExecute)
        {
            if (String.IsNullOrEmpty(Message))
                Message = "You may only perform this action every {n} seconds.";

            c.Result = new ContentResult { Content = Message.Replace("{n}", Seconds.ToString()) };
            // see 409 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
            c.HttpContext.Response.StatusCode = (int)HttpStatusCode.Conflict;
        }
    }
}

Örnek kullanım:

[Throttle(Name="TestThrottle", Message = "You must wait {n} seconds before accessing this url again.", Seconds = 5)]
public ActionResult TestThrottle()
{
    return Content("TestThrottle executed");
}

ASP.NET Önbellek bir şampiyon gibi çalışır, bu kullanarak, otomatik gaz girişleri-up temiz. Ve bizim büyüyen trafik, bu sunucuda bir sorun olduğunu görmüyoruz.

Bu yöntem hakkında geri bildirim vermek için çekinmeyin, daha iyi Yığın Taşması yaptığımızda, ** 4 bile daha hızlı :)

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • jedimasterkyle

    jedimasterky

    11 ŞUBAT 2006
  • listedabive

    listedabive

    30 Ocak 2007
  • Warner Bros. UK

    Warner Bros.

    6 HAZİRAN 2008