mall: redis project source code analysis

1. mall open source project

1.1 Source

mall learning tutorial , comprehensive analysis of architecture, business and technical points. The mall project ( 50k+star ) is an e-commerce system that is implemented using current mainstream technologies. Covers SpringBoot 2.3.0, MyBatis 3.4.6, Elasticsearch 7.6.2, RabbitMQ 3.7.15, Redis 5.0, MongoDB 4.2.5, Mysql5.7 and other technologies, using Docker container deployment.

Project github address: github.com

1.2 Project transfer

You can transfer projects on github to gitee for easy cloning to idea.

Specific steps are as follows:

Insert image description here

1.3 Project cloning

Since github is deployed abroad, although idea also supports pulling from github, the cloning speed is too slow, so it is recommended to clone the project to idea after importing gitee as mentioned above.

The specific cloning steps are too simple and routine. Readers can complete it by themselves, or search on Baidu~

2. Redis non-relational database

2.1 Introduction to Redis

Redis is an open source log-type Key-Value database written in ANSI C language, complies with the BSD protocol, supports the network, can be memory-based and persistent, and provides APIs in multiple languages. It is a high-performance key-value database. database.

It is often called a data structure server because values ​​can be of types such as String, Map, List, Sets, and Sorted Sets.

Redis and other key-value caching products have the following three characteristics:

  • Redis supports data persistence, which can keep data in memory on disk and can be loaded again for use when restarting.
  • Redis not only supports simple key-value type data, but also provides storage of data structures such as list, set, zset, and hash.
  • Redis supports data backup, that is, data backup in master-slave mode.

Redis development document address: Introduction to Redis_redis tutorial

Installing Redis is simple. Readers can install it by themselves on Baidu or by viewing the development documentation.

2.2 Usage process of distributed back-end projects

The figure below is a basic usage process summarized by myself based on project experience and Baidu search information.

The flow chart of using Redis is as follows:

Insert image description here

2.3 Usage scenarios of distributed backend projects

The picture below is a basic usage scenario summarized by myself based on project experience and Baidu search information.

The usage scenario diagram of Redis is as follows:

Insert image description here

2.4 Common caching problems

The picture below is a summary of common caching problems based on project experience and Baidu search information.

The cache problem diagram of Redis is as follows:

Insert image description here

3. Source code analysis

Looking at the source code, I summarized the basic steps when looking at a new project. First, look at integration and configuration, then analyze it from a business perspective, and combine the integrated framework and components to explore the system architecture in turn.

**Analysis content:** Most of the analysis content below is explained in the figure, and there will not be too much explanation outside.

3.1 Integration and configuration

Take the source code analysis directly and only analyze the parts related to Redis. Readers of other parts are asked to read the source code analysis by themselves.

**Project Startup:**Only need to start mall-tiny-redisthe module part.

**Required for startup:** Start mysql5, Redis service, create database, and import tables (the location of the sql file is in the documentfolder of the same level directory of the project).

**ps:** Remember to modify the configuration information of the database connection and the connection information of Redis.

3.1.1 Import dependencies

pom文件Import Redis related dependencies in .

Insert image description here

3.1.2 Add configuration

application.ymlAdd Redis related configuration in .
Insert image description here

3.1.3 Global cross-domain configuration

Insert image description here

3.2 Redis test

3.2.1 Redis configuration class

The Redis configuration class implements the configuration and initialization of Redis, including creating RedisTemplate objects, configuring the Redis serializer, and setting the Redis cache validity period. Through these configurations, applications can easily use Redis for caching operations.

1、Redis的配置和初始化工作

Insert image description here

2、Redis序列化器

Insert image description here

3、管理Redis缓存的读写操作和生命周期

Insert image description here

3.2.2 Problem with swagger version during startup

1、出现问题

Springboot integrates swagger, and the error No mapping for GET /swagger-ui.html occurs

Insert image description here

2、解决办法

Inherit the WebMvcConfigurationSupport class in the configuration class and override the addResourceHandlers method

Step 1 : Inherit

Insert image description here

Step 2 : Rewrite

Insert image description here

code show as below:

  @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

3、成功效果

Insert image description here

3.2.3 Test Redis cache

After the above episode, let’s get down to business.

1、测试简单缓存

存:redisTemplate.opsForValue().set(key, value)

取:redisTemplate.opsForValue().get(key)

Insert image description here

2、测试Hash结构的缓存

存:redisTemplate.opsForHash().putAll(key, map)

取:redisTemplate.opsForHash().entries(key)

Insert image description here

3. 测试Set结构的缓存(out of order)

存:redisTemplate.opsForSet().add(key, values)

删:redisTemplate.opsForSet().remove(key, values)

取:redisTemplate.opsForSet().members(key)

Insert image description here

4. 测试List结构的缓存(orderly)

Batch storage: redisTemplate.opsForList().rightPushAll(key, values)

删:redisTemplate.opsForList().remove(key, count, value)

取:redisTemplate.opsForList().range(key, start, end)

Insert image description here

5、Redis中的存储效果

When readers browsed earlier, they must have doubts. Isn't it about the operation of Redis? Why don't you see the storage effect? ​​The belated rendering is as follows, plus some introductions.

Insert image description here

3.2.4 Test the cache of brand interface

Regarding the use of Redis cache for the brand interface, only querying brand details, deletion, and updating use Redis cache, and they are described in the form of annotations. Here I will give two examples, respectively querying brand details and updating brand information.

1、获取指定id的品牌详情

注解:@Cacheable(value = RedisConfig.REDIS_KEY_DATABASE, key = “‘pms:brand:’+#id”, unless = “#result==null”)

Rendering of testing under swagger :

Insert image description here

Business logic code:

Insert image description here

Redis rendering :

Insert image description here

2、更新指定id品牌信息

注解:@CacheEvict(value = RedisConfig.REDIS_KEY_DATABASE, key = “‘pms:brand:’+#id”)

Rendering of testing under swagger :

Insert image description here

Business logic code:

Insert image description here

Redis rendering :

Insert image description here

4. Summary

In this article, I first obtained the requirements from the actual project, so as to learn the Redis cache, combined with the source code to learn, and learned mallRedis from the open source project. I feel that I have gained a lot. I hope this article will be helpful to you.

In the future, I will also combine this framework to learn other technology stacks.

盈若安好,便是晴天

Guess you like

Origin blog.csdn.net/qq_51601665/article/details/132514142