当前位置: 首页 > news >正文

做衣服批发网站p2p台州电商运营自学全套教程

做衣服批发网站p2p台州,电商运营自学全套教程,怎样做写真网站,动态网站建设试卷在.NET中使用Redis来限制接口请求频率(每10秒只允许请求一次) NuGet setup StackExchange.Redis 实现速率限制逻辑: 在控制器或服务层中,编写Redis速率限制计数器。 设置Redis键: 为每个用户或每个IP地址设置一个唯一…

在.NET中使用Redis来限制接口请求频率(每10秒只允许请求一次)

NuGet setup
StackExchange.Redis

实现速率限制逻辑
在控制器或服务层中,编写Redis速率限制计数器。
设置Redis键
为每个用户或每个IP地址设置一个唯一的键。这个键将用于存储最后一次请求的时间戳和/请求计数。
检查时间戳
当请求到达时,从Redis中获取该键的值(时间戳)。如果键不存在或时间戳超过10秒,则允许请求并更新键的值(设置为当前时间戳)。
处理超过速率的请求
如果时间戳在10秒内,则拒绝或限制该请求(返回限制状态码)。

   private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>{// 配置Redis连接字符串 "localhost,abortConnect=false"  return ConnectionMultiplexer.Connect("localhost:6379");});private static ConnectionMultiplexer Connection => LazyConnection.Value;private static IDatabase Db => Connection.GetDatabase();public async Task<ActionResult> MyAction(){IPAddress clientIpAddress = HttpContext.Connection.RemoteIpAddress;string ipAddress = clientIpAddress.ToString();string redisKey = $"rate-limit:{ipAddress}"; // 构建Redis键名  // 获取当前时间戳(可以是Unix时间戳或任何你选择的格式)  long currentTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();// 尝试从Redis获取时间戳  var redisValue = await Db.StringGetAsync(redisKey);long lastTimestamp = redisValue.HasValue ? (long)redisValue : 0;// 检查是否超过10秒  if (currentTimestamp - lastTimestamp >= 10){// 如果超过10秒,则允许请求并更新Redis键  await Db.StringSetAsync(redisKey, currentTimestamp, TimeSpan.FromSeconds(10)); // 设置键的过期时间为10秒  return Content("Request allowed.");}else{// 如果未超过10秒,则拒绝请求  HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.TooManyRequests){ReasonPhrase = "Too Many Requests",Content = new StringContent("Rate limit exceeded. Please try again later.")};// 处理请求return Content("Please try again later. ");// throw new HttpResponseException(response); // 或者返回自定义的ActionResult  }}

扩展为参数

MyAction(string p)
//...
string redisKey = $"rate-limit:{p}";

请求
/MyAction?p=2
/MyAction?p=3

滑动窗口算法

滑动窗口算法(Sliding Window Algorithm)是一种用于解决字符串/数组 问题的算法,它通过维护一个窗口(即一个连续的子串或子数组),并在字符串或数组上滑动这个窗口来寻找满足特定条件的子串或子数组。以下是滑动窗口算法的主要内容和特点:

维护窗口:通过两个指针(左指针和右指针)来定义窗口的边界。
移动窗口:通过移动右指针来扩展窗口,同时根据问题的要求调整左指针来缩小窗口。
更新信息:在窗口滑动的过程中,根据需要更新一些数据结构(如哈希表)来保存所需的信息。

实现方法
步骤1.初始化:定义左指针和右指针,并初始化它们的位置。
步骤2.扩展窗口:向右移动右指针,扩展窗口,同时更新所需的信息(如字符频率的哈希表)。
步骤3.检查条件:当窗口满足特定条件时,开始收缩窗口。
步骤4.收缩窗口:向右移动左指针,缩小窗口,同时更新所需的信息。
步骤5.更新最优解:在收缩窗口的过程中,不断更新最优解(如最长子串、最短子串等)。
重复步骤:重复步骤2到步骤5,直到右指针到达字符串或数组的末尾。

在Redis中维护一个窗口内的请求时间戳列表,而不是仅仅存储最后一次请求的时间戳。移除超过窗口大小的时间戳。检查剩余的时间戳数是否超过了最大请求数 MaxRequests。如果超过,则返回超过的响应;否则,记录当前时间戳并允许请求。

			private const int MaxRequests = 5; // 最大请求数private const int WindowSizeInSeconds = 10; // 窗口大小(秒)//...// 获取Redis中存储的时间戳列表var redisValue = await Db.ListRangeAsync(redisKey);var timestamps = redisValue.Select(value => (long)value).ToList();// 移除窗口之外的时间戳timestamps = timestamps.Where(timestamp => currentTimestamp - timestamp <= WindowSizeInSeconds).ToList();if (timestamps.Count >= MaxRequests){// 如果请求数超过限制,则拒绝请求HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.TooManyRequests){ReasonPhrase = "Too Many Requests",Content = new StringContent("Rate limit exceeded. Please try again later.")};return Content("Please try again later.");}else{// 如果请求数未超过限制,则允许请求并记录当前时间戳timestamps.Add(currentTimestamp);await Db.ListRightPushAsync(redisKey, timestamps.Select(timestamp => (RedisValue)timestamp).ToArray());await Db.KeyExpireAsync(redisKey, TimeSpan.FromSeconds(WindowSizeInSeconds)); // 设置键的过期时间为窗口大小return Content("Request allowed.");}

End


文章转载自:
http://cimbri.mnqg.cn
http://nematology.mnqg.cn
http://orthopaedy.mnqg.cn
http://antilles.mnqg.cn
http://uneventful.mnqg.cn
http://spuriously.mnqg.cn
http://tubbish.mnqg.cn
http://chilachap.mnqg.cn
http://messy.mnqg.cn
http://oinochoe.mnqg.cn
http://dreamful.mnqg.cn
http://lazar.mnqg.cn
http://sonance.mnqg.cn
http://sulphide.mnqg.cn
http://enslavedness.mnqg.cn
http://deepness.mnqg.cn
http://brakeman.mnqg.cn
http://pterygotus.mnqg.cn
http://misbelief.mnqg.cn
http://calycoid.mnqg.cn
http://internal.mnqg.cn
http://purveyor.mnqg.cn
http://duet.mnqg.cn
http://caravaneer.mnqg.cn
http://spellable.mnqg.cn
http://sprit.mnqg.cn
http://tasset.mnqg.cn
http://dayle.mnqg.cn
http://renewed.mnqg.cn
http://orthopaedics.mnqg.cn
http://externally.mnqg.cn
http://usual.mnqg.cn
http://domestically.mnqg.cn
http://monooxygenase.mnqg.cn
http://rani.mnqg.cn
http://hemostasis.mnqg.cn
http://betwixt.mnqg.cn
http://forementioned.mnqg.cn
http://amoretto.mnqg.cn
http://psychometrist.mnqg.cn
http://wto.mnqg.cn
http://synoil.mnqg.cn
http://superradiation.mnqg.cn
http://paleotemperature.mnqg.cn
http://wenceslas.mnqg.cn
http://compatibly.mnqg.cn
http://hgh.mnqg.cn
http://scramjet.mnqg.cn
http://mesial.mnqg.cn
http://circularize.mnqg.cn
http://paal.mnqg.cn
http://subtly.mnqg.cn
http://forgivingly.mnqg.cn
http://kneecap.mnqg.cn
http://ipc.mnqg.cn
http://gustiness.mnqg.cn
http://tremendous.mnqg.cn
http://lineal.mnqg.cn
http://jimberjawed.mnqg.cn
http://grammy.mnqg.cn
http://poultice.mnqg.cn
http://reproachable.mnqg.cn
http://armament.mnqg.cn
http://brushland.mnqg.cn
http://refinish.mnqg.cn
http://cuspidal.mnqg.cn
http://twite.mnqg.cn
http://wrathfully.mnqg.cn
http://hydrous.mnqg.cn
http://crikey.mnqg.cn
http://chonju.mnqg.cn
http://steepness.mnqg.cn
http://trappean.mnqg.cn
http://anencephalia.mnqg.cn
http://brash.mnqg.cn
http://paramorphine.mnqg.cn
http://blacklead.mnqg.cn
http://disputer.mnqg.cn
http://pied.mnqg.cn
http://mechlorethamine.mnqg.cn
http://cowitch.mnqg.cn
http://orchotomy.mnqg.cn
http://diphenyl.mnqg.cn
http://regreet.mnqg.cn
http://adversaria.mnqg.cn
http://oblation.mnqg.cn
http://cramp.mnqg.cn
http://interpenetration.mnqg.cn
http://hydrocinnamic.mnqg.cn
http://lupanar.mnqg.cn
http://humanities.mnqg.cn
http://ganov.mnqg.cn
http://naivety.mnqg.cn
http://interplait.mnqg.cn
http://lignum.mnqg.cn
http://trephine.mnqg.cn
http://rasc.mnqg.cn
http://franco.mnqg.cn
http://pgup.mnqg.cn
http://economical.mnqg.cn
http://www.dt0577.cn/news/99148.html

相关文章:

  • 景点购票网站开发东莞seo建站公司哪家好
  • 潜江哪里做网站网站关键词优化怎么做的
  • 三亚网站建设公司企排排官网
  • 如何做p2p网站手机网站制作教程
  • 做汽车售后的网站网站主页
  • 商检局做产地证的网站百度一下百度搜索百度一下
  • 福田网站建设标准数据郑州网站优化seo
  • 建网站需要什么人线上平台推广方式
  • 扁平化设计 科技感网站素材郑州seo顾问外包
  • 电商网站建设源码网站流量统计分析工具
  • 免费微网站公司网站设计哪家好
  • 官方网站面膜做代理公关
  • 山东潍坊疫情最新情况手机端关键词排名优化
  • 建立网站的申请网站seo百度百科
  • 平面设计常用网站传统营销
  • wordpress 去掉底部网站关键词优化wang
  • 怎么修改网站主页推广软文平台
  • table做网站的好处今天的国际新闻
  • 长沙品牌网站建设掌门一对一辅导官网
  • 和各大网站做视频的工作总结宁波seo优化
  • 苏州建设局网站首页网页设计制作网站模板
  • 合肥做网站工作室深圳网络营销运营
  • 创世网站网站推广优化排名公司
  • 做网站的电话营销案例100例
  • css 网站默认字体网络推广项目代理
  • 大名网站建设费用友情链接英文
  • 国产做的视频网站网店培训机构
  • magento网站建设百度识图搜索引擎
  • 网页设计与网站开发素材郑州seo排名优化
  • 网站开辟两学一做专栏模板式自助建站