What is the value of project construction

Redis is an open source storage data store based on key-value pairs. Of course, Redis can also be used to cache data.

1. Spring support for Redis

(1) configuration
Spring for Redis support is also implemented through Spring Data Redis. Spring Data JPA offers us connection-related ConnectionFactory and data operation-related RedisTemplate. In particular, it should be noted that Spring Data Redis has only tested the Redis 2.6 and 2.8 versions.

According to Redis' various Java clients, Spring Data Redis offers the following ConnectionFactory:
JedisConnectionFactory: Use Jedis as a Redis client
JredisConnectionFactory: Use Jredis as a Redis client
LettuceConnectionFactory: Use Lettuce as a Redis client
SrpConnectionFactory: Use the Spulara / redis protocol as a Redis client

The configuration method is as follows:

The RedisTemplate configuration method is as follows:

(2). Use
Spring Data Redis provides us with two templates, RedisTemplate and StringRedisTemplate, for data manipulation. Among these, StringRedisTemplate only processes data whose keys are all characters.

The main data access methods of RedisTemplate and StringRedisTemplate are as follows:
opsForValue (): edit data with simple attributes
opsForList (): Execute data with a list
opsForSet (): The process only has specified dates
opsForZSet (): The operation contains only ZSet data (ordered set)
opsForHash (): The process only contains hash data

(3) Define the serializer
When our data is stored in Redis, our keys and values ​​are serialized to the database via the serializer provided by Spring.
RedisTemplate uses JdkSerializationRedisSerializer by default, StringRedisTemplate uses StringRedisSerializer by default.

2. Spring Boot support for Redis

Spring Boot support for Redis is in the org.springframework.boot.autoconfigure.redis package.

RedisAutoConfiguration configures JedisConnectionFactory, RedisTemplate and StringRedisTemplate for us by default, so we can use Redis directly as a data store.
RedisProperties shows us that Redis can be configured in application.properties using properties preceded by "spring.redis". The main characteristics are as follows:

3. Actual fight

(1) Install Redis
Non-Docker installation: Gohttp: //redis.io/download Download the appropriate version from Redis
Docker installation: First download the Redis image, then run the container with the following command
Docker run -d -p 6379: 6379 redis redis-server
and configure the port mapping in VirtualBox as shown in the picture:

Redis data management can use the Redis client. The download address is https://github.com/caoxinyu/RedisClient, As shown in the picture after installation:

(2) Create a new Spring Boot project
To create a Spring Boot project, the dependencies are Redis (Spring-Boot-Starter-Redis) and Web (Spring-Boot-Starter-Web).

The project structure and configuration are as follows:

(3) code

Domain model class:

The methods for calling up and setting are omitted

Code explanation:
This class must implement the serialization interface because using Jackson to serialize requires an empty constructor.

Data access:

Code explanation:
1. Spring Boot has configured StringRedisTemplate for us, which can be injected here directly
2. Spring Boot has configured RedisTemplate for us, which can be injected directly
3. You can use the @Resource annotation to specify stringRedisTemplate, which can be used to insert simple string-based attribute operation methods
4. You can use the @Resource annotation to specify redisTemplate, which can be used to insert simple object-based attribute operation methods

construction
Spring Boot will automatically configure RedisTemplate for us, and RedisTemplate will use JdkSerializationRedisSerializer, which is not intuitive for the Redis client we were demonstrating, as JdkSerializationRedisSerializer uses it to store data right here we will automatically configure RedisTemplate and define serializers. Display code as follows:

Code explanation:
1. Set the serialization of the value (value) with Jackson2JsonRedisSerializer
2. Set the serialization of the key with StringRedisSerializer

Controller code:

(4) Run the test

Accesshttp: // localhost: 8084 / set Check the Redis client here. as follows:


Access http: // localhost: 8084 / getStr The page is displayed as follows:


Accesshttp: // localhost: 8084 / getPerson The page is displayed as follows: