Databases Reference
In-Depth Information
Second, we have to implement a persist() method that saves the contact
information. We can do this by performing the following steps:
1.
If the contact ID is null, get a new ID and set the received Long object as an
ID of the Contact object.
2.
Create a key for the contact.
3.
Save the contact information as a string value.
We use value operations because we need to execute
only one operation.
The source code of the persist() method is given as follows:
private void persist(Contact persisted) {
Long id = persisted.getId();
if (id == null) {
id = contactIdCounter.incrementAndGet();
persisted.setId(id);
}
String key = buildKey(persisted.getId());
redisTemplate.opsForValue().set(key, persisted);
}
We are now ready to start implementing the CRUD operations for contacts. Let's
move on and find out how it is done.
Create
We can implement a method that adds new contacts by following these steps:
1.
Save the added contact.
2.
Add the contact information into the contact set.
3.
Return the added contact.
The source code of the add() method is given as follows:
@Override
public Contact add(Contact added) {
persist(added);
redisTemplate.opsForSet().add("contacts", added);
return added;
}
 
Search WWH ::




Custom Search