Junior — Middle
Have you used caching mechanisms in your recent projects?
sobes.tech AI
Answer from AI
Yes, in my recent projects I have used caching mechanisms to improve performance and reduce the load on databases or external services.
Most commonly, I used:
- Spring Cache with the
@Cacheableannotation for automatic caching of method results. - External caches, such as Redis, for distributed caching.
Example of using Spring Cache:
@Service
public class UserService {
@Cacheable("users")
public User getUserById(Long id) {
// Long operation, e.g., database query
return userRepository.findById(id).orElse(null);
}
}
This approach allows reusing previously obtained data without repeated computational or query costs.