ApiAccountCore.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. using System.Xml.Linq;
  11. using System.Data;
  12. using YunhuiKit;
  13. namespace molilian.core
  14. {
  15. public partial class ApiAccountCore
  16. {
  17. private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  18. private static IEnumerable<ApiAccountDTO> _cached;
  19. public static async Task<ApiAccountDTO> GetOneAsync(string api_key)
  20. {
  21. var list = await ListAsync();
  22. if (!list.Any()) return null;
  23. return list.Where(e => e.api_key == api_key).FirstOrDefault();
  24. }
  25. //public static async Task<IEnumerable<ApiAccountDTO>> ListAsync(bool force = false)
  26. //{
  27. // if (!force && _cached != null) return _cached;
  28. // string cache_key = $"cache:api_key";
  29. // var list = await RedisHelper.GetAsync<IEnumerable<ApiAccountDTO>>(cache_key);
  30. // if (force || list == null)
  31. // {
  32. // lock (_lockObj)
  33. // {
  34. // list = new DBContext.Table("api_account").Select<ApiAccountDTO>();
  35. // if (list == null) return default;
  36. // _ = RedisHelper.SetAsync(cache_key, list, 30 * 86400);
  37. // }
  38. // }
  39. // _cached = list;
  40. // return list;
  41. //}
  42. public static async Task<IEnumerable<ApiAccountDTO>> ListAsync(bool force = false)
  43. {
  44. if (!force && _cached != null) return _cached;
  45. try
  46. {
  47. await _semaphore.WaitAsync();
  48. string cache_key = $"cache:api_key";
  49. var list = await RedisHelper.GetAsync<IEnumerable<ApiAccountDTO>>(cache_key);
  50. if (force || list == null)
  51. {
  52. list = new DBContext.Table("api_account").Select<ApiAccountDTO>();
  53. if (list == null) return default;
  54. // 等待 Redis 写入完成
  55. await RedisHelper.SetAsync(cache_key, list, 30 * 86400);
  56. }
  57. _cached = list;
  58. return list;
  59. }
  60. catch (Exception)
  61. {
  62. // 发生异常时返回上一次的缓存,如果没有则返回默认值
  63. return _cached ?? default;
  64. }
  65. finally
  66. {
  67. _semaphore.Release();
  68. }
  69. }
  70. public static void Refresh()
  71. {
  72. _ = ListAsync(true);
  73. }
  74. public static void Disabled(string name)
  75. {
  76. string cache_key = $"cache:api_key:{name}:disabled";
  77. long count = RedisHelper.IncrBy(cache_key);
  78. RedisHelper.Expire(cache_key, 10);
  79. if (count > 1) return;
  80. new DBContext.Table("api_account")
  81. .Add("status", 0)
  82. .Where("name=@name", new { name })
  83. .Update();
  84. _ = ListAsync(true);
  85. EndPointCore.NotifyReload();
  86. }
  87. public static int Update(ApiAccountDTO data, IDbConnection conn)
  88. {
  89. var result = (int)conn.Update<ApiAccountDTO>(data, new { data.id });
  90. _ = ListAsync(true);
  91. #if DEBUG
  92. #else
  93. EndPointCore.NotifyReload();
  94. #endif
  95. return result;
  96. }
  97. public static int Create(ApiAccountDTO data, IDbConnection conn)
  98. {
  99. var result = (int)conn.Insert(data);
  100. _ = ListAsync(true);
  101. #if DEBUG
  102. #else
  103. EndPointCore.NotifyReload();
  104. #endif
  105. return result;
  106. }
  107. }
  108. }