package com.ynxbd.common.cache; import com.ynxbd.common.bean.Patient; import com.ynxbd.common.bean.User; import com.ynxbd.common.config.EhCacheConfig; import com.ynxbd.common.helper.common.JsonHelper; import lombok.extern.slf4j.Slf4j; import org.ehcache.Cache; import java.util.ArrayList; import java.util.List; @Slf4j public class GmcCacheManager { // 医共体缓存数据 private static Cache GMC_USER_CACHE; static { createGmcUserCacheManager(); } private synchronized static void createGmcUserCacheManager() { if (GMC_USER_CACHE == null) { GMC_USER_CACHE = EhCacheConfig.createCacheTTL(String.class, User.class, "db_gmc_cache", (900L)); // 15分钟 } } public static Cache getGmcUserCacheManager() { if (GMC_USER_CACHE == null) { createGmcUserCacheManager(); } return GMC_USER_CACHE; } public static void addCacheGmcUser(String openid, User user) { Cache cache = getGmcUserCacheManager(); if (cache != null) { cache.put(openid, user); } } public static void removeCacheGmcUser(String openid) { Cache cache = getGmcUserCacheManager(); if (cache != null) { cache.remove(openid); } } public static boolean hasCacheGmcUser(String openid) { if (GMC_USER_CACHE == null) { createGmcUserCacheManager(); } return GMC_USER_CACHE.containsKey(openid); } public static List getCacheGmcUserPatientList(String openid) { if (GMC_USER_CACHE == null) { createGmcUserCacheManager(); } User user = GMC_USER_CACHE.get(openid); if (user == null) { return new ArrayList<>(); } List patientList = user.getPatientList(); if (patientList == null || patientList.isEmpty()) { return new ArrayList<>(); } return JsonHelper.copyArray(patientList, Patient.class); } }