← Back to Home

Module 4: In-Memory Caching

Learning Objectives

Introduction to In-Memory Caching

In-memory caching is a technique that stores frequently accessed data in memory to improve application performance. By reducing the need to fetch data from slow storage systems or recompute complex calculations, caching can significantly improve response times.

In this module, we'll explore different caching strategies, implementation techniques, and best practices for effective cache management in Java applications.

Caching Fundamentals

Key Caching Concepts

Benefits of Caching

Implementing a Simple Cache in Java

Here's a basic implementation of an in-memory cache using Java's HashMap with time-based expiration:

public class SimpleCache<K, V> {
    private final Map<K, CacheEntry<V>> cache = new HashMap<>();
    private final long defaultTtlMillis;
    private final int maxSize;
    
    public SimpleCache(long defaultTtlMillis, int maxSize) {
        this.defaultTtlMillis = defaultTtlMillis;
        this.maxSize = maxSize;
    }
    
    public V get(K key) {
        CacheEntry<V> entry = cache.get(key);
        
        // Cache miss
        if (entry == null) {
            return null;
        }
        
        // Entry expired
        if (entry.isExpired()) {
            cache.remove(key);
            return null;
        }
        
        // Cache hit
        return entry.getValue();
    }
    
    public void put(K key, V value) {
        put(key, value, defaultTtlMillis);
    }
    
    public void put(K key, V value, long ttlMillis) {
        evictIfNeeded();
        cache.put(key, new CacheEntry<>(value, ttlMillis));
    }
    
    private void evictIfNeeded() {
        // Evict if cache is at capacity
        if (cache.size() >= maxSize) {
            // Simple strategy: remove oldest entry
            // More sophisticated: LRU, LFU, etc.
            K oldestKey = findOldestEntry();
            if (oldestKey != null) {
                cache.remove(oldestKey);
            }
        }
    }
    
    private K findOldestEntry() {
        // Implementation to find oldest entry
        // ...
    }
    
    private static class CacheEntry<V> {
        private final V value;
        private final long expiryTime;
        
        public CacheEntry(V value, long ttlMillis) {
            this.value = value;
            this.expiryTime = System.currentTimeMillis() + ttlMillis;
        }
        
        public boolean isExpired() {
            return System.currentTimeMillis() > expiryTime;
        }
        
        public V getValue() {
            return value;
        }
    }
}

Eviction Policies

When the cache reaches capacity, an eviction policy determines which items to remove:

The choice of policy depends on your application's specific access patterns and requirements.

Cache Size and TTL Considerations

Effects of Cache Size

Effects of TTL

The ideal settings balance memory usage, data freshness, and performance requirements.

Key Topics

Caching Fundamentals

Understanding the core concepts of caching.

  • Cache hits and misses
  • Time and space trade-offs
  • Cache sizing considerations

Cache Implementations

Learn about different caching implementations in Java.

  • HashMap-based caches
  • Guava Cache
  • Caffeine Cache

Eviction Strategies

Approaches to managing cache size and freshness.

  • Least Recently Used (LRU)
  • Time-based expiration
  • Size-based eviction

Cache Invalidation

Strategies for ensuring cache data remains fresh.

  • Write-through caching
  • Write-behind caching
  • Cache aside pattern

Performance Considerations

While caching can significantly improve application performance, it's important to consider several factors:

Effective caching strategies consider these factors to balance performance improvement against resource utilization and complexity.

Resources

In-Memory Caching Code-Along Starter

Starter code for the in-memory caching implementation.

In-Memory Caching Code-Along Solution

Solution code for the in-memory caching implementation.

Amazon Gaming Membership Project

Project demonstrating caching in a real-world scenario.

Code-Alongs

Additional code-along exercises for this sprint.

Sprint Challenge

Access the sprint challenge for this unit.