Junior — Middle
What data caching methods have you applied in your projects?
sobes.tech AI
Answer from AI
In Java projects, the following data caching methods were used:
-
Built-in cache in applications: using collections like
HashMaporConcurrentHashMapto store frequently requested data in memory. -
Caching with libraries: for example, using Ehcache, Caffeine, or Guava Cache, which provide advanced features for cache lifetime management, maximum size, and eviction policies.
-
Database-level caching: using built-in caching mechanisms or external solutions such as Redis or Memcached to store query results or sessions.
-
Spring Cache: if the project uses Spring Framework, the Spring Cache module was used with annotations
@Cacheable,@CachePut, and@CacheEvictfor transparent method caching.
Example of using Caffeine Cache:
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, java.util.concurrent.TimeUnit.MINUTES)
.build();
// Adding to cache
cache.put("key1", "value1");
// Retrieving from cache
String value = cache.getIfPresent("key1");