微信后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.0 KiB

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<String, User> 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<String, User> getGmcUserCacheManager() {
if (GMC_USER_CACHE == null) {
createGmcUserCacheManager();
}
return GMC_USER_CACHE;
}
public static void addCacheGmcUser(String openid, User user) {
Cache<String, User> cache = getGmcUserCacheManager();
if (cache != null) {
cache.put(openid, user);
}
}
public static void removeCacheGmcUser(String openid) {
Cache<String, User> 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<Patient> getCacheGmcUserPatientList(String openid) {
if (GMC_USER_CACHE == null) {
createGmcUserCacheManager();
}
User user = GMC_USER_CACHE.get(openid);
if (user == null) {
return new ArrayList<>();
}
List<Patient> patientList = user.getPatientList();
if (patientList == null || patientList.isEmpty()) {
return new ArrayList<>();
}
return JsonHelper.copyArray(patientList, Patient.class);
}
}