spring boot redis
3861 ワード
dependency
application-redis.properties
Test
ソースコード-RedisProperties
ソースコード-RedisAutoConfiguration
参照先:https://blog.csdn.net/abombhz/article/details/78123253?locationNum=6&fps=1
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
application-redis.properties
######### #########
spring.redis.database=0
#spring.redis.url=redis://user:[email protected]:6379
spring.redis.host=
spring.redis.port=
spring.redis.password=
spring.redis.timeout=30000
spring.redis.ssl=false
#########redis #########
#spring.redis.sentinel.master=
#spring.redis.sentinel.nodes=
#spring.redis.cluster.max-redirects=
#spring.redis.cluster.nodes=
#########jedis client #########
#spring.redis.jedis.pool.max-active=
#spring.redis.jedis.pool.max-idle=
#spring.redis.jedis.pool.min-idle=
#spring.redis.jedis.pool.max-wait=
#########lettuce client ( )#########
spring.redis.lettuce.pool.max-active=10
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=1
spring.redis.lettuce.pool.max-wait=20000
spring.redis.lettuce.shutdown-timeout=10
Test
@RunWith(SpringRunner.class)
@SpringBootTest
public class AnnotationAppContextTest {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void setTest(){
redisTemplate.opsForValue().set("key1","val1", 100);
String key1 = redisTemplate.opsForValue().get("key1");
System.err.println("=============="+key1);
}
}
ソースコード-RedisProperties
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
/**
* Database index used by the connection factory.
*/
private int database = 0;
/**
* Connection URL. Overrides host, port, and password. User is ignored. Example:
* redis://user:[email protected]:6379
*/
private String url;
/**
* Redis server host.
*/
private String host = "localhost";
/**
* Login password of the redis server.
*/
private String password;
/**
* Redis server port.
*/
private int port = 6379;
/**
* Whether to enable SSL support.
*/
private boolean ssl;
/**
* Connection timeout.
*/
private Duration timeout;
private Sentinel sentinel;
private Cluster cluster;
private final Jedis jedis = new Jedis();
private final Lettuce lettuce = new Lettuce();
}
ソースコード-RedisAutoConfiguration
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate
参照先:https://blog.csdn.net/abombhz/article/details/78123253?locationNum=6&fps=1