Skip to content

Quarkus Cache

DodaTech 1 min read

In this tutorial, you'll learn about Fix Quarkus Cache Not Caching Results. We cover key concepts, practical examples, and best practices.

The Problem

@CacheResult annotated methods execute every time, ignoring the cache.

Quick Fix

Add cache dependency

Wrong:

<!-- No cache -->
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-cache</artifactId>
</dependency>

Output:

No caching

Right:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-cache</artifactId>
</dependency>

Output:

Caching enabled

Use @CacheResult

Wrong:

@CacheResult(cacheName = "products")
public Product getProduct(Long id) { return expensiveCall(); }

Output:

Cached after first call

Right:

@CacheResult(cacheName = "products")
public Product getProduct(Long id) { return expensiveCall(); }

Output:

Results cached

Use @CacheInvalidate

Wrong:

@CacheInvalidate(cacheName = "products")
public void updateProduct(Product p) { }

Output:

Cache not cleared

Right:

@CacheInvalidate(cacheName = "products")
public void updateProduct(Product p) { }

Output:

Cache entries invalidated

Prevention

  • Add quarkus-cache dependency
  • Use @CacheResult for caching method results
  • Use @CacheInvalidate to evict stale cache entries

Common Mistakes with cache

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world QUARKUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What cache backends does Quarkus support?

Caffeine (default), Redis, Hazelcast, and custom implementations.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro