整合SpringBoot2.x + Redis

整合 Spring Boot 2.x + Redis

所需服务

本地启动Redis

端口号使用默认端口号6379

Spring Data Redis 单机无需配置,即可使用!

所需依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

直接开始使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.Serializable;

/**
* @author <a href ='jxh98@foxmail.com'>Josway</a>
* @date 2020/8/3
* @since JDK 1.8
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Resource
private RedisTemplate<String, Object> redisTemplate;

@Test
public void test() {
String key = "user:1";
redisTemplate.opsForValue().set(key, new User(1, "Josway", 18));
Object o = redisTemplate.opsForValue().get(key);
log.info("user:" + o.toString());

}
}

@Data
@AllArgsConstructor
class User implements Serializable {
private static final long serialVersionUID = -6680135988818656280L;
private Integer id;
private String name;
private Integer age;
}

Spring Cache 单机无需配置,即可使用!

所需依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

开始使用

@EnableCaching

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

/**
* @author <a href ='jxh98@foxmail.com'>Josway</a>
* @date 2020/6/28
* @since JDK 1.8</a>
*/
@SpringBootApplication
@EnableCaching
public class DroolsApplication {
public static void main(String[] args) {
SpringApplication.run(DroolsApplication.class, args);
}
}

@CacheConfig

@Cacheable
@CachePut
@CacheEvict 可以自动缓存。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import com.example.visualization.entity.Demo;
import com.example.visualization.mapper.DemoMapper;
import com.example.visualization.service.DemoService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author <a href ='jxh98@foxmail.com'>Josway</a>
* @date 2020/8/4
* @since JDK 1.8
*/
@CacheConfig(cacheNames = "demo")
@Service
public class DemoServiceImpl implements DemoService {

@Resource
private DemoMapper demoMapper;

@Override
public int deleteByPrimaryKey(Integer id) {
return demoMapper.deleteByPrimaryKey(id);
}

@Override
public int insert(Demo record) {
return demoMapper.insert(record);
}

@Override
public int insertSelective(Demo record) {
return demoMapper.insertSelective(record);
}

@Cacheable
@Override
public Demo selectByPrimaryKey(Integer id) {
return demoMapper.selectByPrimaryKey(id);
}

@CachePut(key = "#record.id")
@Override
public int updateByPrimaryKeySelective(Demo record) {
return demoMapper.updateByPrimaryKeySelective(record);
}

@CacheEvict
@Override
public int updateByPrimaryKey(Demo record) {
return demoMapper.updateByPrimaryKey(record);
}

}

整合SpringBoot2.x + Redis
https://blog.josway.cc/2022/04/25/yuque/整合SpringBoot2.x + Redis/
作者
JOSWAY
发布于
2022年4月25日
许可协议