public class FrequencyControler {
///
/// 访问控制器名称,用于区分其它控制器,支持多个控制器
///
private string Name { get; set; }
///
/// 限定时长
///
private int Seconds { get; set; }
///
/// 限定次数
///
private int Times { get; set; }
public readonly int MAX_TIMES = 100;
#region 私有方法
private string SessionNameDatelist {
get { return String.Format("fc.{0}.datelist", Name); }
}
private string SessionNameDatepos {
get { return String.Format("fc.{0}.datepos", Name); }
}
///
/// 取得用于保存每次访问时间点的数组(做队列用)
///
///
private long[] GetDateList() {
if (HttpContext.Current.Session[SessionNameDatelist] == null) {
HttpContext.Current.Session[SessionNameDatelist] = new long[MAX_TIMES];
}
return (long[])HttpContext.Current.Session[SessionNameDatelist];
}
///
/// 获取时间记录位置,相当于当前队列位置
///
///
private int GetDatepos() {
if (HttpContext.Current.Session[SessionNameDatepos] == null) {
HttpContext.Current.Session[SessionNameDatepos] = 0;
}
return (int)HttpContext.Current.Session[SessionNameDatepos];
}
///
/// 设置时间记录位置,相当于当前队列位置
///
///
private void SetDatepos(int pos) {
HttpContext.Current.Session[SessionNameDatepos] = pos;
}
#endregion
///
/// 构造访问检测器,限定指定时间内最多请求次数
///
/// 名称
/// 限定时间范围(秒数)
/// 限定次数
public FrequencyControler(string name, int seconds, int times) {
Name = name;
Seconds = seconds;
Times = times;
if (times > MAX_TIMES) throw new Exception("限定次数设置值超出上限!");
}
///
/// 记录一次访问,在时间点数组的下一个位置(按最大长度循环覆盖存储)
///
pub
希望能帮到你