: redis keyspace notifications( )
redis 2.8 , reids 2.8 ;
(A) :
1、 ,
2、redis keyspace notifications key ,
(B) :
1、 reids (redis.conf)
redis keyspace notifications, cpu
:
E:keyevent , __keyevent@__ ;
x: , ;
:
notify-keyspace-events "Ex"
2、 redis , 。
:
[[email protected] ~]$ redis-cli
127.0.0.1:6379> psubscribe __keyevent@0__:expired
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "__keyevent@0__:expired"
3) (integer) 1
key :
[[email protected] ~]$ redis-cli
127.0.0.1:6379> set testKey 123 PX 100
OK
127.0.0.1:6379>
:
[[email protected] ~]$ redis-cli
127.0.0.1:6379> psubscribe __keyevent@0__:expired
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "__keyevent@0__:expired"
3) (integer) 1
1) "pmessage"
2) "__keyevent@0__:expired"
3) "__keyevent@0__:expired"
4) "testKey"
!
(C)springBoot
1、 application.properties
spring.redis.host=127.0.0.1
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.timeout=3000
spring.redis.password=123456
spring.redis.port=6379
2、 com.xxx.xxx RedisConfiguration
package com.xxx.xxx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Created by test on 2017/2/12.
*/
@Component
public class RedisConfiguration extends CachingConfigurerSupport {
Logger logger = LoggerFactory.getLogger(RedisConfiguration.class);
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.password}")
private String password;
@Bean
public JedisPool redisPoolFactory() {
logger.info("JedisPool !!");
logger.info("redis :" + host + ":" + port);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password);
return jedisPool;
}
}
3、 com.xxx.xxx redisKeyspace
Subscribe.java
package com.xxx.xxx.redisKeyspace;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.JedisPubSub;
/**
* Created by test on 2017/2/12.
*/
public class Subscribe extends JedisPubSub {
private static final Log log= LogFactory.getLog(Subscribe.class);
//
public void onPSubscribe(String pattern, int subscribedChannels) {
log.info("Subscribe-onPSubscribe>>>>>>>>>>>>>>>>>>>>>>>>"+pattern + "=" + subscribedChannels);
}
//
public void onPMessage(String pattern, String channel, String message) {
try {
log.info(pattern + "=" + channel + "=" + message);
//
}catch (Exception e){
e.printStackTrace();
}
}
}
3、 SubscribeThread.java
: CommandLineRunner,springBoot CommandLineRunner
package com.xxx.xxx.redisKeyspace;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* Created by test on 2017/2/13.
*/
@Component
public class SubscribeThread implements CommandLineRunner {
private Log log= LogFactory.getLog(SubscribeThread.class);
@Autowired
JedisPool jedisPool;
@Override
public void run(String... strings) throws Exception {
Jedis jedis= jedisPool.getResource();
try {
// reids
jedis.psubscribe(new Subscribe(), "*");
} catch (Exception e) {
jedis.close();
e.printStackTrace();
}finally {
jedis.close();
}
}
}
4、具体的な業務でredisにkeyを追加する/**
* redis
* @param seconds
*/
private void addRedisTimer(String key,int seconds) {
Jedis jedis = jedisPool.getResource();
try {
if (!jedis.exists(key))
jedis.setex(key,seconds,"");
}catch (Exception e){
logger.error("addRedisTimer error: ", e);
}finally {
jedis.close();
}
}
これによりspringbootが起動するとreidsのkeyspace notificationsイベントを傍受するスレッドを開き、イベントを受信して対応するビジネスコードを処理することができます.