public class RedisRateLimiter
{
private static Logger LOG = LogManager.GetLogger("redis-limiter"); private static readonly string TIME_KEY = "TIME_KEY";
private static readonly string COUNTER_KEY = "COUNTER_KEY"; public int MaxLimitNum { get; set; } public RedisRateLimiter(int maxLimitNum)
{
this.MaxLimitNum = maxLimitNum;
} public bool CheckLimit()
{
using (var redisClient = RedisUtil.GetClient())
{
if (!redisClient.ContainsKey(TIME_KEY))
{
LOG.Debug("Key Exp");
redisClient.Set<int>(TIME_KEY, 0, TimeSpan.FromSeconds(1));
redisClient.Set<long>(COUNTER_KEY, 0, TimeSpan.FromSeconds(1));
} if (redisClient.ContainsKey(TIME_KEY))
{
var current = redisClient.IncrementValue(COUNTER_KEY);
if (current > MaxLimitNum)
{
LOG.Error("限流成功,当前值: {0}, 限制最大值:{1}", current, MaxLimitNum);
return false;
} }
}
return true;
}
}

C# RedisRateLimiter的更多相关文章

  1. Sentry的安装搭建与使用

    业务监控工具 Sentry 的搭建与使用 官方网址 Django Sentry 官网链接 Sentry 简介 Sentry 是一个开源的实时错误报告工具,支持 web 前后端.移动应用以及游戏,支持 ...

  2. SpringCloud-Gateway

    在微服务架构中,我们会遇到这样的问题:1.在调用微服务时,需要鉴权,微服务不能任意给外部调用.但是,多个微服务如果都需要同一套鉴权规则,明显会产生冗余,如果鉴权方法需要修改,则需要改动多个地方.2.在 ...

  3. Sentry部署

    前期准备 [root@Aaron ~]# uname -r 3.10.0-327.el7.x86_64 [root@Aaron ~]# uname -a Linux Aaron 3.10.0-327. ...

  4. SpringCloud-Greenwich版本新特性探索(1)---SpringCloudGateway

    一.前言 1.SpringCloudGateway是SpringCloud新推出的网关框架,比较于上一代Zuul,功能和性能有很大的提升.Zuul1.x采用的是阻塞多线程方式,也就是一个线程处理一个连 ...

  5. spring cloud gateway - RequestRateLimiter

    1. Official website 5.7 RequestRateLimiter GatewayFilter Factory The RequestRateLimiter GatewayFilte ...

  6. nginx 、springMvc(非分布式)相应的限流、消峰

    互联网服务赖以生存的根本是流量, 产品和运营会经常通过各种方式来为应用倒流,比如淘宝的双十一等,如何让系统在处理高并发的同时还是保证自身系统的稳定, 通常在最短时间内提高并发的做法就是加机器, 但是如 ...

  7. 003-spring cloud gateway-概述、Route模型、网关初始化配置过程、基本原理

    一.概述 网关服务核心是将进入的请求正确合理的路由到下层具体的服务进行业务处理,由此可见网关服务的核心就是路由信息的构建. Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到 ...

  8. 使用springcloud gateway搭建网关(分流,限流,熔断)

    Spring Cloud Gateway Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 ...

  9. Zuul:构建高可用网关之多维度限流

    对请求的目标URL进行限流(例如:某个URL每分钟只允许调用多少次) 对客户端的访问IP进行限流(例如:某个IP每分钟只允许请求多少次) 对某些特定用户或者用户组进行限流(例如:非VIP用户限制每分钟 ...

随机推荐

  1. Delphi APP 開發入門(八)SQLite資料庫

    Delphi APP 開發入門(八)SQLite資料庫 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次 ...

  2. 最值得阅读学习的 10 个 C 语言开源项目代码

    1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连 ...

  3. [HZNUOJ] 博

    Description 定义一个数字序列为“非下降序列”: 此处我们约定用$n\;表示数字序列的长度,下面定义在n \in [1, \infty]时有效$ $if \;\; n = 1:$ $\;\; ...

  4. 在xshell中使用sftp上传文件

    Xshell 5 (Build 1335)Copyright (c) 2002-2017 NetSarang Computer, Inc. All rights reserved. Type `hel ...

  5. Nginx将不同IP的请求分发到不同的WEB服务器

    server { listen ; server_name localhost; large_client_header_buffers 16k; client_max_body_size 300m; ...

  6. react-refetch的使用小例子

    出处:<react设计模式和最佳实践> 作者:米凯莱·贝尔托利 出版时间:2018年8月第1版(还算新) 使用react-refetch来简化api获取数据的代码 const List = ...

  7. golang中文字符编码转换

    golang 有很多需要将中文转成utf8的 网上搜到一个直接转的,记录下,备用 package main import "golang.org/x/text/encoding/simpli ...

  8. APP开放接口API安全性——Token令牌Sign签名的设计与实现

    在APP开放接口API的设计中,避免不了的就是安全性问题. 一.https协议 对于一些敏感的API接口,需要使用https协议.https是在http超文本传输协议加入SSL层,它在网络间通信是加密 ...

  9. LeetCode (236):Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  10. 优盘版Kali

    准备USB镜象 下载Kali linux. 如果你用的是Windows,下载Win32 Disk Imager. *nix类系统不需要额外的软件. 一块U盘(至少 2GB 容量). Kali Linu ...