JCache Integration

Learn how to trace cache operations using the Sentry JCache (JSR-107) integration.

Sentry's JCache (JSR-107) integration automatically creates spans for cache operations like get, put, remove, and clear. It works with any JCache provider (Caffeine, Ehcache, Hazelcast, etc.).

Cache spans appear in Sentry's Caches dashboard.

Copied
implementation 'io.sentry:sentry-jcache:8.36.0'

For other dependency managers, check out the central Maven repository.

Enable cache tracing in your Sentry configuration:

Copied
Sentry.init(options -> {
    options.setDsn("___DSN___");
    options.setTracesSampleRate(1.0);
    options.setEnableCacheTracing(true);
});

Wrap your javax.cache.Cache instance with SentryJCacheWrapper:

Copied
import io.sentry.jcache.SentryJCacheWrapper;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;

CachingProvider provider = Caching.getCachingProvider();
CacheManager manager = provider.getCacheManager();

Cache<String, String> cache = manager.getCache("myCache");
Cache<String, String> sentryCache = new SentryJCacheWrapper<>(cache);

// Use sentryCache — all operations produce Sentry spans
sentryCache.put("key", "value");       // cache.put span
String val = sentryCache.get("key");   // cache.get span (cache.hit = true)
sentryCache.remove("key");             // cache.remove span
sentryCache.clear();                   // cache.clear span

All JCache operations are traced. Each method creates a span with the operation cache.<methodName> (e.g. cache.get, cache.putIfAbsent, cache.removeAll).

KeyTypeDescription
cache.hitbooleanWhether the cache lookup returned a value (read spans only)
cache.keylist of stringsThe cache key(s) involved in the operation
cache.operationstringThe JCache method name (e.g. get, putIfAbsent, removeAll)
cache.writebooleanWhether the operation modified the cache. Always true for unconditional writes (put, putAll, remove, clear); reflects the actual outcome for conditional operations (putIfAbsent, replace, getAndReplace) and value-matched removes

Bulk operations (getAll, putAll, removeAll) create a single span with all keys listed in cache.key.

To verify, trigger a cache operation within an active transaction and check the Caches dashboard or the trace view in Sentry.

Copied
import io.sentry.ISentryLifecycleToken;
import io.sentry.ITransaction;
import io.sentry.Sentry;

ITransaction tx = Sentry.startTransaction("test-cache", "task");
try (ISentryLifecycleToken ignored = tx.makeCurrent()) {
    sentryCache.put("greeting", "hello");
    String value = sentryCache.get("greeting");
} finally {
    tx.finish();
}
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").