SpringBoot-Redis構成
8242 ワード
一、接続プールの構成等の情報
## redis
spring:
## redis
redis:
host: 127.0.0.1
port: 6379
#password: ## ( )
database: 0 ## redis , 0
timeout: 30000 ## ( )
jedis:
pool:
max-active: 8 ##
max-wait: 5000 ## (-1 )
max-idle: 8 ##
min-idle: 0 ##
二、配置RedisTemplate
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
/**
* redisTemplate jdkSerializeable, ,
*
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
//
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
三、新規データ
@Autowired
private RedisTemplate redisTemplate;
ValueOperations redisOperations = redisTemplate.opsForValue();
//String
redisOperations.set(key,value);
//setnx
redisOperations.set(key,value,time, );
四、購読とプッシュ
プッシュdemo:
redisTemplate.convertAndSend(String channel, Object message);
例:
redisTemplate.convertAndSend(channelValue, base64CommandJsonStr);
購読プッシュリファレンス:springboot入門–springboot統合redisメッセージパブリッシュ購読モードの実装