This commit is contained in:
2023-07-17 17:38:06 +08:00
parent a9b02b5a4d
commit 1b3a3fdfb7
17 changed files with 318 additions and 69 deletions

View File

@ -0,0 +1,25 @@
package com.qiaoba.common.redis.config;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
/**
* @author ailanyin
* @version 1.0
* @since 2023/7/17 10:24
*/
@Component
@RequiredArgsConstructor
public class RedisSubConfig {
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory factory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(factory);
return container;
}
}

View File

@ -0,0 +1,32 @@
package com.qiaoba.common.redis.manager;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
/**
* RedisChannelManager
*
* @author ailanyin
* @version 1.0
* @since 2023/7/17 10:18
*/
@Component
@RequiredArgsConstructor
public class RedisChannelManager {
private final RedisMessageListenerContainer messageListenerContainer;
public void addChannelListener(String channelName, MessageListener listener) {
ChannelTopic topic = new ChannelTopic(channelName);
messageListenerContainer.addMessageListener(listener, topic);
messageListenerContainer.start();
}
public void removeChannelListener(String channelName, MessageListener listener) {
ChannelTopic topic = new ChannelTopic(channelName);
messageListenerContainer.removeMessageListener(listener, topic);
}
}

View File

@ -141,6 +141,25 @@ public interface RedisService {
*/
Map<Object, Object> hGetAll(String key);
/**
* 往hash集合里面插入一个值
*
* @param key key
* @param hashKey hashKey
* @param value value
* @param clazz clazz
*/
<T> void addHashList(String key, String hashKey, T value, Class<T> clazz);
/**
* 获取hash集合数据
*
* @param key key
* @param clazz clazz
* @return Map<String, List < T>>
*/
<T> Map<String, List<T>> getHashList(String key, Class<T> clazz);
/**
* 获取整个hash结构的大小
*
@ -353,6 +372,14 @@ public interface RedisService {
*/
Collection<String> getKeys(String key);
/**
* 发布消息
*
* @param channel 管道 (类似于topic)
* @param message 消息内容
*/
void convertAndSend(String channel, String message);
/**
* get object
*

View File

@ -1,6 +1,7 @@
package com.qiaoba.common.redis.service.impl;
import cn.hutool.core.collection.ListUtil;
import com.qiaoba.common.base.constant.BaseConstant;
import com.qiaoba.common.base.constant.TenantConstant;
import com.qiaoba.common.base.context.BaseContext;
@ -102,6 +103,33 @@ public class RedisServiceImpl implements RedisService {
return redisTemplate.opsForHash().entries(addTenantPrefix(key));
}
@Override
@SuppressWarnings("unchecked")
public <T> void addHashList(String key, String hashKey, T value, Class<T> clazz) {
if (hHasKey(key, hashKey)) {
List<T> list = (List<T>) hGet(key, hashKey);
list.add(value);
hSet(key, hashKey, list);
} else {
hSet(key, hashKey, ListUtil.toList(value));
}
}
@Override
@SuppressWarnings("unchecked")
public <T> Map<String, List<T>> getHashList(String key, Class<T> clazz) {
if (hasKey(key)) {
Map<Object, Object> map = hGetAll(key);
Map<String, List<T>> returnMap = new HashMap<>(map.size());
for (Object tempKey : map.keySet()) {
returnMap.put(tempKey.toString(), (List<T>) map.get(tempKey));
}
return returnMap;
}
return null;
}
@Override
public Long hSize(String key) {
return redisTemplate.opsForHash().size(addTenantPrefix(key));
@ -240,6 +268,11 @@ public class RedisServiceImpl implements RedisService {
return redisTemplate.keys(addTenantPrefix(key));
}
@Override
public void convertAndSend(String channel, String message) {
redisTemplate.convertAndSend(channel, message);
}
@Override
@SuppressWarnings("unchecked")
public <T> T getObject(String key, Class<T> clazz) {

View File

@ -1,5 +1,7 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.qiaoba.common.redis.config.RedisConfig,\
com.qiaoba.common.redis.config.RedisSubConfig,\
com.qiaoba.common.redis.manager.RedisChannelManager,\
com.qiaoba.common.redis.service.impl.RedisServiceImpl