springboot integrates redis

In the past few days, redis has been used in new projects. I searched the Internet and summarized it.

There are two types on the Internet, one is to use classes to configure and manage redis without configuration files, and the other is to write in yaml or properties

I feel that the way to uninstall yaml is more intuitive and simple. Let's briefly introduce it. It's very simple.

1. Our project uses springboot2.0, and then introduces redis dependencies in pom, the format is as follows

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2. Inject in your class

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

or

    @Autowired
    private RedisTemplate redisTemplate;

Then save the key, take the key, set the timeout time, etc., other types of usage Baidu will do, many.

使用:redisTemplate.opsForValue().set("name","tom");
Result: the output of redisTemplate.opsForValue().get("name") is tom

Oh, by the way, add the basic configuration file to properties

# Redis database index (default 0)
spring.redis.database=0
# Redis server address
spring.redis.host=127.0.0.1
# Redis server connection port
spring.redis.port=6379
# Redis server connection password (default is empty)
spring.redis.password=
# The maximum number of connections in the connection pool (use a negative value to indicate no limit)
spring.redis.pool.max-active=8
# Connection pool maximum blocking wait time (use a negative value to indicate no limit)
spring.redis.pool.max-wait=-1
# Maximum idle connections in the connection pool
spring.redis.pool.max-idle=8
# Minimum idle connection in the connection pool
spring.redis.pool.min-idle=0
# Connection timeout (ms)
spring.redis.timeout=5000

Then there is another way to search for all approximate keys

stringRedisTemplate.keys("xxx")

This usage must not be used in a production environment unless the amount of data is small. . .

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324551406&siteId=291194637