I have a service that searches the user by id
, email
, or username
. Also, it can update or delete user by id
.
@Service
public class UserService{
public User getUserByEmail(String email) { ... }
public User getUserByUsername(String usern) { ... }
@Cacheable(cacheNames = "user", key = "#id")
public User getUserById(Long id) { ... }
@CachePut(cacheNames = "user", key = "#id")
public User updateUser(Long id, String name, String phone) { ... }
@CacheEvict(cacheNames = "user", key = "#id")
public User deleteUserById(Long id) { ... }
}
The caching implementation works fine with this current implementation.
Now, what should I do to implement caching for getUserByEmail
and getUserByUsername
function as well? Since all of the getter functions point to the same cache table user
, is there a way to maintain the cache table with multiple keys (id, email, and username) such that I can use any of the key to get cached data?
If possible, how would that affect my @CachePut and @CacheEvict?