Sample code for SpringBoot integration with redis

  • 2021-12-04 10:03:23
  • OfStack

Preface to the table of contents 1. What is redis 2. Integrate redis Step 3. Code demo

Preface

redis must be often heard by friends even if they have not used it. In work, redis is used very frequently. Today, I will introduce the integration steps in SpringBoot in detail

1. What is redis

In popular terms, redis is a database, which runs directly in memory, so its running speed is quite fast, and its concurrency is also very strong. redis exists in the form of key-value key value pairs (e.g., "name": huage), and its key has five common types:

String: String Hash: Dictionary List: List Set: Collection SortSet: Ordered Set

In addition, redis also has one advanced data structure, such as HyperLogLog, Geo, Pub/Sub, BloomFilter, RedisSearch, etc. This flower Gie will have a special series to explain, so it will not be expanded here (otherwise the liver will not be finished).

2. Integrate redis steps

pom File Configuration


<!--redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--jedis-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

Configuration file


#redis Configuration start 
# Redis Database index (default is 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 blank) 
spring.redis.password=
#  Maximum number of connections in connection pool (use negative value to indicate no limit) 
spring.redis.jedis.pool.max-active=1024
#  Maximum blocking wait time of connection pool (use negative value to indicate no limit) 
spring.redis.jedis.pool.max-wait=10000
#  Maximum free connection in connection pool 
spring.redis.jedis.pool.max-idle=200
#  Minimum free connection in connection pool 
spring.redis.jedis.pool.min-idle=0
#  Connection timeout (milliseconds) 
spring.redis.timeout=10000
#redis End of configuration 
spring.redis.block-when-exhausted=true

Initialize configuration file


// Initialization jedis
public JedisPool redisPoolFactory() throws Exception {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxIdle(maxIdle);
    jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
    //  Is the connection blocked when it is exhausted , false Report an abnormality ,ture Blocking until timeout ,  Default true
    jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
    //  Enable or not pool Adj. jmx Management function ,  Default true
    jedisPoolConfig.setJmxEnabled(true);
    JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
    return jedisPool;
}

3. Code demo

After completing the above configuration, we only need to use @ Autowired to introduce RedisTemplate, which can easily access redis. In addition, Gie has added an RedisUtil tool class in the project, including most commands of redis, which is enough for normal development and use.


// Introduce redis
@Autowired
private RedisTemplate redisTemplate;
​
// Will " name: Brother Hua "   Deposit redis
redisTemplate.opsForValue().set("name"," Brother Hua ");
// Take out redis Medium key For name Data of 
redisTemplate.opsForValue().get("name");

Related articles: