Learnitweb

Expiring Individual Fields in Redis Hashes Using Redisson

Redis allows expiring entire keys, but individual fields in a hash cannot be expired by default. Redisson provides a solution through RMapCache, which supports per-field expiration. This is extremely useful when you want different fields in the same map to have different lifetimes.


1. Introduction to RMapCache

RMapCache is an extension of RMap that provides:

  • All operations of RMap (get, put, remove, putAll, etc.)
  • Ability to set time-to-live (TTL) for individual entries
  • Automatic eviction of expired entries using background threads in Redisson

Key points:

  • Each entry can have a different TTL
  • Expired entries are automatically removed from the map
  • Useful for caching scenarios where fields have different lifetimes

2. Setting Up the Test Class

Create a dedicated class to test map caching:

import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import java.util.concurrent.TimeUnit;

public class MapCacheTest extends BaseTest {

    public void testMapCacheFieldExpiry() throws InterruptedException {
        // Create a RMapCache instance
        RMapCache<Integer, Student> usersCache = redisson.getMapCache("usersCache");

        // Create student objects
        Student student1 = new Student("Alice", 25, "Atlanta");
        Student student2 = new Student("Jake", 30, "Miami");

        // Put values with individual expiration times
        usersCache.put(1, student1, 5, TimeUnit.SECONDS); // expires in 5 seconds
        usersCache.put(2, student2, 10, TimeUnit.SECONDS); // expires in 10 seconds

3. Accessing Map Values Before Expiration

You can retrieve values just like in RMap:

        // Sleep for 3 seconds before accessing
        Thread.sleep(3000);

        // Access both entries
        System.out.println("Student 1: " + usersCache.get(1)); // Should be present
        System.out.println("Student 2: " + usersCache.get(2)); // Should be present

At this point:

  • Student 1 (TTL 5s) → still present
  • Student 2 (TTL 10s) → still present

4. Accessing Map Values After Partial Expiration

After waiting for additional seconds:

        // Sleep 3 more seconds
        Thread.sleep(3000);

        // Access again
        System.out.println("Student 1: " + usersCache.get(1)); // Should be null (expired)
        System.out.println("Student 2: " + usersCache.get(2)); // Should still be present
    }
}

Now:

  • Student 1 → expired and automatically removed
  • Student 2 → still in the map

Behavior: Redisson emits an empty signal when accessing expired entries, so get returns null.


5. Key Points and Best Practices

  1. Per-field expiration: RMapCache is the only Redisson map variant that allows this.
  2. Automatic eviction: You do not need to manually remove expired entries.
  3. TTL management: Each put can have a custom TTL.
  4. Data types: Works with any value type (String, POJO, complex object).
  5. Thread safety: Redisson handles background cleanup threads for expiration.
  6. Use cases:
    • Session storage per field
    • Caching individual user preferences
    • Temporary configuration or state storage

6. Complete Example

RMapCache<Integer, Student> usersCache = redisson.getMapCache("usersCache");

Student student1 = new Student("Alice", 25, "Atlanta");
Student student2 = new Student("Jake", 30, "Miami");

usersCache.put(1, student1, 5, TimeUnit.SECONDS);
usersCache.put(2, student2, 10, TimeUnit.SECONDS);

Thread.sleep(3000);
System.out.println("After 3 seconds:");
System.out.println("Student 1: " + usersCache.get(1)); // present
System.out.println("Student 2: " + usersCache.get(2)); // present

Thread.sleep(3000);
System.out.println("After 6 seconds:");
System.out.println("Student 1: " + usersCache.get(1)); // expired -> null
System.out.println("Student 2: " + usersCache.get(2)); // still present