mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 所有业务适配 RedisUtils 新工具
This commit is contained in:
		@@ -6,8 +6,8 @@ import cn.hutool.core.convert.Convert;
 | 
			
		||||
import cn.hutool.core.util.IdUtil;
 | 
			
		||||
import com.ruoyi.common.constant.Constants;
 | 
			
		||||
import com.ruoyi.common.core.domain.AjaxResult;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.enums.CaptchaType;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.common.utils.reflect.ReflectUtils;
 | 
			
		||||
import com.ruoyi.common.utils.spring.SpringUtils;
 | 
			
		||||
@@ -29,9 +29,6 @@ import java.util.concurrent.TimeUnit;
 | 
			
		||||
@RestController
 | 
			
		||||
public class CaptchaController {
 | 
			
		||||
 | 
			
		||||
	@Autowired
 | 
			
		||||
	private RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
	@Autowired
 | 
			
		||||
	private CaptchaProperties captchaProperties;
 | 
			
		||||
 | 
			
		||||
@@ -61,7 +58,7 @@ public class CaptchaController {
 | 
			
		||||
		captcha.setGenerator(codeGenerator);
 | 
			
		||||
		captcha.createCode();
 | 
			
		||||
		String code = isMath ? getCodeResult(captcha.getCode()) : captcha.getCode();
 | 
			
		||||
		redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
 | 
			
		||||
		RedisUtils.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
 | 
			
		||||
		ajax.put("uuid", uuid);
 | 
			
		||||
		ajax.put("img", captcha.getImageBase64());
 | 
			
		||||
		return AjaxResult.success(ajax);
 | 
			
		||||
 
 | 
			
		||||
@@ -6,9 +6,9 @@ import com.ruoyi.common.core.controller.BaseController;
 | 
			
		||||
import com.ruoyi.common.core.domain.AjaxResult;
 | 
			
		||||
import com.ruoyi.common.core.domain.model.LoginUser;
 | 
			
		||||
import com.ruoyi.common.core.page.TableDataInfo;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.enums.BusinessType;
 | 
			
		||||
import com.ruoyi.common.utils.PageUtils;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.system.domain.SysUserOnline;
 | 
			
		||||
import com.ruoyi.system.service.ISysUserOnlineService;
 | 
			
		||||
@@ -33,18 +33,15 @@ public class SysUserOnlineController extends BaseController
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private ISysUserOnlineService userOnlineService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
 | 
			
		||||
    @GetMapping("/list")
 | 
			
		||||
    public TableDataInfo list(String ipaddr, String userName)
 | 
			
		||||
    {
 | 
			
		||||
        Collection<String> keys = redisCache.keys(Constants.LOGIN_TOKEN_KEY + "*");
 | 
			
		||||
        Collection<String> keys = RedisUtils.keys(Constants.LOGIN_TOKEN_KEY + "*");
 | 
			
		||||
        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
 | 
			
		||||
        for (String key : keys)
 | 
			
		||||
        {
 | 
			
		||||
            LoginUser user = redisCache.getCacheObject(key);
 | 
			
		||||
            LoginUser user = RedisUtils.getCacheObject(key);
 | 
			
		||||
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName))
 | 
			
		||||
            {
 | 
			
		||||
                if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername()))
 | 
			
		||||
@@ -84,7 +81,7 @@ public class SysUserOnlineController extends BaseController
 | 
			
		||||
    @DeleteMapping("/{tokenId}")
 | 
			
		||||
    public AjaxResult forceLogout(@PathVariable String tokenId)
 | 
			
		||||
    {
 | 
			
		||||
        redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + tokenId);
 | 
			
		||||
        RedisUtils.deleteObject(Constants.LOGIN_TOKEN_KEY + tokenId);
 | 
			
		||||
        return AjaxResult.success();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
package com.ruoyi.common.core.mybatisplus.cache;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.extra.spring.SpringUtil;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.apache.ibatis.cache.Cache;
 | 
			
		||||
import org.springframework.data.redis.connection.RedisServerCommands;
 | 
			
