JdPoolCore.cs 2.1 KB

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