Databases Reference
In-Depth Information
Update
We can update the information of an existing contact by following these steps:
1.
Get the old contact information from Redis.
2.
Save the updated contact information.
3.
Remove the old contact information from the contact set. This ensures that
our set does not contain duplicate entries for the same contact.
4.
Add the updated contact information to the contact set.
5.
Return the updated contact.
The source code of the update() method is given as follows:
@Override
public Contact update(Contact updated) throws NotFoundException {
Contact old = findById(updated.getId());
persist(updated);
redisTemplate.opsForSet().remove("contacts", old);
redisTemplate.opsForSet().add("contacts", updated);
return updated;
}
Delete
We can delete contact information by following these steps:
1.
Find the deleted contact by calling the findById() method. This ensures that
NotFoundException is thrown if the contact is not found.
2.
Build a key used to get the contact information.
3.
Remove the deleted contact from the contact set.
4.
Remove the JSON representation of the deleted contact.
5.
Return the deleted contact.
The source code of the delete() method is given as follows:
@Override
public Contact deleteById(Long id) throws NotFoundException {
Contact deleted = findById(id);
String key = buildKey(id);
 
Search WWH ::




Custom Search