VeapiPoolCore.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 VeapiPoolCore
  12. {
  13. private static readonly object _lockObj = new();
  14. private static IEnumerable<VeapiPoolDTO> _cached;
  15. public static VeapiPoolDTO? GetOne()
  16. {
  17. var list = List();
  18. if (!list.Any()) return null;
  19. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  20. }
  21. public static VeapiPoolDTO? 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<VeapiPoolDTO> List(bool force = false)
  28. {
  29. if (!force && _cached != null) return _cached;
  30. string cache_key = $"cache:veapi_pool";
  31. var list = RedisHelper.Get<IEnumerable<VeapiPoolDTO>>(cache_key);
  32. if (force || list == null)
  33. {
  34. lock (_lockObj)
  35. {
  36. list = new DBContext.Table("veapi_pool")
  37. .Where("status=@status", new { status = 1 })
  38. .Select<VeapiPoolDTO>();
  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. _cached = null;
  49. _ = List(true);
  50. }
  51. public static void Disabled(string name)
  52. {
  53. string cache_key = $"cache:veapi_pool:{name}:disabled";
  54. long count = RedisHelper.IncrBy(cache_key);
  55. if (count > 0) return;
  56. RedisHelper.Expire(cache_key, 10);
  57. new DBContext.Table("veapi_pool")
  58. .Add("status", 0)
  59. .Where("name=@name", new { name })
  60. .Update();
  61. _ = List(true);
  62. NotifyCore.Notify(new NifyMessage
  63. {
  64. message = $"【veapi:{name}】cookie 掉线",
  65. priority = NifyMessagePriority.high,
  66. tags = ["red_circle"]
  67. });
  68. NotifyCore.AnPushNotify("掉线", $"【veapi:{name}】cookie 掉线");
  69. }
  70. }
  71. }