ConfigCore.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 System.Xml.Linq;
  10. namespace molilian.core
  11. {
  12. public partial class TkConfigCore
  13. {
  14. private static readonly object _lockObj = new();
  15. private static TkConfigDTO _cached;
  16. public static TkConfigDTO Get(bool force = false)
  17. {
  18. if (!force && _cached != null) return _cached;
  19. string cache_key = $"cache:tk_config";
  20. var item = CenterHub.Redis.Get<TkConfigDTO>(cache_key);
  21. if (force || item == null)
  22. {
  23. lock (_lockObj)
  24. {
  25. using var conn = CenterHub.GetOpenConnection();
  26. item = new DBContext.Table(conn, "tk_config").Get<TkConfigDTO>("id>0", new { });
  27. if (item == null) return default;
  28. CenterHub.Redis.Set(cache_key, item, 30 * 86400);
  29. }
  30. }
  31. _cached = item;
  32. return item;
  33. }
  34. public static void Refresh()
  35. {
  36. _ = Get(true);
  37. }
  38. }
  39. }