在将多个线程与 spring-redis-data 一起使用时,我遇到了一个大问题,而且它很容易重现,以至于我认为我错过了一些微不足道的东西。
如果我在执行保存操作时查询 CrudRepository,有时(高达 60%)在 Redis 上找不到记录。
尽管可以在上面的链接中找到完整的代码,但以下是主要组件:
@Repository
public interface MyEntityRepository extends CrudRepository<MyEntity, Integer> {
}
Run Code Online (Sandbox Code Playgroud)
@RedisHash("my-entity")
public class MyEntity implements Serializable {
@Id
private int id1;
private double attribute1;
private String attribute2;
private String attribute3;
Run Code Online (Sandbox Code Playgroud)
@GetMapping( "/my-endpoint")
public ResponseEntity<?> myEndpoint () {
MyEntity myEntity = new MyEntity();
myEntity.setAttribute1(0.7);
myEntity.setAttribute2("attr2");
myEntity.setAttribute3("attr3");
myEntity.setId1(1);
myEntityRepository.save(myEntity);//create it in redis
logger.info("STARTED");
for (int i = 0; i < …Run Code Online (Sandbox Code Playgroud)