
# Caches

With this starter every cache key carries who the cached call was made as: `anon`, `user:<id>` or `service`. Nothing is written in `@Cacheable` for it.

## Why

With row-level security one call answers differently per user: `findByListId('l1')` gives each user only their rows. A cache key made of the arguments alone would hand one user's rows to the next caller, or an answer read as the service role to a user. In a Flutter app, a new login would read the previous user's entries.

## Example

```dart
@Service()
class Reports {
  Reports(this.functions);

  final Functions functions;

  @Cacheable('reports')                              // one entry per caller and year
  Future<String> byYear(int year) => functions.report(year);

  @Cacheable('reports', key: "'explicit:' + #year")  // kept apart per caller too
  Future<String> byYearWithKey(int year) => functions.report(year);

  @Cacheable('catalog')                              // the same for everyone, see below
  Future<String> shared(int year) => functions.report(year);
}
```

```yaml
spring:
  supabase:
    cache:
      shared: [catalog]
```

Two users who call `byYear(2026)` get an entry each. So do the anon key and the service role.

## How

The starter declares a `CachingConfigurer` (the bean `supabaseCachingConfigurer`) with two defaults:

| Bean | What it does |
|---|---|
| `supabaseKeyGenerator` (`SupabaseKeyGenerator`, a `KeyGenerator`) | The key of a call without `key:` is a `SupabaseCacheKey`: who calls ([`SupabaseCredentials.caller`](credentials.md)), plus what `SimpleKeyGenerator` makes of the arguments. |
| `supabaseCacheResolver` (`SupabaseCacheResolver`, a `CacheResolver`) | An explicit `key:` goes past any key generator. So the caches this resolver gives add the caller to such a key, and leave a key that already has it as it is: no key is scoped twice. |

- `evict` and `put` use the same keys, so `@CacheEvict` removes the entry of the caller.
- `clear` (`@CacheEvict(allEntries: true)`) empties a cache for every caller.
- A cache that does not exist is a `StateError` that names it.

## Shared caches

A cache of data that is the same for everyone is named in `spring.supabase.cache.shared`. No key of it carries a caller, so one entry serves all.

Share a cache only when the cached call answers the same for every caller: a public table, a function without row-level security.

## A CachingConfigurer of your own

The starter's configurer steps back for a `CachingConfigurer` bean of the application. The two beans stay; name them where they are wanted:

```dart
@Cacheable('todos', keyGenerator: 'supabaseKeyGenerator')
Future<List<Todo>> open(String listId) => todos.findByListId(listId);
```

A key generator of your own can call `SupabaseKeyGenerator`, or read `credentials.caller.key` itself.

## What is not cached

Streams are not cacheable, as in the cache starter: a `Stream` download, the streams of [Realtime](realtime.md). A [signed URL](storage.md) stops working when it expires: cache it for less than its `expiresIn`. The starter does not check that.

## The properties

| Key | Type | Default | Meaning |
|---|---|---|---|
| `spring.supabase.cache.shared` | `List<String>` | empty | The caches whose entries are the same for every user. |

## In Spring (Java)

`KeyGenerator`, `CacheResolver` and `CachingConfigurer` are Spring's. Spring has no default that puts the user into the key: an application writes a `KeyGenerator` for it.

## See also

- [spring_boot_starter_cache](../spring-boot-starter-cache/index.md)
- [Cache annotations](../spring-boot-starter-cache/annotations.md)
- [Who a call is made as](credentials.md)
