Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

One, foreword

Redis must be small partners even if not used, but also often heard, in the work, redis use frequency is very high, flower brother today in detail to introduce the integration steps in SpringBoot

What is Redis

In layman’s terms, Redis is a database, running directly in memory, so it is very fast, and its concurrency is very strong. Redis is in the form of key-value pairs (e.g. “name”:huage). There are five common types of keys:

  • String: String
  • Hash: dictionary
  • List the List:
  • Set the Set:
  • SortSet: Ordered set

In addition, Redis also has some advanced data structures, such as HyperLogLog, Geo, Pub/Sub, BloomFilter, RedisSearch and so on. This later Gie will have a special series to explain, here will not be expanded (otherwise 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> <! Clients </groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>Copy the code
  • The configuration file
Database =0 #redis server address Spring.redis. Host =127.0.0.1 # Redis server connection port Port =6379 # Redis server connection password (default: empty) Spring. Redis. Jedis. Pool. Max - active = 1024 # connection pool maximum blocking the wait time (use a negative value indicates no limit) spring. Redis. Jedis. Pool. The Max - wait = 10000 # the maximum idle connections in the connection pool Spring. Redis. Jedis. Pool. Max - idle = 200 # connection pool in minimum free connection spring. Redis. Jedis. Pool. Min - idle = 0 # connection timeout (ms) Spring.redis. timeout=10000 # redis. block-lay-exhausted =trueCopy the code
  • Initialize the configuration file
Jedis public JedisPool redisPoolFactory() throws Exception {JedisPoolConfig JedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); / / connection runs out whether blocking, false anomaly, true block until timeout, the default true jedisPoolConfig. SetBlockWhenExhausted (blockWhenExhausted); / / whether to enable the pool JMX management functions, the default true jedisPoolConfig. SetJmxEnabled (true); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); return jedisPool; }Copy the code

Third, code demonstration

After completing the above configuration, we only need to use @autoWired to introduce RedisTemplate, which can easily access Redis. In addition, Gie added a RedisUtil tool class in the project, which contains most of the commands of Redis, enough for daily development.

// add redis@autoWired private RedisTemplate; / / will be deposited in the redis name: flower elder brother 】 【 redisTemplate. OpsForValue (). The set (" name ", "flower elder brother"); / / remove the redis redisTemplate. The key is the name of the data in opsForValue () get (" name ");Copy the code