Performance: Redis as cache provider
This affects page load speed by reducing backend response time and lowering server load through fast data retrieval.
public String getData(String key) {
String cached = redis.get(key);
if (cached != null) return cached;
String data = database.query(key);
redis.set(key, data);
return data;
}public String getData(String key) {
// No caching, always fetch from database
return database.query(key);
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No cache, direct DB calls | N/A | N/A | High backend delay delays paint | [X] Bad |
| Redis cache for frequent data | N/A | N/A | Fast backend response speeds paint | [OK] Good |
@Cacheable("books")
public Book findBookById(Long id) {
System.out.println("Fetching from DB");
return bookRepository.findById(id).orElse(null);
}
What will be printed if findBookById(1L) is called twice in a row?@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
return RedisCacheManager.builder(factory).build();
}
If the Redis server is down, what will happen when the application tries to cache data?public List<User> getUsersByRole(String role) {
// fetch users
}Pass the test and this pattern moves unknown