Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this article, we’ll learn how to get all cached keys in the Caffeine Cache when used with Spring’s Cache abstraction layer.

2. Spring Caching

Caching is an integral part of Spring Framework. It’s been a part of Spring’s ecosystem since the 3.1 version. Therefore, it has a set of well-defined and battle-tested interfaces.

Let’s have a look at the two main ones: CacheManager and Cache:

interface CacheManager {
    Cache getCache(String name);
    Collection<String> getCacheNames();
}

public interface Cache {
    String getName();
    Object getNativeCache();
    ValueWrapper get(Object key);
    <T> T get(Object key, @Nullable Class<T> type);
    <T> T get(Object key, Callable<T> valueLoader);
    void put(Object key, @Nullable Object value);
    ValueWrapper putIfAbsent(Object key, @Nullable Object value);
    void evict(Object key);
    void clear();
}

As we can see, CacheManager is simply a wrapper. A registry of cache regions that are available in the application. A Cache object, on the other hand, is a set of key-value pairs within the region.

None of them, though, provides a way to list available keys.

3. Setup

Before we explore our options for accessing the collection of all available keys, let’s define the CaffeineCacheManager used by our test application:

@Configuration
@EnableCaching
public class AllKeysConfig {

    @Bean
    CacheManager cacheManager() {
        return new CaffeineCacheManager();
    }
}

Then, let’s create a slow service that would populate the cache upon each call:

public class SlowServiceWithCache {

    @CachePut(cacheNames = "slowServiceCache", key = "#name")
    public String save(String name, String details) {
        return details;
    }
}

With both manager and service in place, we are ready to look for the keys in the slowServiceCache region.

4. Accessing All Cache Keys

As we’ve learned already, CacheManager does not expose any method to access all available keys. Neither does the Cache interface.

Therefore we need to use the knowledge of the actual cache implementation we’ve defined in our application. Let’s cast Spring’s generic interfaces to their appropriate Caffeine implementations.

We need to inject the CacheManager first:

@Autowired
CacheManager cacheManager;

Then let’s do a few simple cast operations to access the native Caffeine Cache:

CaffeineCacheManager caffeineCacheManager = (CaffeineCacheManager) cacheManager;
CaffeineCache cache = (CaffeineCache) caffeineCacheManager.getCache("slowServiceCache");
Cache<Object, Object> caffeine = cache.getNativeCache();

Then, let’s call caffeine.asMap(). Because it’s a map, we can access the keys simply by calling caffeine.asMap().keySet():

@Test
public void givenCaffeineCacheCachingSlowCalls_whenCacheManagerProperlyCasted_thenAllKeysAreAccessible() {
    slowServiceWithCache.save("first", "some-value-first");
    slowServiceWithCache.save("second", "other-value-second");

    Cache<Object, Object> caffeine = getNativeCaffeineCacheForSlowService();

    assertThat(caffeine.asMap().keySet()).containsOnly("first", "second");
}

4. Conclusion

In this article, we’ve learned how to access the collection of all available keys from the Caffeine cache used with Spring Cache. Knowing the actual cache we’re dealing with, accessing all available keys requires only a few simple cast operations.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW