问题
I've taken a reference of the link : http://panuoksala.blogspot.com/2015/09/redis-many-to-many.html to developed below code. I've implemented some code, looks like nothing is achievable so far as
Get User 1 groups:
hget User:1 Groups
Does not yeild the results.
I wanted to get answer more from the Data Modelling perspectives. I've modeled code like below, but its not working as per my requirement -
Group.java
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("groups")
public class Group {
@Id
private Long groupId;
private String name;
private List<User> users;
}
And User.java
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("users")
public class User {
@Id
private Long userId;
private String name;
List<Group> groups;
}
UserGroupTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserGroupTest extends RepositoryTestSupport{
@Autowired UserRepository userRepository;
@Autowired GroupRepository groupRepository;
@Before
public void setUp() {
// User -> Group relations
Group group1 = Group.builder().groupId(1L).name("Nature-Group").build();
Group group2 = Group.builder().groupId(2L).name("Music-Group").build();
Group group3 = Group.builder().groupId(3L).name("Sports-Group").build();
User user1 = User.builder().userId(1L).name("John").groups(Arrays.asList(group1, group2)).build();
User user2 = User.builder().userId(2L).name("Sally").groups(Arrays.asList(group2, group3)).build();
User user3 = User.builder().userId(3L).name("Chris").groups(Arrays.asList(group3)).build();
userRepository.save(user1);
userRepository.save(user2);
userRepository.save(user3);
// second contains Group -> User
User myuser1 = User.builder().userId(1L).name("John").build();
User myuser2 = User.builder().userId(2L).name("Sally").build();
User myuser3 = User.builder().userId(3L).name("Chris").build();
Group mygroup1 = Group.builder().groupId(1L).name("Nature-Group").users(Arrays.asList(myuser1)).build();
Group mygroup2 = Group.builder().groupId(2L).name("Music-Group").users(Arrays.asList(myuser1, myuser2)).build();
Group mygroup3 = Group.builder().groupId(3L).name("Sports-Group").users(Arrays.asList(myuser2, myuser3)).build();
groupRepository.save(mygroup1);
groupRepository.save(mygroup2);
groupRepository.save(mygroup3);
}
@Test
public void test() {
}
}
To solve this problem we can use two different sets to keep track of relations.
First we store users and groups into sets. Then we store relations into two separate hash "structures". First one contains User -> Group relations and second contains Group -> User relations.
127.0.0.1:6379> HGET users:1 groups
(nil)
127.0.0.1:6379> HGET users:1 Groups
(nil)
127.0.0.1:6379> HGET users:1 Group
(nil)
How to model the POJO classes so that result of all this can be achieved ?
hmset User:1 Groups 1,2 (Store user to database)
hmset Group1 Users 1 (Store group to database)
Get User 1 groups:
hget User:1 Groups
Remove user from group:
hmset User:1 Group ""
hmset Group:1 Users: ""
?
来源:https://stackoverflow.com/questions/53265496/how-to-model-data-in-redis-for-values-complex-data-structure