ApiAccountCore.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. namespace molilian.core
  11. {
  12. public class ApiAccountDTO
  13. {
  14. public int id { get; set; }
  15. public string name { get; set; } = string.Empty;
  16. public string api_key { get; set; } = string.Empty;
  17. public string api_secret { get; set; } = string.Empty;
  18. public string description { get; set; } = string.Empty;
  19. public DateTime create_time { get; set; }
  20. public DateTime last_time { get; set; }
  21. public bool status { get; set; }
  22. public int interval { get; set; }
  23. public bool enable_report { get; set; } = false;
  24. }
  25. public partial class ApiAccountCore
  26. {
  27. private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  28. private static IEnumerable<ApiAccountDTO> _cached;
  29. public static async Task<ApiAccountDTO> GetOneAsync(string api_key)
  30. {
  31. var list = await ListAsync();
  32. if (!list.Any()) return null;
  33. return list.Where(e => e.api_key == api_key).FirstOrDefault();
  34. }
  35. //public static async Task<IEnumerable<ApiAccountDTO>> ListAsync(bool force = false)
  36. //{
  37. // if (!force && _cached != null) return _cached;
  38. // string cache_key = $"cache:api_key";
  39. // var list = await RedisHelper.GetAsync<IEnumerable<ApiAccountDTO>>(cache_key);
  40. // if (force || list == null)
  41. // {
  42. // lock (_lockObj)
  43. // {
  44. // list = new DBContext.Table("api_account").Select<ApiAccountDTO>();
  45. // if (list == null) return default;
  46. // _ = RedisHelper.SetAsync(cache_key, list, 30 * 86400);
  47. // }
  48. // }
  49. // _cached = list;
  50. // return list;
  51. //}
  52. public static async Task<IEnumerable<ApiAccountDTO>> ListAsync(bool force = false)
  53. {
  54. if (!force && _cached != null) return _cached;
  55. try
  56. {
  57. await _semaphore.WaitAsync();
  58. string cache_key = $"cache:api_key";
  59. var list = await RedisHelper.GetAsync<IEnumerable<ApiAccountDTO>>(cache_key);
  60. if (force || list == null)
  61. {
  62. list = new DBContext.Table("api_account").Select<ApiAccountDTO>();
  63. if (list == null) return default;
  64. // 等待 Redis 写入完成
  65. await RedisHelper.SetAsync(cache_key, list, 30 * 86400);
  66. }
  67. _cached = list;
  68. return list;
  69. }
  70. catch (Exception)
  71. {
  72. // 发生异常时返回上一次的缓存,如果没有则返回默认值
  73. return _cached ?? default;
  74. }
  75. finally
  76. {
  77. _semaphore.Release();
  78. }
  79. }
  80. public static void Refresh()
  81. {
  82. _ = ListAsync(true);
  83. }
  84. public static void Disabled(string name)
  85. {
  86. string cache_key = $"cache:api_key:{name}:disabled";
  87. long count = RedisHelper.IncrBy(cache_key);
  88. RedisHelper.Expire(cache_key, 10);
  89. if (count > 1) return;
  90. new DBContext.Table("api_account")
  91. .Add("status", 0)
  92. .Where("name=@name", new { name })
  93. .Update();
  94. _ = ListAsync(true);
  95. EndPointCore.NotifyReload();
  96. }
  97. }
  98. }