redis-om-spring
redis-om-spring copied to clipboard
upsert support
there are incrBy can easily do some plus on fields and no need manually add lock, which is perfect.
Any solution can achieve the upsert (like Mongo) function, say if obj not exists , then new object and do incrBy on some fields ? Eg. there is a Document call PostDoc:
@Document public class PostDoc{ @Id String id;
@Indexed String userId;
@Indexed LocalDate date;
@Indexed long likeNum;
@Indexed long viewNum; ........ }
the id is generated by hash(userId+date), There will by many thread come to call my function to add number on likeNum or viewNum, but for the first rows save in redisjson , I need check this id existing or not , if not existing, save it first and then call incrBy like:
entityStream.of(PostDoc.class).filter(PostDoc$.ID.eq(id)).forEach(PostDoc$.LIKE_NUM.incrBy(1L));
so i need check the id existing or not and also need consider lock , if there are upsert function , then redisjson itself will do insert /update which will reduce such code complex
There might be a way to do something with pipelining and/or Redis transactions, let me think about it
@guos so something like:
entityStream.of(PostDoc.class)
.filter(PostDoc$.ID.eq(id))
.findFirst()
.orElse(.....); // <=== something in here to create a PostDoc
.forEach(PostDoc$.LIKE_NUM.incrBy(1L));
To stick close to Java Streams semantics