/// <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;
}
}
}
[Throttle(Name="TestThrottle", Message = "You must wait {n} seconds before accessing this url again.", Seconds = 5)]
public ActionResult TestThrottle()
{
return Content("TestThrottle executed");
}

ThrottleAttribute的更多相关文章

随机推荐

  1. jenkins 使用oclint 扫描 oc 代码

    jenkins 环境的搭建,在这里不在赘述,下面我们写一写,如何接入oclint. OCLint是一个强大的静态代码分析工具,可以用来提高代码质量,查找潜在的bug,主要针对c,c++和Objecti ...

  2. win10下 homestead 安装

    1.安装VirtualBox 和 Vagrant 2.git或者composer安装 homestead git clone https://github.com/laravel/homestead. ...

  3. CentOS 6 minimal 网络配置

    安装CentOS6 minimal 之后ifconfig 只有lo本地 的127.0.0.1 没有局域网ip. 这边我用的是vmware  nat  DHCP 提供网络服务.过程就不赘述了. 解决笔记 ...

  4. mySql中IFNULL的使用说明

    IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值 具体用法如:现有学生表(tbl_stu ...

  5. 用JS实现的类似QQ密码的输入特效

    <input ID="_PSD"></input> <script> function passwordText(ID,pd,mark){ ma ...

  6. redis入门笔记(2)

    redis入门笔记(2) 上篇文章介绍了redis的基本情况和支持的数据类型,本篇文章将介绍redis持久化.主从复制.简单的事务支持及发布订阅功能. 持久化 •redis是一个支持持久化的内存数据库 ...

  7. 我们还是太NAive

    蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡蛤鸡

  8. VS安装及单元测试

    VS安装过程: 最大子数组算法已提交至coding.net: https://git.coding.net/CoCoBeer/Maxsubaray.git 用例编号 用例描述 输入数据 预期输出数据 ...

  9. Android Saving Data(二)

    Saving File android读写文件的形式和普通的java IO的方式并没有什么不同,唯一有所限制的是当我们创建文件的时候不能够在像javaSE那样随意了.一般android读写文件有两种形 ...

  10. Android ui 测试课堂笔记

    开始接触Android ui测试了,笔记如下 模拟器 Genemotion , the fastest android simulator in the world Android ui 测试工具 S ...