Indexing and Querying

Redis Cache Java supports automatic indexing of cached data, enabling powerful search capabilities.

Enabling Indexing

Use RedisCacheConfiguration.indexEnabled(true) to create an index on the cache automatically.

RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .indexEnabled(true);

The default index name is <cacheName>Idx and can be overridden with RedisCacheConfiguration.indexName(String).

RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .indexEnabled(true)
    .indexName("myCustomIndex");

Getting Cache Count

When indexing is enabled you can get a count of entries in the cache using RedisCache.getCount().

RedisCache cache = (RedisCache) cacheManager.getCache("myCache");
long count = cache.getCount();

Use Cases

  • Cache Statistics: Track the number of cached entries

  • Search Capabilities: Query cached data using Redis Search

  • Cache Management: Monitor and manage cache size