DeeplinkParseRuleCore.cs 2.1 KB

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