PangolinPoolCore.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. _cached = null;
  48. _ = List(true);
  49. }
  50. public static void Disabled(string name)
  51. {
  52. string cache_key = $"cache:pangolin_pool:{name}:disabled";
  53. long count = RedisHelper.IncrBy(cache_key);
  54. RedisHelper.Expire(cache_key, 10);
  55. if (count > 1) return;
  56. new DBContext.Table("pangolin_pool")
  57. .Add("status", 0)
  58. .Where("name=@name", new { name })
  59. .Update();
  60. _ = List(true);
  61. NotifyCore.Notify(new NifyMessage
  62. {
  63. message = $"【穿山甲:{name}】调用异常",
  64. priority = NifyMessagePriority.high,
  65. tags = ["red_circle"]
  66. });
  67. }
  68. }
  69. }