Caching strategies

Optimizing Code in Java

Pavlos Kosmetatos

Lead Engineer @Wealthyhood

What is caching?

Caching is like keeping our frequently used cooking ingredients on the kitchen counter instead of in the cupboard - it's faster to access, but we have limited counter space

We commonly cache:

  • Database query results
  • API responses
  • Expensive calculations
  • Other resource-intensive operations
Optimizing Code in Java

In-memory caching in Java

// In-memory cache using HashMap
public class SimpleCache<K, V> {
    private final Map<K, V> cache = new HashMap<>();

    public V get(K key) {
        return cache.get(key);
    }

    public void put(K key, V value) {
        cache.put(key, value);
    }
}
Optimizing Code in Java

Cache eviction policies

  • LRU (Least Recently Used)
  • LFU (Least Frequently Used)
  • FIFO (First In, First Out)
  • Time-based expiration
  • And more!
Optimizing Code in Java

Redis for distributed caching

What is Redis:

  • In-memory data store/cache
  • Excels as a distributed cache
  • Supports many data structures
  • Many Java client libraries - one example is Jedis

Screenshot 2025-05-14 at 6.59.29?PM.png

Optimizing Code in Java

Using Redis with Jedis

import redis.clients.jedis.Jedis;

// Using Jedis client
Jedis jedis = new Jedis("localhost");
jedis.set("key", "value");
String value = jedis.get("key");

Additional features:

  • Automatic expiration of cached items
  • Cluster support
Optimizing Code in Java

Implementing a time-based cache with Redis

public class RedisTimedCache {
    private final Jedis jedis;

    public RedisTimedCache(String host, int port) {
        this.jedis = new Jedis(host, port);
    }

    public String get(String key) {
        return jedis.get(key);
    }

    public void put(String key, String value, int timeToLiveSeconds) {
        // Sets both the value and expiration time in seconds
        jedis.setex(key, timeToLiveSeconds, value);
    }
}
Optimizing Code in Java

Summary

  • Caching stores computed results to avoid recalculation
  • Effective for expensive operations and frequently accessed data
  • Implement proper eviction policies to manage memory
  • Consider distributed caching for multi-server applications (e.g. Redis with Jedis)
Optimizing Code in Java

Let's practice!

Optimizing Code in Java

Preparing Video For Download...