VeapiPoolCore.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. namespace molilian.core
  10. {
  11. public partial class OtherApiPoolCore
  12. {
  13. private static readonly object _lockObj = new();
  14. private static IEnumerable<OtherApiPoolDTO> _cached;
  15. public static OtherApiPoolDTO? GetOne()
  16. {
  17. var list = List();
  18. if (!list.Any()) return null;
  19. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  20. }
  21. public static OtherApiPoolDTO? Get(int id)
  22. {
  23. var list = List();
  24. if (!list.Any()) return null;
  25. return list.Where(o => o.id == id).FirstOrDefault();
  26. }
  27. public static IEnumerable<OtherApiPoolDTO> List(bool force = false)
  28. {
  29. if (!force && _cached != null) return _cached;
  30. string cache_key = $"cache:other_api_pool";
  31. var list = RedisHelper.Get<IEnumerable<OtherApiPoolDTO>>(cache_key);
  32. if (force || list == null)
  33. {
  34. lock (_lockObj)
  35. {
  36. list = new DBContext.Table("other_api_pool")
  37. .Where("status=@status", new { status = 1 })
  38. .Select<OtherApiPoolDTO>();
  39. if (list == null) return default;
  40. RedisHelper.Set(cache_key, list, 30 * 86400);
  41. }
  42. }
  43. _cached = list;
  44. return list;
  45. }
  46. public static void Refresh()
  47. {
  48. _ = List(true);
  49. }
  50. public static void Disabled(string name)
  51. {
  52. string cache_key = $"cache:other_api_pool:{name}:disabled";
  53. long count = RedisHelper.IncrBy(cache_key);
  54. if (count > 0) return;
  55. RedisHelper.Expire(cache_key, 10);
  56. new DBContext.Table("other_api_pool")
  57. .Add("status", 0)
  58. .Where("name=@name", new { name })
  59. .Update();
  60. _ = List(true);
  61. NotifyCore.Notify(new NifyMessage
  62. {
  63. message = $"【veapi:{name}】cookie 掉线",
  64. priority = NifyMessagePriority.high,
  65. tags = ["red_circle"]
  66. });
  67. NotifyCore.AnPushNotify("掉线", $"【第三方接口:{name}】cookie 掉线");
  68. }
  69. }
  70. }