TenantProvider.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package jnpf.util;
  2. import cn.dev33.satoken.session.SaSession;
  3. import cn.dev33.satoken.session.SaSessionCustomUtil;
  4. import jnpf.model.BaseSystemInfo;
  5. import lombok.extern.slf4j.Slf4j;
  6. import static jnpf.consts.AuthConsts.DEF_TENANT_ID;
  7. import static jnpf.consts.AuthConsts.TENANT_SESSION;
  8. /**
  9. * @author JNPF开发平台组
  10. * @copyright 引迈信息技术有限公司
  11. */
  12. @Slf4j
  13. public class TenantProvider {
  14. private static final long tenantTimeout = 60 * 60 * 24 * 30L;
  15. /**
  16. * 获取租户Redis存储对象
  17. *
  18. * @param tenantId
  19. * @return
  20. */
  21. public static SaSession getTenantSession(String tenantId) {
  22. if (tenantId == null) {
  23. // tenantId = TenantHolder.getDatasourceId();
  24. // if (tenantId == null) {
  25. tenantId = DEF_TENANT_ID;
  26. // }
  27. }
  28. SaSession saSession = SaSessionCustomUtil.getSessionById(TENANT_SESSION + tenantId);
  29. if (saSession != null && !saSession.get("init", false)) {
  30. saSession.set("init", true);
  31. saSession.updateTimeout(tenantTimeout);
  32. }
  33. return saSession;
  34. }
  35. /**
  36. * 存入租户缓存空间
  37. *
  38. * @param tenantId
  39. * @param key
  40. * @param value
  41. */
  42. public static void putTenantCache(String tenantId, String key, Object value) {
  43. SaSession saSession = getTenantSession(tenantId);
  44. if (saSession != null) {
  45. saSession.set(key, value).updateTimeout(tenantTimeout);
  46. }
  47. }
  48. /**
  49. * 获取租户缓存数据
  50. *
  51. * @param tenantId
  52. * @param key
  53. * @param <T>
  54. * @return
  55. */
  56. public static <T> T getTenantCache(String tenantId, String key) {
  57. SaSession saSession = getTenantSession(tenantId);
  58. if (saSession != null) {
  59. return (T) saSession.get(key);
  60. }
  61. return null;
  62. }
  63. /**
  64. * 删除租户缓存数据
  65. *
  66. * @param tenantId
  67. * @param key
  68. */
  69. public static void delTenantCache(String tenantId, String key) {
  70. SaSession saSession = getTenantSession(tenantId);
  71. if (saSession != null) {
  72. saSession.delete(key);
  73. }
  74. }
  75. public static void renewTimeout(String tenantId, long timeout) {
  76. if (tenantId == null) {
  77. tenantId = DEF_TENANT_ID;
  78. }
  79. SaSession saSession = getTenantSession(tenantId);
  80. if (saSession != null) {
  81. saSession.updateTimeout(timeout);
  82. }
  83. }
  84. private static ThreadLocal<BaseSystemInfo> systemInfoThreadLocal = new ThreadLocal<>();
  85. /**
  86. * 获取系统设置信息
  87. *
  88. * @return
  89. */
  90. public static BaseSystemInfo getBaseSystemInfo() {
  91. BaseSystemInfo systemInfo = systemInfoThreadLocal.get();
  92. return systemInfo;
  93. }
  94. public static void setBaseSystemInfo(BaseSystemInfo baseSystemInfo) {
  95. systemInfoThreadLocal.set(baseSystemInfo);
  96. }
  97. public static void clearBaseSystemIfo() {
  98. systemInfoThreadLocal.remove();
  99. }
  100. }