Agent Wranglers

APP-27Required

Key cached data by every input

Build every cache key from all the inputs that change the answer, including the account, the signed-in user, the filters, the sort and the page.

How to do it

Put the account or tenant first, so one call can drop all of its data. Then add the resource and each parameter. Pass the same values to the request and to the key from one object, so they can't drift apart. Clear the whole cache on sign-out and on switching accounts. The same rule holds for server-side caches, as caches and copies explains. HTTP caches need the same, as say which request headers change a response explains.

Examples

TypeScript example

export const orderKeys = {
  all: (accountId: string) => ['accounts', accountId, 'orders'] as const,
  list: (accountId: string, filters: OrderFilters) => [...orderKeys.all(accountId), 'list', filters] as const,
};

export function useOrders(accountId: string, filters: OrderFilters) {
  return useQuery({ queryKey: orderKeys.list(accountId, filters), queryFn: () => api.listOrders(accountId, filters) });
}

export function onAccountSwitch(queryClient: QueryClient) {
  queryClient.clear();
}

Python example

def orders_cache_key(account_id: str, filters: OrderFilters) -> str:
    return ":".join(["orders", account_id, filters.status or "any", filters.sort, filters.cursor or "first"])

How to check

A test signs in to account A, loads orders, switches to account B and loads orders. No request answer or rendered row from account A appears.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/app-ui/client-and-server-state/complete-cache-keys">Agent Wranglers web app interface playbook</a>. In code you write, add a comment next to it with the same link.