Beispiel #1
0
        //防止恶意请求
        public static bool IsRedis(HttpContext context, string key)
        {
            if (context.Request.Browser.Crawler)
            {
                return(false);
            }
            if (RedisCacheTools.Exists(key))
            {
                string keycount = "keycount:" + key;
                bool   check    = RedisCacheTools.Exists(keycount);
                if (check)
                {
                    RedisCacheTools.Incr(keycount);
                    int hit = RedisCacheTools.Get <int>(keycount);
                    if (hit > 700000)
                    {
                        return(false);
                    }
                }
                else
                {
                    DateTime dt = DateTime.Now.Date.AddDays(1);
                    RedisCacheTools.Incr(keycount);

                    RedisCacheTools.Expire(keycount, dt);
                }
            }
            else
            {
                return(false);
            }
            return(true);
        }
        //防止恶意请求
        public static bool IsRedis(HttpContext context)
        {
            if (context.Request.Browser.Crawler)
            {
                return(false);
            }
            string key   = userIP;
            bool   check = RedisCacheTools.Exists(key);

            if (check)
            {
                RedisCacheTools.Incr(key);
                int hit = RedisCacheTools.Get <int>(key);
                if (hit > 16)
                {
                    return(false);
                }

                /*
                 *  $redis->incr($key);
                 *  $count = $redis->get($key);
                 *  if($count > 5){
                 *      exit('请求太频繁,请稍后再试!');
                 *  }
                 */
            }
            else
            {
                DateTime dt = DateTime.Now.AddDays(1);
                RedisCacheTools.Incr(key);

                /*
                 *  $redis->incr($key);
                 *      //限制时间为60秒
                 *      $redis->expire($key,60)
                 */
                RedisCacheTools.Expire(key, dt);
            }

            return(true);
        }