IdempotentAttribute.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using System.Security.Claims;
  7. namespace Admin.NET.Core;
  8. /// <summary>
  9. /// 防止重复请求过滤器特性
  10. /// </summary>
  11. [SuppressSniffer]
  12. [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
  13. public class IdempotentAttribute : Attribute, IAsyncActionFilter
  14. {
  15. /// <summary>
  16. /// 请求间隔时间/秒
  17. /// </summary>
  18. public int IntervalTime { get; set; } = 5;
  19. /// <summary>
  20. /// 错误提示内容
  21. /// </summary>
  22. public string Message { get; set; } = "你操作频率过快,请稍后重试!";
  23. /// <summary>
  24. /// 缓存前缀: Key+请求路由+用户Id+请求参数
  25. /// </summary>
  26. public string CacheKey { get; set; }
  27. /// <summary>
  28. /// 是否直接抛出异常:Ture是,False返回上次请求结果
  29. /// </summary>
  30. public bool ThrowBah { get; set; }
  31. /// <summary>
  32. /// 缓存服务
  33. /// </summary>
  34. private readonly SysCacheService _sysCacheService = App.GetRequiredService<SysCacheService>();
  35. public IdempotentAttribute()
  36. {
  37. }
  38. public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  39. {
  40. var httpContext = context.HttpContext;
  41. var path = httpContext.Request.Path.Value.ToString();
  42. var userId = httpContext.User?.FindFirstValue(ClaimConst.UserId);
  43. var cacheExpireTime = TimeSpan.FromSeconds(IntervalTime);
  44. var parameters = "";
  45. foreach (var parameter in context.ActionDescriptor.Parameters)
  46. {
  47. parameters += parameter.Name;
  48. parameters += context.ActionArguments[parameter.Name].ToJson();
  49. }
  50. var cacheKey = MD5Encryption.Encrypt($"{CacheKey}{path}{userId}{parameters}");
  51. if (_sysCacheService.ExistKey(cacheKey))
  52. {
  53. if (ThrowBah) throw Oops.Oh(Message);
  54. try
  55. {
  56. var cachedResult = _sysCacheService.Get<ResponseData>(cacheKey);
  57. context.Result = new ObjectResult(cachedResult.Value);
  58. }
  59. catch (Exception ex)
  60. {
  61. throw Oops.Oh($"{Message}-{ex}");
  62. }
  63. }
  64. else
  65. {
  66. // 先加入一个空缓存,防止第一次请求结果没回来导致连续请求
  67. _sysCacheService.Set(cacheKey, "", cacheExpireTime);
  68. var resultContext = await next();
  69. if (resultContext.Result is ObjectResult objectResult)
  70. {
  71. var valueType = objectResult.Value.GetType();
  72. var responseData = new ResponseData
  73. {
  74. Type = valueType.Name,
  75. Value = objectResult.Value
  76. };
  77. _sysCacheService.Set(cacheKey, responseData, cacheExpireTime);
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 请求结果数据
  83. /// </summary>
  84. private class ResponseData
  85. {
  86. /// <summary>
  87. /// 结果类型
  88. /// </summary>
  89. public string Type { get; set; }
  90. /// <summary>
  91. /// 请求结果
  92. /// </summary>
  93. public dynamic Value { get; set; }
  94. }
  95. }