Databases Reference
In-Depth Information
We can provide the CRUD operations for the
Contact
objects by implementing the
ContactService
interface. Let's start by creating a dummy service implementation
and adding or implementing the actual CRUD operations later. The steps needed to
create a dummy service implementation are described as follows:
1.
Implement the
ContactService
interface.
2.
Annotate the created class with the
@Service
annotation.
3.
Add the required dependencies as private members of the created class and
annotate these members with the
@Resource
annotation. We need to have a
reference to both the
RedisTemplate
and
RedisAtomicLong
objects.
The source code of our dummy service implementation is given as follows:
@Service
public class RedisContactService implements ContactService {
@Resource
private RedisAtomicLong contactIdCounter;
@Resource
private RedisTemplate<String, Contact> redisTemplate;
//Add methods here
}
We also have to implement some utility methods that are used by the methods
declared by the
ContactService
interface. These private methods are described in
the following table:
Method
Description
String buildKey(Long contactId)
Returns a key for a contact.
void persist(Contact persisted)
Saves the contact information to a string
value.
First, we have to implement a method that is used to build keys for the persisted
Contact
objects. The implementation of the
buildKey()
method is simple. We
build the key by appending the contact ID given as a parameter to a string
contact
and return the resulting string. The source code of the
buildKey()
method is given
as follows:
private String buildKey(Long contactId) {
return "contact" + contactId;
}