# Add Caching to a Quarkus App

Yes. An agent adds quarkus-cache to your Panache service, caches the list query, invalidates it on every write, proves it with @QuarkusTest and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-caching/quarkus/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own cloud sandbox and reads how data reaches the endpoints before it caches anything. In a service like the `hibernate-orm-panache-quickstart` in quarkusio/quarkus-quickstarts, `pom.xml` pins `quarkus.platform.version` 3.40.1.1, builds for Java 17 and depends on `quarkus-hibernate-orm-panache`, `quarkus-rest-jackson` and `quarkus-jdbc-postgresql`. Two resources serve the same data two ways: `FruitEntityResource` at `entity/fruits` calls `FruitEntity.listAll(Sort.by("name"))`, and `FruitRepositoryResource` at `repository/fruits` calls `fruitRepository.listAll(Sort.by("name"))`. Both entities carry `@Cacheable`, which turns on Hibernate's second-level cache for lookups by id, so `findById` is already served from memory while the sorted list runs a query on every `GET`. `application.properties` sets `quarkus.hibernate-orm.log.sql=true`, which is what lets anyone see that query repeat in the log.

It then reads what the repository already knows about caching. The sibling `cache-quickstart` depends on `quarkus-cache` and marks `WeatherForecastService.getDailyForecast` with `@CacheResult(cacheName = "weather-cache")`, so the agent follows that shape rather than inventing one. It adds `quarkus-cache` to the Panache module's `pom.xml`, moves the two list calls into a small `@ApplicationScoped` service with `@CacheResult(cacheName = "fruits")`, and annotates `create`, `update` and `delete` in both resources with `@CacheInvalidateAll(cacheName = "fruits")`, because a `PUT` that renames Apple through `findById` would otherwise leave the old name in the cached list. An `expire-after-write` for the `fruits` cache goes into `application.properties` as a safety net, with a comment saying the invalidation is what keeps the list correct and the expiry only bounds the damage of a write that misses it.

The existing test is the first check. `FruitsEndpointTest` lists `/entity/fruits` and `/repository/fruits`, expects Cherry, Apple and Banana from `import.sql`, deletes `/1` and expects the list without Cherry, on each path in turn. That test already fails if the delete does not invalidate, so the agent keeps it unchanged and adds to it: a `POST` followed by a `GET` that must contain the new fruit, a `PUT` followed by a `GET` that must show the new name, and a test that injects `CacheManager`, reads the `fruits` cache as a `CaffeineCache` and asserts its key set is empty after a write and non-empty after a read. `FruitsEndpointIT` extends the test class, so the native profile runs the same checks.

Finally it runs `./mvnw clean install` in that module. Dev Services starts the Postgres container the tests need, as the quickstart README describes, and with `log.sql` on the agent reads the surefire output to confirm the `select` for the list appears once between writes instead of once per request. The repository's own pull request workflow rejects wildcard imports with a `git grep` step, so the agent writes explicit imports in the new service and test code. It opens a pull request with the dependency, the service, the annotations, the property and the tests.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add application-level caching to this Quarkus service. Read pom.xml, application.properties, import.sql, every resource and entity class and the existing tests first. Add the quarkus-cache extension, cache the sorted list query behind @CacheResult in a dedicated service, and invalidate that cache with @CacheInvalidateAll on every create, update and delete, keeping the by-id lookups on the Hibernate second-level cache the entities already enable. Add an expire-after-write for the new cache in application.properties. Extend the existing endpoint tests so a POST, PUT and DELETE are each followed by a GET that proves the list changed, and add a test that reads the cache through CacheManager. Run ./mvnw clean install, use explicit imports, and open a pull request that shows the SQL log before and after.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch. The diff adds the `quarkus-cache` dependency to `pom.xml`, a `FruitService` with the `@CacheResult` method, `@CacheInvalidateAll` on the six write methods across `FruitEntityResource` and `FruitRepositoryResource`, one `quarkus.cache.caffeine` line in `application.properties` and the new tests beside `FruitsEndpointTest`. The description names the cache, lists which methods invalidate it and quotes the two SQL log excerpts.

The run's record keeps the full `./mvnw clean install` output: the Dev Services container start, each test name with its result and the Hibernate SQL lines, so a reviewer can see the repeated `select` disappear without running anything. Nothing is pushed to any repository other than the reader's.

Before merging, a reviewer checks that every write path is annotated, including any resource added since, that the cache key covers every argument the list method takes, and that the expiry is short enough for the data. If the service runs more than one instance, the reviewer decides whether a per-instance Caffeine cache is acceptable or whether the same annotations should back onto a shared store, which the pull request notes.

## Run it on every new read-heavy endpoint

In the console, an automation with a `github` trigger on `pull_request.opened` starts one run per opened pull request, in the Project that has the repository, with the pull request appended after the prompt. The prompt asks the agent to do nothing when the pull request adds no resource class or Panache query, and otherwise to say whether the new endpoint should be cached and, when it should, to push the annotations and tests to the branch.

From your own server, the same work is one `POST /o/{org}/sessions` per service, with the repository in `resources` and a `request_id` so a retry cannot start a second run. A webhook on the API key posts `run.completed` or `run.failed` when the turn finishes, with the `session_url` to read the pull request link and the Maven output.

## Why run it on Rush?

A cache is only correct when every write invalidates it, and the only convincing evidence is a test log in which the query stops repeating and the list still changes after each write. The sandbox has the JDK, Maven and Docker for Dev Services, so the agent runs the whole Quarkus test suite against a real Postgres and keeps the SQL log, instead of a reviewer reading annotations and hoping.

Caching touches the dependency, a service, two resources, the configuration and the tests at once. One run returns one pull request with all of it and the proof, and the same prompt runs against several Quarkus services in parallel, each in its own sandbox, each reading its own entities and tests.

## Frequently asked questions

### Will it cache the by-id lookups too?

No. Both entity classes already carry `@Cacheable`, so Hibernate's second-level cache serves `findById`. The agent leaves that alone and caches only the sorted list, which is the query that runs on every `GET`.

### What happens when two instances of the service run?

The default `quarkus-cache` backend is Caffeine, in the memory of each instance, so an invalidation on one instance does not reach the other. The expire-after-write bounds how long the other instance serves the old list. For a shared cache the same annotations can back onto the Redis cache extension, which the pull request mentions as the next step.

### Does it change the existing test?

No. `FruitsEndpointTest` stays as written, and its delete-then-list check is kept as the first proof of invalidation. The new cases are added next to it and `FruitsEndpointIT` inherits them, so the native build runs them too.

### Can it cache per argument?

Yes. `@CacheResult` keys the entry on the method arguments, so a list method that takes a sort or a page gets one entry per distinct call. The agent keeps the method's arguments to what the endpoint actually varies, because every extra argument multiplies the entries.

## More Quarkus jobs

- [Add CORS to a Quarkus REST API](https://getrush.ai/run/add-cors/quarkus/)
- [Add Test Coverage Reporting to Quarkus CI](https://getrush.ai/run/add-coverage-to-ci/quarkus/)
- [Add CSV Export to a Quarkus REST API](https://getrush.ai/run/add-csv-export/quarkus/)

## The same job on other stacks

- [Add Caching to a Streamlit App](https://getrush.ai/run/add-caching/streamlit/)
- [Add Caching to a Vapor App](https://getrush.ai/run/add-caching/vapor/)
- [Add Caching to a Flask App](https://getrush.ai/run/add-caching/flask/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
