mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	add RedisCache 工具类 增加 发布订阅工具
This commit is contained in:
		@@ -5,8 +5,12 @@ import org.redisson.api.*;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.function.Consumer;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * spring redis 工具类
 | 
			
		||||
@@ -20,6 +24,36 @@ public class RedisCache {
 | 
			
		||||
	@Autowired
 | 
			
		||||
	private RedissonClient redissonClient;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 发布通道消息
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param channelKey 通道key
 | 
			
		||||
	 * @param msg 发送数据
 | 
			
		||||
	 * @param consumer 自定义处理
 | 
			
		||||
	 */
 | 
			
		||||
	public <T> void publish(String channelKey, T msg, Consumer consumer) {
 | 
			
		||||
		RTopic topic = redissonClient.getTopic(channelKey);
 | 
			
		||||
		topic.publish(msg);
 | 
			
		||||
		consumer.accept(msg);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public <T> void publish(String channelKey, T msg) {
 | 
			
		||||
		RTopic topic = redissonClient.getTopic(channelKey);
 | 
			
		||||
		topic.publish(msg);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 订阅通道接收消息
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param channelKey 通道key
 | 
			
		||||
	 * @param clazz 消息类型
 | 
			
		||||
	 * @param consumer 自定义处理
 | 
			
		||||
	 */
 | 
			
		||||
	public <T> void subscribe(String channelKey, Class<T> clazz, Consumer consumer) {
 | 
			
		||||
		RTopic topic = redissonClient.getTopic(channelKey);
 | 
			
		||||
		topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 缓存基本的对象,Integer、String、实体类等
 | 
			
		||||
	 *
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user