alibaba / alibaba/jetcache

在单元测试时,遇到无法更新缓存、删除缓存情况出现,想了解是什么原因?

Open
#892 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.6k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

yaml已配置 local、remote,代码示例:

```java
public interface UserService {
@Cached(name = "userCache-", key = "#userId", expire = 100)
User getUserById(String userId);

@CacheUpdate(name = "userCache-", key = "#user.userId", value = "#user")
void updateUser(User user);

@CacheInvalidate(name = "userCache-", key = "#userId")
void deleteUser(String userId);
}
```

单元测试:

```java
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) // 支持 jupiter 测试方法按指定 @Order(1) 顺序执行
public class UserServiceTest {

@Autowired
private UserService userService;

@Test
@Order(1)
public void testGetUserById() {
User user = userService.getUserById("111");
System.out.println(user);
}

@Test
@Order(2)
public void testUpdateUser() {
// 准备数据
User user = new User(); // 创建要更新的用户对象
user.setUserId("111");
user.setName("李四");
user.setAge(18);

// 更新用户
userService.updateUser(user);

// 获取用户
User user2 = userService.getUserById("111");
System.out.println(user2);
}

@Test
@Order(3)
public void testDeleteUser() {
// 准备数据
String userId = "111";

// 执行测试
userService.deleteUser(userId);
}
}
```

现象:
1、当运行整个`UserServiceTest`单元测试示例时,可以通过测试,能够正常写入redis缓存、修改redis缓存、删除redis缓存
2、当运行单个`UserServiceTest`中的某个方法的单元测试示例时,例如,先运行获取`testGetUserById()`(会写入redis缓存中),再运行`testUpdateUser()`(无法更新redis缓存),则运行失败,无法更新redis中缓存数据

更新or删除时报错信息:

```

Cache operation aborted because can't find cached definition

can't find cache definition with area=default name=userCache-, specified in public abstract void com.zja.cache.jetcache.service.UserService.updateUser(com.zja.cache.jetcache.model.User)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the UserService interface annotations and the UserServiceTest methods testGetUserById, testUpdateUser, and testDeleteUser. Reproduce the isolated test sequence and compare it with the full test class run, using the reported “can't find cached definition” message as the completion criterion for explaining the update and delete behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, redis
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.