		||||
@@ -25,8 +25,6 @@ public class MybatisPlusRedisCache implements Cache {
 | 
			
		||||
 | 
			
		||||
	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
 | 
			
		||||
 | 
			
		||||
	private RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
	private String id;
 | 
			
		||||
 | 
			
		||||
	public MybatisPlusRedisCache(final String id) {
 | 
			
		||||
@@ -43,23 +41,16 @@ public class MybatisPlusRedisCache implements Cache {
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void putObject(Object key, Object value) {
 | 
			
		||||
		if (redisCache == null) {
 | 
			
		||||
			redisCache = SpringUtil.getBean(RedisCache.class);
 | 
			
		||||
		}
 | 
			
		||||
		if (value != null) {
 | 
			
		||||
			redisCache.setCacheObject(key.toString(), value);
 | 
			
		||||
			RedisUtils.setCacheObject(key.toString(), value);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Object getObject(Object key) {
 | 
			
		||||
		if (redisCache == null) {
 | 
			
		||||
			//由于启动期间注入失败,只能运行期间注入,这段代码可以删除
 | 
			
		||||
			redisCache = SpringUtil.getBean(RedisCache.class);
 | 
			
		||||
		}
 | 
			
		||||
		try {
 | 
			
		||||
			if (key != null) {
 | 
			
		||||
				return redisCache.getCacheObject(key.toString());
 | 
			
		||||
				return RedisUtils.getCacheObject(key.toString());
 | 
			
		||||
			}
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
@@ -70,11 +61,8 @@ public class MybatisPlusRedisCache implements Cache {
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Object removeObject(Object key) {
 | 
			
		||||
		if (redisCache == null) {
 | 
			
		||||
			redisCache = SpringUtil.getBean(RedisCache.class);
 | 
			
		||||
		}
 | 
			
		||||
		if (key != null) {
 | 
			
		||||
			redisCache.deleteObject(key.toString());
 | 
			
		||||
			RedisUtils.deleteObject(key.toString());
 | 
			
		||||
		}
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
@@ -82,12 +70,9 @@ public class MybatisPlusRedisCache implements Cache {
 | 
			
		||||
	@Override
 | 
			
		||||
	public void clear() {
 | 
			
		||||
		log.debug("清空缓存");
 | 
			
		||||
		if (redisCache == null) {
 | 
			
		||||
			redisCache = SpringUtil.getBean(RedisCache.class);
 | 
			
		||||
		}
 | 
			
		||||
		Collection<String> keys = redisCache.keys("*:" + this.id + "*");
 | 
			
		||||
		Collection<String> keys = RedisUtils.keys("*:" + this.id + "*");
 | 
			
		||||
		if (!CollectionUtils.isEmpty(keys)) {
 | 
			
		||||
			redisCache.deleteObject(keys);
 | 
			
		||||
			RedisUtils.deleteObject(keys);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,8 +3,6 @@ package com.ruoyi.common.utils;
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import com.ruoyi.common.constant.Constants;
 | 
			
		||||
import com.ruoyi.common.core.domain.entity.SysDictData;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.utils.spring.SpringUtils;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@@ -29,7 +27,7 @@ public class DictUtils
 | 
			
		||||
     */
 | 
			
		||||
    public static void setDictCache(String key, List<SysDictData> dictDatas)
 | 
			
		||||
    {
 | 
			
		||||
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDatas);
 | 
			
		||||
        RedisUtils.setCacheObject(getCacheKey(key), dictDatas);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -40,7 +38,7 @@ public class DictUtils
 | 
			
		||||
     */
 | 
			
		||||
    public static List<SysDictData> getDictCache(String key)
 | 
			
		||||
    {
 | 
			
		||||
        Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
 | 
			
		||||
        Object cacheObj = RedisUtils.getCacheObject(getCacheKey(key));
 | 
			
		||||
        if (StringUtils.isNotNull(cacheObj))
 | 
			
		||||
        {
 | 
			
		||||
            List<SysDictData> dictDatas = (List<SysDictData>)cacheObj;
 | 
			
		||||
@@ -160,7 +158,7 @@ public class DictUtils
 | 
			
		||||
     */
 | 
			
		||||
    public static void removeDictCache(String key)
 | 
			
		||||
    {
 | 
			
		||||
        SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key));
 | 
			
		||||
        RedisUtils.deleteObject(getCacheKey(key));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -168,8 +166,8 @@ public class DictUtils
 | 
			
		||||
     */
 | 
			
		||||
    public static void clearDictCache()
 | 
			
		||||
    {
 | 
			
		||||
        Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*");
 | 
			
		||||
        SpringUtils.getBean(RedisCache.class).deleteObject(keys);
 | 
			
		||||
        Collection<String> keys = RedisUtils.keys(Constants.SYS_DICT_KEY + "*");
 | 
			
		||||
        RedisUtils.deleteObject(keys);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
package com.ruoyi.demo.controller;
 | 
			
		||||
 | 
			
		||||
import com.ruoyi.common.core.domain.AjaxResult;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import io.swagger.annotations.Api;
 | 
			
		||||
import io.swagger.annotations.ApiOperation;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
@@ -21,12 +21,10 @@ import org.springframework.web.bind.annotation.RestController;
 | 
			
		||||
@RequestMapping("/demo/redis/pubsub")
 | 
			
		||||
public class RedisPubSubController {
 | 
			
		||||
 | 
			
		||||
	private final RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
	@ApiOperation("发布消息")
 | 
			
		||||
	@GetMapping("/pub")
 | 
			
		||||
	public AjaxResult<Void> pub(String key, String value){
 | 
			
		||||
		redisCache.publish(key, value, consumer -> {
 | 
			
		||||
		RedisUtils.publish(key, value, consumer -> {
 | 
			
		||||
			System.out.println("发布通道 => " + key + ", 发送值 => " + value);
 | 
			
		||||
		});
 | 
			
		||||
		return AjaxResult.success("操作成功");
 | 
			
		||||
@@ -35,7 +33,7 @@ public class RedisPubSubController {
 | 
			
		||||
	@ApiOperation("订阅消息")
 | 
			
		||||
	@GetMapping("/sub")
 | 
			
		||||
	public AjaxResult<Void> sub(String key){
 | 
			
		||||
		redisCache.subscribe(key, String.class, msg -> {
 | 
			
		||||
		RedisUtils.subscribe(key, String.class, msg -> {
 | 
			
		||||
			System.out.println("订阅通道 => " + key + ", 接收值 => " + msg);
 | 
			
		||||
		});
 | 
			
		||||
		return AjaxResult.success("操作成功");
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package com.ruoyi.framework.config;
 | 
			
		||||
import cn.hutool.core.util.StrUtil;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.framework.config.properties.RedissonProperties;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.redisson.Redisson;
 | 
			
		||||
import org.redisson.api.RedissonClient;
 | 
			
		||||
import org.redisson.codec.JsonJacksonCodec;
 | 
			
		||||
@@ -29,6 +30,7 @@ import java.util.Map;
 | 
			
		||||
 *
 | 
			
		||||
 * @author Lion Li
 | 
			
		||||
 */
 | 
			
		||||
@Slf4j
 | 
			
		||||
@Configuration
 | 
			
		||||
@EnableCaching
 | 
			
		||||
public class RedisConfig extends CachingConfigurerSupport {
 | 
			
		||||
@@ -73,7 +75,9 @@ public class RedisConfig extends CachingConfigurerSupport {
 | 
			
		||||
			.setConnectionMinimumIdleSize(singleServerConfig.getConnectionMinimumIdleSize())
 | 
			
		||||
			.setConnectionPoolSize(singleServerConfig.getConnectionPoolSize())
 | 
			
		||||
			.setDnsMonitoringInterval(singleServerConfig.getDnsMonitoringInterval());
 | 
			
		||||
		return Redisson.create(config);
 | 
			
		||||
		RedissonClient redissonClient = Redisson.create(config);
 | 
			
		||||
		log.info("初始化 redis 配置");
 | 
			
		||||
		return redissonClient;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
 
 | 
			
		||||
@@ -4,9 +4,9 @@ import cn.hutool.core.convert.Convert;
 | 
			
		||||
import cn.hutool.core.io.IoUtil;
 | 
			
		||||
import com.ruoyi.common.annotation.RepeatSubmit;
 | 
			
		||||
import com.ruoyi.common.constant.Constants;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.filter.RepeatedlyRequestWrapper;
 | 
			
		||||
import com.ruoyi.common.utils.JsonUtils;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.framework.config.properties.RepeatSubmitProperties;
 | 
			
		||||
import com.ruoyi.framework.config.properties.TokenProperties;
 | 
			
		||||
@@ -38,7 +38,6 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor {
 | 
			
		||||
 | 
			
		||||
	private final TokenProperties tokenProperties;
 | 
			
		||||
	private final RepeatSubmitProperties repeatSubmitProperties;
 | 
			
		||||
	private final RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	@SuppressWarnings("unchecked")
 | 
			
		||||
@@ -79,7 +78,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor {
 | 
			
		||||
		// 唯一标识(指定key + 消息头)
 | 
			
		||||
		String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + submitKey;
 | 
			
		||||
 | 
			
		||||
		Object sessionObj = redisCache.getCacheObject(cacheRepeatKey);
 | 
			
		||||
		Object sessionObj = RedisUtils.getCacheObject(cacheRepeatKey);
 | 
			
		||||
		if (sessionObj != null) {
 | 
			
		||||
			Map<String, Object> sessionMap = (Map<String, Object>) sessionObj;
 | 
			
		||||
			if (sessionMap.containsKey(url)) {
 | 
			
		||||
@@ -91,7 +90,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor {
 | 
			
		||||
		}
 | 
			
		||||
		Map<String, Object> cacheMap = new HashMap<String, Object>();
 | 
			
		||||
		cacheMap.put(url, nowDataMap);
 | 
			
		||||
		redisCache.setCacheObject(cacheRepeatKey, cacheMap, Convert.toInt(intervalTime), TimeUnit.MILLISECONDS);
 | 
			
		||||
		RedisUtils.setCacheObject(cacheRepeatKey, cacheMap, Convert.toInt(intervalTime), TimeUnit.MILLISECONDS);
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,13 +3,13 @@ package com.ruoyi.framework.web.service;
 | 
			
		||||
import com.ruoyi.common.constant.Constants;
 | 
			
		||||
import com.ruoyi.common.core.domain.entity.SysUser;
 | 
			
		||||
import com.ruoyi.common.core.domain.model.LoginUser;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.exception.ServiceException;
 | 
			
		||||
import com.ruoyi.common.exception.user.CaptchaException;
 | 
			
		||||
import com.ruoyi.common.exception.user.CaptchaExpireException;
 | 
			
		||||
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
 | 
			
		||||
import com.ruoyi.common.utils.DateUtils;
 | 
			
		||||
import com.ruoyi.common.utils.MessageUtils;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.utils.ServletUtils;
 | 
			
		||||
import com.ruoyi.system.service.ISysConfigService;
 | 
			
		||||
import com.ruoyi.system.service.ISysUserService;
 | 
			
		||||
@@ -37,9 +37,6 @@ public class SysLoginService
 | 
			
		||||
    @Resource
 | 
			
		||||
    private AuthenticationManager authenticationManager;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
	@Autowired
 | 
			
		||||
    private ISysUserService userService;
 | 
			
		||||
 | 
			
		||||
@@ -105,8 +102,8 @@ public class SysLoginService
 | 
			
		||||
     */
 | 
			
		||||
    public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
 | 
			
		||||
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
 | 
			
		||||
		String captcha = redisCache.getCacheObject(verifyKey);
 | 
			
		||||
		redisCache.deleteObject(verifyKey);
 | 
			
		||||
		String captcha = RedisUtils.getCacheObject(verifyKey);
 | 
			
		||||
        RedisUtils.deleteObject(verifyKey);
 | 
			
		||||
		if (captcha == null) {
 | 
			
		||||
			asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
 | 
			
		||||
			throw new CaptchaExpireException();
 | 
			
		||||
 
 | 
			
		||||
@@ -4,13 +4,9 @@ import com.ruoyi.common.constant.Constants;
 | 
			
		||||
import com.ruoyi.common.constant.UserConstants;
 | 
			
		||||
import com.ruoyi.common.core.domain.entity.SysUser;
 | 
			
		||||
import com.ruoyi.common.core.domain.model.RegisterBody;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.exception.user.CaptchaException;
 | 
			
		||||
import com.ruoyi.common.exception.user.CaptchaExpireException;
 | 
			
		||||
import com.ruoyi.common.utils.MessageUtils;
 | 
			
		||||
import com.ruoyi.common.utils.SecurityUtils;
 | 
			
		||||
import com.ruoyi.common.utils.ServletUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.common.utils.*;
 | 
			
		||||
import com.ruoyi.system.service.ISysConfigService;
 | 
			
		||||
import com.ruoyi.system.service.ISysUserService;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
@@ -30,9 +26,6 @@ public class SysRegisterService
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private ISysConfigService configService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
	@Autowired
 | 
			
		||||
	private AsyncService asyncService;
 | 
			
		||||
 | 
			
		||||
@@ -103,8 +96,8 @@ public class SysRegisterService
 | 
			
		||||
    public void validateCaptcha(String username, String code, String uuid)
 | 
			
		||||
    {
 | 
			
		||||
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
 | 
			
		||||
        String captcha = redisCache.getCacheObject(verifyKey);
 | 
			
		||||
        redisCache.deleteObject(verifyKey);
 | 
			
		||||
        String captcha = RedisUtils.getCacheObject(verifyKey);
 | 
			
		||||
        RedisUtils.deleteObject(verifyKey);
 | 
			
		||||
        if (captcha == null)
 | 
			
		||||
        {
 | 
			
		||||
            throw new CaptchaExpireException();
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import cn.hutool.http.useragent.UserAgent;
 | 
			
		||||
import cn.hutool.http.useragent.UserAgentUtil;
 | 
			
		||||
import com.ruoyi.common.constant.Constants;
 | 
			
		||||
import com.ruoyi.common.core.domain.model.LoginUser;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.utils.ServletUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.common.utils.ip.AddressUtils;
 | 
			
		||||
@@ -35,9 +35,6 @@ public class TokenService {
 | 
			
		||||
 | 
			
		||||
    private static final Long MILLIS_MINUTE_TEN = 20 * 60 * 1000L;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private TokenProperties tokenProperties;
 | 
			
		||||
 | 
			
		||||
@@ -55,7 +52,7 @@ public class TokenService {
 | 
			
		||||
				// 解析对应的权限以及用户信息
 | 
			
		||||
				String uuid = (String) claims.get(Constants.LOGIN_USER_KEY);
 | 
			
		||||
				String userKey = getTokenKey(uuid);
 | 
			
		||||
				LoginUser user = redisCache.getCacheObject(userKey);
 | 
			
		||||
				LoginUser user = RedisUtils.getCacheObject(userKey);
 | 
			
		||||
				return user;
 | 
			
		||||
			} catch (Exception e) {
 | 
			
		||||
 | 
			
		||||
@@ -79,7 +76,7 @@ public class TokenService {
 | 
			
		||||
    public void delLoginUser(String token) {
 | 
			
		||||
        if (StringUtils.isNotEmpty(token)) {
 | 
			
		||||
            String userKey = getTokenKey(token);
 | 
			
		||||
            redisCache.deleteObject(userKey);
 | 
			
		||||
            RedisUtils.deleteObject(userKey);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -124,7 +121,7 @@ public class TokenService {
 | 
			
		||||
        loginUser.setExpireTime(loginUser.getLoginTime() + tokenProperties.getExpireTime() * MILLIS_MINUTE);
 | 
			
		||||
        // 根据uuid将loginUser缓存
 | 
			
		||||
        String userKey = getTokenKey(loginUser.getToken());
 | 
			
		||||
        redisCache.setCacheObject(userKey, loginUser, tokenProperties.getExpireTime(), TimeUnit.MINUTES);
 | 
			
		||||
        RedisUtils.setCacheObject(userKey, loginUser, tokenProperties.getExpireTime(), TimeUnit.MINUTES);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,10 @@
 | 
			
		||||
package com.ruoyi.oss.factory;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.convert.Convert;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.utils.JsonUtils;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.common.utils.reflect.ReflectUtils;
 | 
			
		||||
import com.ruoyi.common.utils.spring.SpringUtils;
 | 
			
		||||
import com.ruoyi.oss.constant.CloudConstant;
 | 
			
		||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
 | 
			
		||||
import com.ruoyi.oss.exception.OssException;
 | 
			
		||||
@@ -25,11 +24,8 @@ import java.util.concurrent.ConcurrentHashMap;
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class OssFactory {
 | 
			
		||||
 | 
			
		||||
	private static RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
	static {
 | 
			
		||||
		OssFactory.redisCache = SpringUtils.getBean(RedisCache.class);
 | 
			
		||||
		redisCache.subscribe(CloudConstant.CACHE_CONFIG_KEY, String.class, msg -> {
 | 
			
		||||
		RedisUtils.subscribe(CloudConstant.CACHE_CONFIG_KEY, String.class, msg -> {
 | 
			
		||||
			refreshService(msg);
 | 
			
		||||
			log.info("订阅刷新OSS配置 => " + msg);
 | 
			
		||||
		});
 | 
			
		||||
@@ -45,7 +41,7 @@ public class OssFactory {
 | 
			
		||||
	 */
 | 
			
		||||
	public static ICloudStorageStrategy instance() {
 | 
			
		||||
		// 获取redis 默认类型
 | 
			
		||||
		String type = Convert.toStr(redisCache.getCacheObject(CloudConstant.CACHE_CONFIG_KEY));
 | 
			
		||||
		String type = Convert.toStr(RedisUtils.getCacheObject(CloudConstant.CACHE_CONFIG_KEY));
 | 
			
		||||
		if (StringUtils.isEmpty(type)) {
 | 
			
		||||
			throw new OssException("文件存储服务类型无法找到!");
 | 
			
		||||
		}
 | 
			
		||||
@@ -65,7 +61,7 @@ public class OssFactory {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static void refreshService(String type) {
 | 
			
		||||
		Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
 | 
			
		||||
		Object json = RedisUtils.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
 | 
			
		||||
		CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class);
 | 
			
		||||
		if (properties == null) {
 | 
			
		||||
			throw new OssException("系统异常, '" + type + "'配置信息不存在!");
 | 
			
		||||
 
 | 
			
		||||
@@ -1,21 +1,20 @@
 | 
			
		||||
package com.ruoyi.system.service.impl;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.convert.Convert;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
			
		||||
import com.ruoyi.common.annotation.DataSource;
 | 
			
		||||
import com.ruoyi.common.constant.Constants;
 | 
			
		||||
import com.ruoyi.common.constant.UserConstants;
 | 
			
		||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
 | 
			
		||||
import com.ruoyi.common.core.page.TableDataInfo;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.enums.DataSourceType;
 | 
			
		||||
import com.ruoyi.common.exception.ServiceException;
 | 
			
		||||
import com.ruoyi.common.utils.PageUtils;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.system.domain.SysConfig;
 | 
			
		||||
import com.ruoyi.system.mapper.SysConfigMapper;
 | 
			
		||||
import com.ruoyi.system.service.ISysConfigService;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.PostConstruct;
 | 
			
		||||
@@ -32,9 +31,6 @@ import java.util.Map;
 | 
			
		||||
@Service
 | 
			
		||||
public class SysConfigServiceImpl extends ServicePlusImpl<SysConfigMapper, SysConfig, SysConfig> implements ISysConfigService {
 | 
			
		||||
 | 
			
		||||
	@Autowired
 | 
			
		||||
	private RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 项目启动时,初始化参数到缓存
 | 
			
		||||
	 */
 | 
			
		||||
@@ -79,14 +75,14 @@ public class SysConfigServiceImpl extends ServicePlusImpl<SysConfigMapper, SysCo
 | 
			
		||||
	 */
 | 
			
		||||
	@Override
 | 
			
		||||
	public String selectConfigByKey(String configKey) {
 | 
			
		||||
		String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
 | 
			
		||||
		String configValue = Convert.toStr(RedisUtils.getCacheObject(getCacheKey(configKey)));
 | 
			
		||||
		if (StringUtils.isNotEmpty(configValue)) {
 | 
			
		||||
			return configValue;
 | 
			
		||||
		}
 | 
			
		||||
		SysConfig retConfig = baseMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
 | 
			
		||||
			.eq(SysConfig::getConfigKey, configKey));
 | 
			
		||||
		if (StringUtils.isNotNull(retConfig)) {
 | 
			
		||||
			redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
 | 
			
		||||
			RedisUtils.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
 | 
			
		||||
			return retConfig.getConfigValue();
 | 
			
		||||
		}
 | 
			
		||||
		return StringUtils.EMPTY;
 | 
			
		||||
@@ -138,7 +134,7 @@ public class SysConfigServiceImpl extends ServicePlusImpl<SysConfigMapper, SysCo
 | 
			
		||||
	public int insertConfig(SysConfig config) {
 | 
			
		||||
		int row = baseMapper.insert(config);
 | 
			
		||||
		if (row > 0) {
 | 
			
		||||
			redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
 | 
			
		||||
			RedisUtils.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
 | 
			
		||||
		}
 | 
			
		||||
		return row;
 | 
			
		||||
	}
 | 
			
		||||
@@ -153,7 +149,7 @@ public class SysConfigServiceImpl extends ServicePlusImpl<SysConfigMapper, SysCo
 | 
			
		||||
	public int updateConfig(SysConfig config) {
 | 
			
		||||
		int row = baseMapper.updateById(config);
 | 
			
		||||
		if (row > 0) {
 | 
			
		||||
			redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
 | 
			
		||||
			RedisUtils.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
 | 
			
		||||
		}
 | 
			
		||||
		return row;
 | 
			
		||||
	}
 | 
			
		||||
@@ -171,7 +167,7 @@ public class SysConfigServiceImpl extends ServicePlusImpl<SysConfigMapper, SysCo
 | 
			
		||||
			if (StringUtils.equals(UserConstants.YES, config.getConfigType())) {
 | 
			
		||||
				throw new ServiceException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey()));
 | 
			
		||||
			}
 | 
			
		||||
			redisCache.deleteObject(getCacheKey(config.getConfigKey()));
 | 
			
		||||
			RedisUtils.deleteObject(getCacheKey(config.getConfigKey()));
 | 
			
		||||
		}
 | 
			
		||||
		baseMapper.deleteBatchIds(Arrays.asList(configIds));
 | 
			
		||||
	}
 | 
			
		||||
@@ -183,7 +179,7 @@ public class SysConfigServiceImpl extends ServicePlusImpl<SysConfigMapper, SysCo
 | 
			
		||||
	public void loadingConfigCache() {
 | 
			
		||||
		List<SysConfig> configsList = selectConfigList(new SysConfig());
 | 
			
		||||
		for (SysConfig config : configsList) {
 | 
			
		||||
			redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
 | 
			
		||||
			RedisUtils.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -192,8 +188,8 @@ public class SysConfigServiceImpl extends ServicePlusImpl<SysConfigMapper, SysCo
 | 
			
		||||
	 */
 | 
			
		||||
	@Override
 | 
			
		||||
	public void clearConfigCache() {
 | 
			
		||||
		Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");
 | 
			
		||||
		redisCache.deleteObject(keys);
 | 
			
		||||
		Collection<String> keys = RedisUtils.keys(Constants.SYS_CONFIG_KEY + "*");
 | 
			
		||||
		RedisUtils.deleteObject(keys);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
 
 | 
			
		||||
@@ -9,10 +9,10 @@ import com.ruoyi.common.constant.UserConstants;
 | 
			
		||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
 | 
			
		||||
import com.ruoyi.common.core.page.PagePlus;
 | 
			
		||||
import com.ruoyi.common.core.page.TableDataInfo;
 | 
			
		||||
import com.ruoyi.common.core.redis.RedisCache;
 | 
			
		||||
import com.ruoyi.common.exception.ServiceException;
 | 
			
		||||
import com.ruoyi.common.utils.JsonUtils;
 | 
			
		||||
import com.ruoyi.common.utils.PageUtils;
 | 
			
		||||
import com.ruoyi.common.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.oss.constant.CloudConstant;
 | 
			
		||||
import com.ruoyi.system.domain.SysOssConfig;
 | 
			
		||||
@@ -42,8 +42,6 @@ import java.util.List;
 | 
			
		||||
@Service
 | 
			
		||||
public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper, SysOssConfig, SysOssConfigVo> implements ISysOssConfigService {
 | 
			
		||||
 | 
			
		||||
	private final RedisCache redisCache;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 项目启动时,初始化参数到缓存,加载配置类
 | 
			
		||||
	 */
 | 
			
		||||
@@ -53,7 +51,7 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
 | 
			
		||||
		for (SysOssConfig config : list) {
 | 
			
		||||
			String configKey = config.getConfigKey();
 | 
			
		||||
			if ("0".equals(config.getStatus())) {
 | 
			
		||||
				redisCache.setCacheObject(CloudConstant.CACHE_CONFIG_KEY, configKey);
 | 
			
		||||
				RedisUtils.setCacheObject(CloudConstant.CACHE_CONFIG_KEY, configKey);
 | 
			
		||||
			}
 | 
			
		||||
			setConfigCache(true, config);
 | 
			
		||||
		}
 | 
			
		||||
@@ -114,7 +112,7 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
 | 
			
		||||
    	if (flag) {
 | 
			
		||||
			for (Long configId : ids) {
 | 
			
		||||
				SysOssConfig config = getById(configId);
 | 
			
		||||
				redisCache.deleteObject(getCacheKey(config.getConfigKey()));
 | 
			
		||||
				RedisUtils.deleteObject(getCacheKey(config.getConfigKey()));
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
    	return flag;
 | 
			
		||||
@@ -145,7 +143,7 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
 | 
			
		||||
			.set(SysOssConfig::getStatus, "1"));
 | 
			
		||||
		row += baseMapper.updateById(sysOssConfig);
 | 
			
		||||
		if (row > 0) {
 | 
			
		||||
			redisCache.setCacheObject(CloudConstant.CACHE_CONFIG_KEY, sysOssConfig.getConfigKey());
 | 
			
		||||
			RedisUtils.setCacheObject(CloudConstant.CACHE_CONFIG_KEY, sysOssConfig.getConfigKey());
 | 
			
		||||
		}
 | 
			
		||||
		return row;
 | 
			
		||||
	}
 | 
			
		||||
@@ -168,10 +166,10 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
 | 
			
		||||
	 */
 | 
			
		||||
	private boolean setConfigCache(boolean flag, SysOssConfig config) {
 | 
			
		||||
		if (flag) {
 | 
			
		||||
			redisCache.setCacheObject(
 | 
			
		||||
			RedisUtils.setCacheObject(
 | 
			
		||||
				getCacheKey(config.getConfigKey()),
 | 
			
		||||
				JsonUtils.toJsonString(config));
 | 
			
		||||
			redisCache.publish(CloudConstant.CACHE_CONFIG_KEY, config.getConfigKey(), msg -> {
 | 
			
		||||
			RedisUtils.publish(CloudConstant.CACHE_CONFIG_KEY, config.getConfigKey(), msg -> {
 | 
			
		||||
				log.info("发布刷新OSS配置 => " + msg);
 | 
			
		||||
			});
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user