Databases Reference
In-Depth Information
The source code of this method is given as follows:
private Contact buildContact(Long id) {
String key = buildKey(id);
return buildContact(key);
}
Fourth, we have to implement the method used to verify whether a contact in
question exists or not. Our implementation of the
contactDoesNotExist()
method
consists of the following steps:
1.
Create the key of the contact.
2.
Check if the key is found from the contacts set by calling the
isMember()
method of the
SetOperations
class, and passing the name of the set and the
key as parameters.
We use
setOperations
because we execute only
one command.
3.
Inverse the return value of the
isMember()
method and return the
inverted value.
The source code of this method is given as follows:
private boolean contactDoesNotExist(Long id) {
String key = buildKey(id);
return !redisTemplate.opsForSet().isMember("contacts", key);
}
Fifth, we have to implement the method that saves the information of a single
contact. Our implementation of the
persist()
method includes the following steps:
1.
If the persisted
Contact
object does not have an ID, create one calling the
incrementAndGet()
method of the
RedisAtomicLong
class and set the
received
Long
object as the contact ID.
2.
Build a key for the persisted contact.
3.
Save the contact in the hash.
4.
Return the persisted contact.