This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Include a column
Spring Boot quick start
Java full stack Architect
preface
After the development of the Internet to a certain scale, directly to the database reading has been unable to meet the needs of the Internet, so there is an efficient storage system, the data stored in memory, which speeds up the reading speed.
The initial
Remote Dictionary Server (Redis) is an open source, net-based, memory-based, persistent, logging, key-value database written in ANSI C, and provides apis in multiple languages. Redis and other key-value cache products have the following three features:
- Redis supports data persistence. Data stored in memory can be saved to disk, which can be reloaded and used upon restart.
- Redis supports not only simple key-value data, but also lists, sets, zsets, hash and other data structures.
- Redis supports data backup in master-slave mode.
The data type
Redis has five basic data types: string, hash, List, set, and zset.
- String: String is the most basic type of Redis. It is binary safe. The value of string can be stored up to 512MB.
- Hash: A mapping table of string fields and values. Hashes are particularly useful for storing objects.
- List: List is a simple list of strings, sorted in insertion order.
- Set: A set is an unordered set of type String. Collections are implemented in a hash table, so the complexity of adding, deleting, and finding is O(1).
- Ordered set (zset) : A zset, like a set, is a collection of string elements and does not allow duplicate members. All members of a Zset are ordered members.
Redis features
Redis features
- High performance
- Rich data types
- Speed is fast
- Data structure server based on key and value pairs
- Rich features (such as columns, subscriptions, pipelines, things, etc.)
- Simple and stable
- Support for multiple conversion languages
- persistence
- A master-slave replication
- High availability, distribution, high concurrency
Quick start
This article will integrate Redis based on Spring Boot and conduct relevant development and testing. Redis configuration items, imported dependencies and other related configurations are introduced for the first time and therefore need to be configured.
Introduction of depend on
This article builds the project based on Maven, so you need to introduce relevant POM dependencies. Related dependencies are as follows:
<! -- redis start--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <! -- redis end--> <! -- dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70< version> </dependency> <! -- fastjson -->Copy the code
Service configuration
Use the simplest Redis configuration file, including host server address information, Port server port information, and password server authentication information. Password can be left blank.
Redis.host =127.0.0.1 spring.redis.port=6379 spring.redis.password=Copy the code
RedisConfig
- @Configuration is used to define a Configuration class that defines dependencies between beans by calling other @Bean methods in the same class.
- ConditionalOnClass: a Bean is instantiated only if a class is on the classpath.
- @ EnableConfigurationProperties annotation is used to make effective use @ ConfigurationProperties annotation class.
- ConditionalOnMissingBean, which is an annotation that decorates a bean and ensures that you only have one bean.
package com.example.demo.config; import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @ClassName RedisConfig * @Description: RedisConfig config * @author JavaZhan @public id :Java full stack Architect * @date 2020/6/13 * @version V1.0 **/ @configuration @ConditionalOnClass(RedisOperations.class) @EnableConfigurationProperties(RedisProperties.class) public class RedisConfig { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); template.setValueSerializer(fastJsonRedisSerializer); template.setHashValueSerializer(fastJsonRedisSerializer); template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }}Copy the code
RedisUtils
Of course, when you integrate Redis, you can’t avoid using the RedisUtils utility class. Online related packaging of RedisUtils has been a lot of, you can according to the need, their own on the Internet to find a general RedisUtils tool class, this article will not post the code, are general code.
Start the Redis
Start local Redis.
DemoApplicationTests
package com.example.demo; import com.example.demo.controller.TestController; import com.example.demo.service.UserService; import com.example.demo.utils.RedisUtils; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @SpringBootTest(classes = DemoRedisApplication.class) @RunWith(SpringRunner.class) @Rollback @AutoConfigureMockMvc class DemoApplicationTests { @Resource private RedisUtils redisUtils; /** * @ClassName testRedis * @Description: TestRedis * @author JavaZhan * @date 2020/6/13 * @version V1.0 **/ @test void testRedis(){ System. The out. Println (" start "); redisUtils.set("test","this is test redis"); System.out.println(" get the result "+ redisutils.get ("test")); System. The out. Println (" end "); }}Copy the code
The execution result is as follows
conclusion
So Redis is successfully integrated with Spring Boot. More tests you can delve into Redis related information, I believe there will be a new continent found.
Author introduction: [little ajie] a love to tinker with the program ape, JAVA developers and enthusiasts. Public account [Java full stack architect] maintainer, welcome to pay attention to reading communication.
Well, thank you for reading, I hope you like it, if it helps you, welcome to like the collection. If there are any shortcomings, please comment and correct them. See you next time.