package com.gkhy.safePlatform.account.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfoDO; import com.gkhy.safePlatform.account.entity.user.MenuInfoDO; import com.gkhy.safePlatform.account.service.RedisService; import com.gkhy.safePlatform.account.utils.RedisUtils; import com.gkhy.safePlatform.commons.co.ContextCacheUser; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.exception.BusinessException; import com.gkhy.safePlatform.commons.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.stereotype.Service; import java.util.List; import java.util.concurrent.TimeUnit; @Service("redisService") public class RedisServiceImpl implements RedisService { @Autowired private RedisUtils redisUtils; /** * @Description: 设置用户缓存 */ @Override public void setCacheUserAndExpireTime(String key, ContextCacheUser user, Long expireTime) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } if (expireTime == null) { expireTime = 5 * 60L; } redisUtils.set(key, JSONObject.toJSONString(user), expireTime, TimeUnit.SECONDS); } @Override public void setCacheAuthorityAndExpireTime(String key, List authorities, Long expireTime) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } if (expireTime == null) { expireTime = 5 * 60L; } redisUtils.set(key, JSONObject.toJSONString(authorities), expireTime, TimeUnit.SECONDS); } @Override public void setCacheMenuAndExpireTime(String key, List menus, Long expireTime) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } if (expireTime == null) { expireTime = 5 * 60L; } redisUtils.set(key, JSONArray.toJSONString(menus), expireTime, TimeUnit.SECONDS); } @Override public void setCacheDepAndExpireTime(String key, List deps, Long expireTime) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } if (expireTime == null) { expireTime = 5 * 60L; } redisUtils.set(key, JSONArray.toJSONString(deps), expireTime, TimeUnit.SECONDS); } @Override public String getCacheUserByKey(String key) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } ; Object o = redisUtils.get(key); if (o != null) { return o.toString(); } return null; } @Override public String getCacheMenuByKey(String key) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } Object o = redisUtils.get(key); if (o != null) { return o.toString(); } return null; } @Override public String getCacheDepByKey(String key) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } Object o = redisUtils.get(key); if (o != null) { return o.toString(); } return null; } @Override public Long getLeftSecondsByKey(String key) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } return redisUtils.getExpireTime(key); } @Override public void resetKeyExpireTime(String key, Long expireTime) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } redisUtils.resetKeyExpireTime(key,expireTime); } @Override public void cleanCacheUserByKey(String key) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } redisUtils.remove(key); } @Override public void cleanCacheAuthorityByKey(String key) { ; if (StringUtils.isBlank(key)) { throw new BusinessException(ResultCodes.REDIS_KEY_NULL); } redisUtils.remove(key); } }