IdempotentAttribute.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. using System.Security.Claims;
  10. namespace Admin.NET.Core;
  11. /// <summary>
  12. /// 防止重复请求过滤器特性
  13. /// </summary>
  14. public class IdempotentAttribute : Attribute, IAsyncActionFilter
  15. {
  16. /// <summary>
  17. /// 请求间隔时间/秒
  18. /// </summary>
  19. public int IntervalTime { get; set; } = 5;
  20. /// <summary>
  21. /// 错误提示内容
  22. /// </summary>
  23. public string Message { get; set; } = "你操作频率过快,请稍后重试!";
  24. /// <summary>
  25. /// 自定义缓存: Key+请求路由+用户Id+请求参数
  26. /// </summary>
  27. public string CacheKey { get; set; } = "";
  28. /// <summary>
  29. /// 直接抛出异常Ture,返回上次请求结果False
  30. /// </summary>
  31. public bool ThrowBah { get; set; } = false;
  32. private SysCacheService _sysCacheService { get; set; }
  33. public IdempotentAttribute()
  34. {
  35. _sysCacheService = App.GetService<SysCacheService>();
  36. }
  37. public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  38. {
  39. var httpContext = context.HttpContext;
  40. var path = httpContext.Request.Path.Value.ToString();
  41. var userId = httpContext.User?.FindFirstValue(ClaimConst.UserId);
  42. var cacheExpireTime = TimeSpan.FromSeconds(IntervalTime);
  43. var parameters = "";
  44. foreach (var parameter in context.ActionDescriptor.Parameters)
  45. {
  46. parameters += parameter.Name;
  47. parameters += context.ActionArguments[parameter.Name].ToJson();
  48. }
  49. var cacheKey = MD5Encryption.Encrypt(CacheKey + path + userId + parameters);
  50. if (_sysCacheService.ExistKey(cacheKey))
  51. {
  52. if (ThrowBah) throw Oops.Oh(Message);
  53. try
  54. {
  55. var cachedResult = _sysCacheService.Get<ResponseData>(cacheKey);
  56. context.Result = new ObjectResult(cachedResult.Value);
  57. }
  58. catch (Exception)
  59. {
  60. throw Oops.Oh(Message);
  61. }
  62. }
  63. else
  64. {
  65. // 先加入一个空缓存,防止第一次请求结果没回来导致连续请求
  66. _sysCacheService.Set(cacheKey, "", cacheExpireTime);
  67. var resultContext = await next();
  68. if (resultContext.Result is ObjectResult objectResult)
  69. {
  70. var valueType = objectResult.Value.GetType();
  71. var responseData = new ResponseData
  72. {
  73. Type = valueType.Name,
  74. Value = objectResult.Value
  75. };
  76. _sysCacheService.Set(cacheKey, responseData, cacheExpireTime);
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// 请求结果
  82. /// </summary>
  83. private class ResponseData
  84. {
  85. /// <summary>
  86. /// 结果类型
  87. /// </summary>
  88. public string Type { get; set; }
  89. /// <summary>
  90. /// 请求结果返回的数据
  91. /// </summary>
  92. public dynamic Value { get; set; }
  93. }
  94. }