DeeplinkParseRuleCore.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 
  2. using dodohold.core;
  3. namespace molilian.core
  4. {
  5. public partial class DeeplinkParseRuleCore
  6. {
  7. private static string _end_point;
  8. private static Dictionary<string, int> _calls = new();
  9. static DeeplinkParseRuleCore()
  10. {
  11. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  12. }
  13. private static readonly object _lockObj = new();
  14. private static IEnumerable<DeeplinkParseRuleDTO> _cached;
  15. public static DeeplinkParseRuleDTO? GetOne(string channel)
  16. {
  17. #if DEBUG
  18. var list = List(true);
  19. #else
  20. var list = List();
  21. #endif
  22. if (!list.Any()) return null;
  23. return list.Where(e => channel.Equals(e.channel_name)).FirstOrDefault();
  24. }
  25. public static IEnumerable<DeeplinkParseRuleDTO> List(bool force = false)
  26. {
  27. if (!force && _cached != null) return _cached;
  28. string cache_key = $"cache:deeplink_parse_rule";
  29. var list = RedisHelper.Get<IEnumerable<DeeplinkParseRuleDTO>>(cache_key);
  30. if (force || list == null)
  31. {
  32. lock (_lockObj)
  33. {
  34. list = new DBContext.Table("deeplink_parse_rule")
  35. .Where("status=@status", new { status = 1 })
  36. .Select<DeeplinkParseRuleDTO>();
  37. if (list == null) return default;
  38. RedisHelper.Set(cache_key, list, 30 * 86400);
  39. }
  40. }
  41. _cached = list;
  42. return list;
  43. }
  44. public static void Refresh()
  45. {
  46. _ = List(true);
  47. }
  48. public static async Task<int> Update(DeeplinkParseRuleDTO rule)
  49. {
  50. var conn = DBContext.GetOpenConnection();
  51. return await conn.UpdateAsync<DeeplinkParseRuleDTO>(rule);
  52. }
  53. }
  54. }