PangolinPoolCore.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 Google.Protobuf.WellKnownTypes;
  11. using System.Diagnostics;
  12. using System.Text.Json;
  13. using TencentCloud.Tcm.V20210413.Models;
  14. namespace molilian.core
  15. {
  16. public partial class PangolinPoolCore
  17. {
  18. private static readonly object _lockObj = new();
  19. private static IEnumerable<PangolinDTO> _cached;
  20. public static PangolinDTO? GetOne()
  21. {
  22. var list = List();
  23. if (!list.Any()) return null;
  24. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  25. }
  26. public static IEnumerable<PangolinDTO> List(bool force = false)
  27. {
  28. if (!force && _cached != null) return _cached;
  29. string cache_key = $"cache:pangolin_pool";
  30. var list = RedisHelper.Get<IEnumerable<PangolinDTO>>(cache_key);
  31. if (force || list == null)
  32. {
  33. lock (_lockObj)
  34. {
  35. list = new DBContext.Table("pangolin_pool")
  36. .Where("status=@status", new { status = 1 })
  37. .Select<PangolinDTO>();
  38. if (list == null) return default;
  39. RedisHelper.Set(cache_key, list, 30 * 86400);
  40. }
  41. }
  42. _cached = list;
  43. return list;
  44. }
  45. public static void Refresh()
  46. {
  47. _ = List(true);
  48. }
  49. public static void Disabled(string name)
  50. {
  51. string cache_key = $"cache:pangolin_pool:{name}:disabled";
  52. long count = RedisHelper.IncrBy(cache_key);
  53. RedisHelper.Expire(cache_key, 10);
  54. if (count > 1) return;
  55. new DBContext.Table("pangolin_pool")
  56. .Add("status", 0)
  57. .Where("name=@name", new { name })
  58. .Update();
  59. _ = List(true);
  60. NotifyCore.Notify(new NifyMessage
  61. {
  62. message = $"【穿山甲:{name}】调用异常",
  63. priority = NifyMessagePriority.high,
  64. tags = ["red_circle"]
  65. });
  66. }
  67. }
  68. }