# Caching

Source: https://help.ruuster.com/idx/caching

> How the API caches answers at the edge, how to cache your own pages with ISR or a CDN, and how to keep serving the last good page when the API asks you to wait.

Caching makes your pages fast, keeps your units low and keeps your site up when the API is busy. There
are two caches: Ruuster's edge cache in front of the API, and your own.

## The edge cache

- **Publishable-key calls** that succeed are cached at the edge for the time each endpoint lists below,
  then served stale for a while longer while a fresh copy is fetched. Answers from the edge cost no
  units. The cache is kept per calling site, so your domain checks still apply.
- **Secret-key calls are never cached at the edge,** and neither is any error. Your server sees the API's
  current answer every time, and caches it itself.
- **Every answer** carries `X-Robots-Tag: noindex`, so the API's own URLs never appear in search
  results. Your pages are what get indexed.

| Method | Path | Key | Units | Cache | Page |
|---|---|---|---|---|---|
| GET | /v1/status | no key | free | cached 30 s | https://help.ruuster.com/idx/status |
| GET | /v1/meta | publishable or secret key | free | cached 3600 s | https://help.ruuster.com/idx/meta |
| GET | /v1/filters | publishable or secret key | 1 unit | cached 3600 s | https://help.ruuster.com/idx/filters |
| GET | /v1/filters/options | publishable or secret key | 1 unit | cached 3600 s | https://help.ruuster.com/idx/filters-options |
| GET | /v1/listings | publishable or secret key | 2 units | cached 120 s | https://help.ruuster.com/idx/listings-search |
| GET | /v1/listings/ids | secret key only | 5 units | never cached | https://help.ruuster.com/idx/listings-ids |
| GET | /v1/listings/{listingId} | publishable or secret key | 1 unit | cached 300 s | https://help.ruuster.com/idx/listings-get |
| GET | /v1/listings/{listingId}/photos | publishable or secret key | 1 unit | cached 900 s | https://help.ruuster.com/idx/listings-photos |
| GET | /v1/listings/{listingId}/similar | publishable or secret key | 2 units | cached 600 s | https://help.ruuster.com/idx/listings-similar |
| GET | /v1/listings/{listingId}/history | publishable or secret key | 1 unit | cached 900 s | https://help.ruuster.com/idx/listings-history |
| GET | /v1/agents/{agentMlsId}/production | publishable or secret key | 2 units | cached 3600 s | https://help.ruuster.com/idx/agents-production |
| GET | /v1/suggest | publishable or secret key | 1 unit | cached 3600 s | https://help.ruuster.com/idx/suggest |
| GET | /v1/boundaries/{boundaryId} | publishable or secret key | 1 unit | cached 3600 s | https://help.ruuster.com/idx/boundaries-get |
| GET | /v1/market | publishable or secret key | 2 units | cached 3600 s | https://help.ruuster.com/idx/market |
| GET | /v1/market/snapshot | publishable or secret key | 2 units | cached 3600 s | https://help.ruuster.com/idx/market-snapshot |
| GET | /v1/market/metrics | publishable or secret key | 1 unit | cached 3600 s | https://help.ruuster.com/idx/market-metrics |
| GET | /v1/shapes | publishable or secret key | 1 unit | cached 300 s | https://help.ruuster.com/idx/shapes-list |
| GET | /v1/shapes/{shapeId} | publishable or secret key | 1 unit | cached 600 s | https://help.ruuster.com/idx/shapes-get |
| GET | /v1/collections | publishable or secret key | 1 unit | cached 300 s | https://help.ruuster.com/idx/collections-list |
| GET | /v1/collections/{collectionId} | publishable or secret key | 1 unit | cached 300 s | https://help.ruuster.com/idx/collections-get |
| GET | /v1/collections/{collectionId}/listings | publishable or secret key | 2 units | cached 300 s | https://help.ruuster.com/idx/collections-listings |
| GET | /v1/render/listings | publishable or secret key | 2 units | cached 120 s | https://help.ruuster.com/idx/render-listings |
| GET | /v1/render/listing/{listingId} | publishable or secret key | 1 unit | cached 300 s | https://help.ruuster.com/idx/render-listing |
| GET | /v1/changes | secret key only | 5 units | never cached | https://help.ruuster.com/idx/changes |
| GET | /v1/beacon | publishable or secret key | free | never cached | https://help.ruuster.com/idx/beacon |
| GET | /v1/events | publishable or secret key | free | never cached | https://help.ruuster.com/idx/events |
| GET | /v1/mortgage/estimate | publishable or secret key | free | cached 3600 s | https://help.ruuster.com/idx/mortgage-estimate |
| GET | /v1/usage | secret key only | free | never cached | https://help.ruuster.com/idx/usage |
| GET | /v1/account/billing | secret key only | free | never cached | https://help.ruuster.com/idx/account-billing |
| GET | /v1/public-records/lookup | secret key only | 1 unit | never cached | https://help.ruuster.com/idx/public-records-lookup |
| GET | /v1/public-records/market | secret key only | 1 unit | never cached | https://help.ruuster.com/idx/public-records-market |
| GET | /v1/public-records/{recordId}/history | secret key only | 1 unit | never cached | https://help.ruuster.com/idx/public-records-history |
| GET | /v1/sandbox/public-records/lookup | secret key only | 1 unit | never cached | https://help.ruuster.com/idx/sandbox-public-records-lookup |
| GET | /v1/sandbox/public-records/{recordId}/history | secret key only | 1 unit | never cached | https://help.ruuster.com/idx/sandbox-public-records-history |

## Your own cache

Server-rendered pages should be cached by your framework or CDN:

| Page | Re-render at most every | And also |
|---|---|---|
| Listing page | 5 minutes | On a webhook or `/v1/changes` row for that listing |
| Area, sold, search page 1 | 5 minutes | On a webhook for any listing on it |
| Market numbers on area pages | 1 hour or longer | |
| Sitemaps | 1 hour | |

In Next.js, `export const revalidate = 300` on a page does this, and the SDK's `next: { revalidate }`
option passes the same value to every fetch. In other frameworks, send
`Cache-Control: s-maxage=300, stale-while-revalidate=600` from your page and let your CDN keep it.

## Keep the last good page

When the API answers 429 or 503, throw from your page's data function instead of rendering an error.
ISR and most CDNs then keep serving the last good copy until the next render succeeds, so a busy minute
never shows visitors an error page. Never cache a page that rendered an error.

## Conditional requests

A listing answer carries an `ETag`. Send it back as `If-None-Match` when you check whether a page needs
re-rendering: a `304 Not Modified` answer costs no units.

## What you may keep

A cache is the only place listing data may live on your side. Keep answers only as long as your cache
time, never copy listings into a database of your own, and re-render or purge a page as soon as its
listing changes or leaves the API ([Keeping pages fresh](https://help.ruuster.com/idx/guide-freshness)).
