All jobs

Add Response Caching to a Fiber App

Yes. An agent puts Fiber's cache middleware on the GET routes of your Fiber v3 app, clears it when a write lands, tests the hit and miss headers, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the app and the middleware it will use. In an app laid out like gofiber/boilerplate, go.mod declares go 1.26.0 with github.com/gofiber/fiber/v3 v3.6.1 and gofiber/utils/v2. app.go parses -port and -prod, the latter switching on prefork through fiber.ListenConfig{EnablePrefork: *prod}, installs recover and logger, mounts GET and POST /api/v1/users on handlers.UserList and handlers.UserCreate, serves ./static/public with the static middleware on /*, and ends with handlers.NotFound. database/database.go is a slice of *models.User with a sync.Mutex that Insert takes and Get does not. static/public/js/app.js fetches the list on load and, after a successful POST, inserts the new row itself. .github/workflows/test.yml runs go test ./... -v -race on Go 1.26.x and 1.27.x on Ubuntu and Windows runners, and lint.yml runs golangci-lint v2.14.0 with --tests=false. There are no test files.

Fiber ships the cache in github.com/gofiber/fiber/v3/middleware/cache. The agent reads its config.go for v3.6.1: by default it caches only GET and HEAD under Methods, keeps entries for Expiration of five minutes, holds at most MaxBytes of one mebibyte, partitions keys by scheme, host, path, the sorted query string and the Accept, Accept-Encoding and Accept-Language headers, answers with an X-Cache header of hit, miss or unreachable, and, unless DisableCacheControl is set, adds public, max-age= with the remaining seconds to each hit. Storage defaults to an in-memory store for the one process, and CacheInvalidator is a function consulted when a cached entry is found. The agent mounts it on the one route that benefits, v1.Get("/users", cache.New(cfg), handlers.UserList), so the static files and the POST are never looked up, and takes the expiry from a new -cache flag beside -port and -prod.

The write path is where response caches go stale. The middleware only consults CacheInvalidator on a read, so the agent gives it an explicit Storage from github.com/gofiber/storage/memory/v2, keeps that store in a package variable, and has UserCreate call its Reset() after database.Insert, which drops every entry so the next GET is a miss. It sets DisableCacheControl: true, because app.js re-fetches the list on each page load and a browser honouring max-age would show a list that the server had already invalidated. With -prod, prefork runs several processes and each has its own store, so a POST on one process leaves the others' entries until they expire; the README and the pull request say so and name a shared store from gofiber/storage as the step for a multi-process deployment.

Reading database.Get() under the race detector is the first thing the new tests expose: it returns the slice without the mutex while Insert appends to it, so the agent takes the lock and returns a copy. handlers/cache_test.go builds the app through a small setup() extracted from main, uses app.Test with an in-memory request, and checks that two GET /api/v1/users calls answer miss then hit in X-Cache, that a POST followed by a GET answers miss with the new name in the body, and that GET / for the static page carries no X-Cache header at all. go test ./... -race and golangci-lint run pass before the pull request opens.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add response caching to this Fiber v3 app. Read go.mod, app.go, handlers/handlers.go, database/database.go, models/user.go, static/public/index.html, static/public/js/app.js, Makefile, Dockerfile, .golangci.yml and the workflows under .github first, then read middleware/cache/config.go in the fiber v3.6.1 module. Mount github.com/gofiber/fiber/v3/middleware/cache on GET /api/v1/users only, with an expiry from a new -cache flag, DisableCacheControl set, and an explicit Storage from github.com/gofiber/storage/memory/v2 kept in a package variable; call its Reset in UserCreate after database.Insert so a write invalidates the list. Fix database.Get to read under the mutex and return a copy. Extract app construction from main into a setup function and write handlers/cache_test.go with app.Test covering miss then hit on X-Cache, a miss after a POST with the new user present, and no X-Cache on the static page. Document in the README that prefork gives each process its own store. Run go test ./... -race and golangci-lint run, then open a pull request that quotes the output.",
  "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

The pull request adds handlers/cache_test.go, changes app.go (the flag, the setup() extraction, the middleware on one route), handlers/handlers.go (the Reset() after the insert), database/database.go (the locked copy in Get), go.mod and go.sum (the storage module), and the README (the flag and the prefork note). Its description quotes the go test -race output and the lint run, and shows the X-Cache header on a hit and on the first request after a POST.

A reviewer sends two requests and reads the header, then reads the three decisions the diff makes for them: caching one route rather than the whole group, disabling the Cache-Control the middleware would otherwise add, and resetting the whole store on a write instead of one key. Each is a one-line change if the team wants it otherwise, and the description says which line.

The race in database.Get is fixed in the same change because the tests cannot pass under -race without it, and it is called out separately in the description.

Run it on every new API endpoint

A team that adds read endpoints often starts one run per route from its own server, each POST /o/{org}/sessions naming the repository in resources, the route in input, and a request_id that is a UUID v5 of the route path, so a retried call returns the run already started. Each run reads the handler behind that route and decides whether a write elsewhere must reset the store.

An automation on the Project with a github trigger on pull_request.opened can read each later pull request that adds a v1.Get route and comment on whether it should sit behind the cache and which handler invalidates it; automation runs read and comment, they do not push.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

A cache that is wrong is worse than none, and the only way to know is to send the requests. The agent runs the real server in its own sandbox, drives it with in-memory requests, runs the suite under the race detector and keeps the output with the pull request, so the reviewer sees the miss, hit, miss sequence rather than a description of it.

The configuration is read from the middleware's own source at the pinned version, and the invalidation is written against this app's one write handler. A team with several Fiber services starts the same request on each at once and reads one pull request per service, each naming the routes it cached.

Frequently asked questions

Why cache one route instead of the whole app?

Only GET /api/v1/users returns data that changes. Mounting the middleware on that route keeps the static files, the 404 handler and the POST out of the key space entirely, and the test for GET / confirms it by asserting no X-Cache header.

How does a POST invalidate the cache?

The middleware consults CacheInvalidator only when it finds an entry for a read. The pull request instead hands the middleware a Storage the handlers can reach and calls Reset() after the insert, so the next read misses and rebuilds the entry.

Does it work with prefork?

Each forked process has its own in-memory store, so a write on one process does not clear the others until their entries expire. The README states this and names a shared store from gofiber/storage for deployments that run -prod.

Why disable Cache-Control?

By default a hit is sent with public, max-age= and the remaining lifetime, which would let the browser skip the server after a write the server already knows about. With it disabled the cache is server-side only and app.js keeps fetching fresh lists.

What is X-Cache for?

It is the middleware's status header: miss when the response was produced by the handler and stored, hit when it came from the store, unreachable when the request bypassed the cache. The tests and a reviewer with curl read it to see what happened.

More Fiber jobs

The same job on other stacks

Keep reading