package com.gkhy.labRiskManage.infra.cache.service.impl; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.gkhy.labRiskManage.commons.enums.SystemCacheKeyEnum; import com.gkhy.labRiskManage.infra.cache.domain.CacheUserInfo; import com.gkhy.labRiskManage.infra.cache.service.UserCacheInfraService; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class UserCacheInfraServiceImpl implements UserCacheInfraService { @Autowired private RedissonClient redissonClient; @Autowired private ObjectMapper objectMapper; @Override public CacheUserInfo getCacheUser(String userId) { if(userId == null || userId.isEmpty()) return null; Object cacheUserObj = redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey()).get(""+userId); if(cacheUserObj != null){ String json = (String)cacheUserObj; try { CacheUserInfo cacheUserInfo = objectMapper.readValue(json,CacheUserInfo.class); return cacheUserInfo; } catch (JsonProcessingException e) { e.printStackTrace(); } } return null; } @Override public boolean putCacheUser(CacheUserInfo cacheUserInfo, String userId) { if(cacheUserInfo == null || userId == null || userId.isEmpty()) return false; try { String json = objectMapper.writeValueAsString(cacheUserInfo); if(redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey()).put(userId,json,120, TimeUnit.MINUTES) != null) return true; } catch (JsonProcessingException e) { e.printStackTrace(); } return false; } @Override public boolean removeCacheUser(String userId) { if(redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey()).remove(userId) != null) return true; else return false; } }