Commit a7519b02 by danmo

添加redis 过期key监听事件,删除无用活码id

1 parent 46af908d
...@@ -44,6 +44,11 @@ public class WeConstans { ...@@ -44,6 +44,11 @@ public class WeConstans {
*/ */
public static final String WE_AGENT_ACCESS_TOKEN = "we_agent_access_token"; public static final String WE_AGENT_ACCESS_TOKEN = "we_agent_access_token";
public static final String WE_EMPLE_CODE_KEY = "we_emple_code_key";
/** /**
* 企业微信接口返回成功code * 企业微信接口返回成功code
*/ */
......
package com.linkwechat.framework.config; package com.linkwechat.framework.config;
import com.linkwechat.framework.listener.EmpleCodeExpiredListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
...@@ -20,6 +25,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -20,6 +25,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@EnableCaching @EnableCaching
public class RedisConfig extends CachingConfigurerSupport public class RedisConfig extends CachingConfigurerSupport
{ {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean @Bean
@SuppressWarnings(value = { "unchecked", "rawtypes" }) @SuppressWarnings(value = { "unchecked", "rawtypes" })
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
...@@ -40,4 +49,18 @@ public class RedisConfig extends CachingConfigurerSupport ...@@ -40,4 +49,18 @@ public class RedisConfig extends CachingConfigurerSupport
template.afterPropertiesSet(); template.afterPropertiesSet();
return template; return template;
} }
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer() {
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
return redisMessageListenerContainer;
}
@Bean
public EmpleCodeExpiredListener codeExpiredListener() {
return new EmpleCodeExpiredListener(this.redisMessageListenerContainer());
}
} }
package com.linkwechat.framework.listener;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.wecom.client.WeExternalContactClient;
import com.linkwechat.wecom.domain.WeEmpleCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import java.nio.charset.StandardCharsets;
/**
* @author danmo
* @description redis 键值过期监听
* @date 2021/2/8 17:24
**/
public class EmpleCodeExpiredListener extends KeyExpirationEventMessageListener {
@Autowired
private WeExternalContactClient weExternalContactClient;
public EmpleCodeExpiredListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
System.out.println("过期key:" + message.toString());
if(message.toString().contains(WeConstans.WE_EMPLE_CODE_KEY)){
weExternalContactClient.delContactWay(message.toString().split(":")[1]);
}
}
}
...@@ -3,6 +3,7 @@ package com.linkwechat.wecom.service.impl; ...@@ -3,6 +3,7 @@ package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkwechat.common.constant.WeConstans; import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.SecurityUtils; import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.SnowFlakeUtil; import com.linkwechat.common.utils.SnowFlakeUtil;
import com.linkwechat.common.utils.StringUtils; import com.linkwechat.common.utils.StringUtils;
...@@ -22,6 +23,7 @@ import org.springframework.stereotype.Service; ...@@ -22,6 +23,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -47,6 +49,9 @@ public class WeEmpleCodeServiceImpl extends ServiceImpl<WeEmpleCodeMapper, WeEmp ...@@ -47,6 +49,9 @@ public class WeEmpleCodeServiceImpl extends ServiceImpl<WeEmpleCodeMapper, WeEmp
@Autowired @Autowired
private IWeMaterialService materialService; private IWeMaterialService materialService;
@Autowired
private RedisCache redisCache;
/** /**
* 查询员工活码 * 查询员工活码
* *
...@@ -237,7 +242,10 @@ public class WeEmpleCodeServiceImpl extends ServiceImpl<WeEmpleCodeMapper, WeEmp ...@@ -237,7 +242,10 @@ public class WeEmpleCodeServiceImpl extends ServiceImpl<WeEmpleCodeMapper, WeEmp
Long[] departmentIdArr = Arrays.stream(departmentIds.split(",")) Long[] departmentIdArr = Arrays.stream(departmentIds.split(","))
.filter(StringUtils::isNotEmpty) .filter(StringUtils::isNotEmpty)
.map(Long::new).toArray(Long[]::new); .map(Long::new).toArray(Long[]::new);
return getQrcode(userIdArr,departmentIdArr); WeExternalContactDto qrcode = getQrcode(userIdArr, departmentIdArr);
//设置24小时过期
redisCache.setCacheObject(WeConstans.WE_EMPLE_CODE_KEY+":"+qrcode.getConfig_id(),qrcode.getConfig_id(),24, TimeUnit.HOURS);
return qrcode;
} }
@Override @Override
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!