|
@ -1,58 +1,58 @@
|
|
|
package com.yihu.wlyy.statistics.config;
|
|
|
|
|
|
import com.yihu.wlyy.statistics.controller.KeyExpiredListener;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.data.redis.listener.PatternTopic;
|
|
|
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
|
|
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
|
|
import redis.clients.jedis.Jedis;
|
|
|
import redis.clients.jedis.JedisPool;
|
|
|
import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2017/7/31.
|
|
|
*/
|
|
|
@Configuration
|
|
|
public class RedisConfig {
|
|
|
@Value("${spring.redis.host}")
|
|
|
private String host;
|
|
|
@Value("${spring.redis.port}")
|
|
|
private Integer port;
|
|
|
@Value("${spring.redis.password}")
|
|
|
private String password;
|
|
|
@Bean
|
|
|
@ConfigurationProperties(prefix = "spring.redis.pool")
|
|
|
JedisPoolConfig jedisPoolConfig() {
|
|
|
return new JedisPoolConfig();
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
|
|
|
System.out.println(jedisPoolConfig);
|
|
|
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
|
|
|
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
|
|
|
jedisConnectionFactory.setPort(port);
|
|
|
jedisConnectionFactory.setHostName(host);
|
|
|
jedisConnectionFactory.setPassword(password);
|
|
|
return jedisConnectionFactory;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
|
|
|
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
|
|
container.setConnectionFactory(connectionFactory);
|
|
|
//监听redis过期的生命周期
|
|
|
//过期监听的管道默认是__keyevent@0__:expired,艾特后面的0表示第几个是数据库,redis默认的数据库是0~15一共16个数据库。所以如果你存入的数据库是2,那么数据接收的管道就是__keyevent@2__:expired
|
|
|
container.addMessageListener(new KeyExpiredListener(connectionFactory), new PatternTopic("__key*__:*"));
|
|
|
return container;
|
|
|
}
|
|
|
}
|
|
|
//package com.yihu.wlyy.statistics.config;
|
|
|
//
|
|
|
//import com.yihu.wlyy.statistics.controller.KeyExpiredListener;
|
|
|
//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
//import org.springframework.beans.factory.annotation.Value;
|
|
|
//import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
//import org.springframework.context.annotation.Bean;
|
|
|
//import org.springframework.context.annotation.Configuration;
|
|
|
//import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
//import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
//import org.springframework.data.redis.listener.PatternTopic;
|
|
|
//import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
|
|
//import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
|
|
//import redis.clients.jedis.Jedis;
|
|
|
//import redis.clients.jedis.JedisPool;
|
|
|
//import redis.clients.jedis.JedisPoolConfig;
|
|
|
//
|
|
|
//import javax.annotation.PostConstruct;
|
|
|
//
|
|
|
///**
|
|
|
// * Created by chenweida on 2017/7/31.
|
|
|
// */
|
|
|
//@Configuration
|
|
|
//public class RedisConfig {
|
|
|
// @Value("${spring.redis.host}")
|
|
|
// private String host;
|
|
|
// @Value("${spring.redis.port}")
|
|
|
// private Integer port;
|
|
|
// @Value("${spring.redis.password}")
|
|
|
// private String password;
|
|
|
// @Bean
|
|
|
// @ConfigurationProperties(prefix = "spring.redis.pool")
|
|
|
// JedisPoolConfig jedisPoolConfig() {
|
|
|
// return new JedisPoolConfig();
|
|
|
// }
|
|
|
//
|
|
|
// @Bean
|
|
|
// JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
|
|
|
// System.out.println(jedisPoolConfig);
|
|
|
// JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
|
|
|
// jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
|
|
|
// jedisConnectionFactory.setPort(port);
|
|
|
// jedisConnectionFactory.setHostName(host);
|
|
|
// jedisConnectionFactory.setPassword(password);
|
|
|
// return jedisConnectionFactory;
|
|
|
// }
|
|
|
//
|
|
|
// @Bean
|
|
|
// RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
|
|
|
// RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
|
|
// container.setConnectionFactory(connectionFactory);
|
|
|
// //监听redis过期的生命周期
|
|
|
// //过期监听的管道默认是__keyevent@0__:expired,艾特后面的0表示第几个是数据库,redis默认的数据库是0~15一共16个数据库。所以如果你存入的数据库是2,那么数据接收的管道就是__keyevent@2__:expired
|
|
|
// container.addMessageListener(new KeyExpiredListener(connectionFactory), new PatternTopic("__key*__:*"));
|
|
|
// return container;
|
|
|
// }
|
|
|
//}
|