说明

  • 缓存注解 @Cacheable
  • 缓存失效注解 @CacheEvict

代码示例

TestCacheService.java

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
58
59
60
61
62
package com.ruoyi.test.service;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class TestCacheService {
public static final String TEST_CACHE = "testCache";

/**
* 缓存,key 包含 1 个参数
* testCache::xxx
* @param id
* @return
*/
@Cacheable(value = TEST_CACHE, key = "#id")
public String cache(String id) {
return System.currentTimeMillis() + ":cache:" + id;
}

/**
* 使指定缓存失效
* @param id
* @return
*/
@CacheEvict(value = TEST_CACHE, key = "#id")
public String cacheEvict(String id) {
return System.currentTimeMillis() + ":cacheEvict:" + id;
}

/**
* 缓存, key 包含多个参数
* testCache::xxx:yyy
* @param code
* @param key
* @return
*/
@Cacheable(value = TEST_CACHE, key = "#code + ':' + #key")
public String cache2(String code, String key) {
return System.currentTimeMillis() + ":cache2:" + code + ":" + key;
}

/**
* 使指定缓存失效
* @param code
* @param key
* @return
*/
@CacheEvict(value = TEST_CACHE, key = "#code + ':' + #key")
public String cacheEvict2(String code, String key) {
return System.currentTimeMillis() + ":cacheEvict2:" + code + ":" + key;
}

/**
* 使缓存 TEST_CACHE 下所有缓存失效
* @return
*/
@CacheEvict(value = TEST_CACHE, allEntries = true)
public String cacheEvictByBigKey() {
return System.currentTimeMillis() + ":cacheEvictByBigKey";
}
}

TestCacheController.java

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
58
59
60
61
package com.ruoyi.test;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.test.service.TestCacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author bowei
* @date 2024/12/2
*/
@Api("测试缓存")
@RestController
@RequestMapping("/testCache")
public class TestCacheController {
@Autowired
private TestCacheService testCacheService;

// 缓存,单个参数
@ApiOperation(value = "缓存,单个参数")
@RequestMapping("/cache")
@Anonymous
public String cache(String id) {
return testCacheService.cache(id);
}

// 缓存,多个参数
@ApiOperation(value = "缓存,多个参数")
@RequestMapping("/cache2")
@Anonymous
public String cache2(String code, String key) {
return testCacheService.cache2(code, key);
}

// 删除指定缓存, 单个参数
@ApiOperation(value = "删除指定缓存, 单个参数")
@RequestMapping("/cacheEvict")
@Anonymous
public String cacheEvict(String id) {
return testCacheService.cacheEvict(id);
}

// 删除指定缓存, 多个参数
@ApiOperation(value = "删除指定缓存, 多个参数")
@RequestMapping("/cacheEvict2")
@Anonymous
public String cacheEvict2(String code, String key) {
return testCacheService.cacheEvict2(code, key);
}

// 删除大 key 下所有缓存
@ApiOperation(value = "删除大 key 下所有缓存")
@RequestMapping("/cacheEvictByBigKey")
@Anonymous
public String cacheEvictByBigKey() {
return testCacheService.cacheEvictByBigKey();
}
}