Home > Database > Redis > Redis cache example code analysis

Redis cache example code analysis

王林
Release: 2023-06-03 20:37:56
forward
1508 people have browsed it

1. Introduction

1. Scenario

Since the data dictionary does not change very frequently, and the system accesses the data dictionary more frequently, it is necessary for us to store the data in the data dictionary. into the cache to reduce database pressure and increase access speed. Here, we use Redis as the distributed cache middleware of the system.

2. RedisTemplate

In the Spring Boot project, Spring Data Redis is integrated by default. Spring Data Redis provides a very convenient operation template for Redis, RedisTemplate, and can automatically manage the connection pool.

2. Introduction of Redis

1. Integrate Redis in the project

Add redis dependency in the service-base module. Spring Boot 2.0 and above connect to Redis through the commons-pool2 connection pool by default

<!-- spring boot redis缓存引入 -->
<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 存储 json序列化 -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
Copy after login

2. Add Redis connection configuration

Add the following configuration to application.yml of service-core

#spring:
redis:
host: 192.168.100.100
port: 6379
database: 0
password: 123456 #Default is empty
timeout: 3000ms #Maximum waiting time, an exception will be thrown if it times out, otherwise the request will wait forever
lettuce:
pool:
max-active: 20 #Maximum number of connections, negative value means no limit, default is 8
max-wait: -1 #Maximum blocking waiting time, negative value means no limit, Default -1
max-idle: 8 #Maximum idle connection, default 8
min-idle: 0 #Minimum idle connection, default 0

3. Start Redis service

Connect to the Linux server remotely, here use redis on the centos virtual machine locally

#Start the service
cd /usr/local/redis-5.0.7
bin/redis- server redis.conf

3. Test RedisTemplate

1. Store value test

Create test class RedisTemplateTests

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTemplateTests {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private DictMapper dictMapper;
    @Test
    public void saveDict(){
        Dict dict = dictMapper.selectById(1);
        //向数据库中存储string类型的键值对, 过期时间5分钟
        redisTemplate.opsForValue().set("dict", dict, 5, TimeUnit.MINUTES);
    }
}
Copy after login

found that RedisTemplate defaults The JDK serialization method is used to store the key and value, which has poor readability

Redis cache example code analysis

2. Add RedisConfig to the Redis configuration file

service-base. We You can configure the Redis serialization scheme in this configuration file

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //首先解决key的序列化方式
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //解决value的序列化方式
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        //序列化时将类的数据类型存入json,以便反序列化的时候转换成正确的类型
        ObjectMapper objectMapper = new ObjectMapper();
        //objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        // 解决jackson2无法反序列化LocalDateTime的问题
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.registerModule(new JavaTimeModule());
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }
}
Copy after login

Test again, the key uses string storage, and the value uses json storage

Redis cache example code analysis

3. Value test

@Test
public void getDict(){
    Dict dict = (Dict)redisTemplate.opsForValue().get("dict");
    System.out.println(dict);
}
Copy after login

4. Store the data dictionary in redis

DictServiceImpl

Note: When the redis server goes down, we should not throw an exception and execute it normally. The following process enables the business to run normally

@Resource
private RedisTemplate redisTemplate;
@Override
public List<Dict> listByParentId(Long parentId) {
    //先查询redis中是否存在数据列表
    List<Dict> dictList = null;
    try {
        dictList = (List<Dict>)redisTemplate.opsForValue().get("srb:core:dictList:" + parentId);
        if(dictList != null){
            log.info("从redis中取值");
            return dictList;
        }
    } catch (Exception e) {
        log.error("redis服务器异常:" + ExceptionUtils.getStackTrace(e));//此处不抛出异常,继续执行后面的代码
    }
    log.info("从数据库中取值");
    dictList = baseMapper.selectList(new QueryWrapper<Dict>().eq("parent_id", parentId));
    dictList.forEach(dict -> {
        //如果有子节点,则是非叶子节点
        boolean hasChildren = this.hasChildren(dict.getId());
        dict.setHasChildren(hasChildren);
    });
    //将数据存入redis
    try {
        redisTemplate.opsForValue().set("srb:core:dictList:" + parentId, dictList, 5, TimeUnit.MINUTES);
        log.info("数据存入redis");
    } catch (Exception e) {
        log.error("redis服务器异常:" + ExceptionUtils.getStackTrace(e));//此处不抛出异常,继续执行后面的代码
    }
    return dictList;
}
Copy after login

Summary of integrated redis:

(1) Import related dependencies;

(2) Configure redis connection information;

(3) Test connection, value test, and value storage test;

(4) Configure the serializer according to your own needs, otherwise the jdk serializer will be used by default.

redis business summary:

(1) First query whether there is corresponding cache information in redis, and if there is, it will be retrieved and returned directly without execution (2), if redis is connected for some reason If there is no downtime, print the error log at this time and continue to query the database;

(2) If not, query the database, store the data in redis and return the data.

The above is the detailed content of Redis cache example code analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template