You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -108,7 +108,7 @@ _2024-12-18_
108
108
# Version 0.0.4
109
109
_2024-11-07_
110
110
111
-
-Cache control support (see [the documentation](https://apollographql.github.io/apollo-kotlin-normalized-cache-incubating/cache-control.html) for details)
111
+
-Expiration support (see [the documentation](https://apollographql.github.io/apollo-kotlin-normalized-cache-incubating/expiration.html) for details)
112
112
- Compatibility with the IntelliJ plugin cache viewer (#42)
113
113
- For consistency, `MemoryCacheFactory` and `MemoryCache` are now in the `com.apollographql.cache.normalized.memory` package
-[Cache control](https://apollographql.github.io/apollo-kotlin-normalized-cache/cache-control.html) (a.k.a. Time to live or expiration)
22
+
-[Expiration](https://apollographql.github.io/apollo-kotlin-normalized-cache/expiration.html) (a.k.a. Time to live)
23
23
-[Garbage collection](https://apollographql.github.io/apollo-kotlin-normalized-cache/garbage-collection.html), and [trimming](https://apollographql.github.io/apollo-kotlin-normalized-cache/trimming.html)
Copy file name to clipboardExpand all lines: Writerside/topics/expiration.md
+13-31Lines changed: 13 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,18 @@
1
-
# Cache control
1
+
# Expiration
2
2
3
-
The cache control feature takes the freshness of fields into consideration when accessing the cache. This is also sometimes referred to as TTL (Time To Live) or expiration.
3
+
The cache can be configured to store expiration information using a max-age. This is also sometimes referred to as TTL (Time To Live) or freshness.
4
4
5
-
Freshness can be configured by the server, by the client, or both.
5
+
Max-age can be configured by the server, by the client, or both.
6
6
7
-
## Server-controlled
7
+
## Server-controlled max-age
8
8
9
-
When receiving a response from the server, the [`Cache-Control` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) can be used to determine the **expiration date** of the fields in the response.
9
+
When receiving a response from the server, the [`Cache-Control` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) can be used to determine the **max age** of the fields in the response.
10
10
11
11
> Apollo Server can be configured to include the `Cache-Control` header in responses. See the [caching documentation](https://www.apollographql.com/docs/apollo-server/performance/caching/) for more information.
12
12
13
+
> The [`Expires` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Expires) is not supported. Only `Cache-Control` is.
14
+
{style="note"}
15
+
13
16
The cache can be configured to store the **expiration date** of the received fields in the corresponding records. To do so, call [`.storeExpirationDate(true)`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/store-expiration-date.html?query=fun%20%3CT%3E%20MutableExecutionOptions%3CT%3E.storeExpirationDate(storeExpirationDate:%20Boolean):%20T), and set your client's cache resolver to [
@@ -24,11 +27,11 @@ val apolloClient = ApolloClient.builder()
24
27
.build()
25
28
```
26
29
27
-
**Expiration dates**will be stored and when a field is resolved, the cache resolver will check if the field is stale. If so, it will throw a `CacheMissException`.
30
+
**Expiration dates**are stored and when a field is resolved, the cache resolver will check if the field is stale. If so, it will return an error..
28
31
29
-
## Client-controlled
32
+
## Client-controlled max-age
30
33
31
-
When storing fields, the cache can also store their **received date**. This date can then be compared to the current date when resolving a field to determine if its age is above its **maximum age**.
34
+
When storing fields, the cache can also store their **received date**. This date can then be compared to the current date when resolving a field to determine if its age is above its **max age**.
32
35
33
36
To store the **received date** of fields, call [`.storeReceivedDate(true)`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/store-receive-date.html?query=fun%20%3CT%3E%20MutableExecutionOptions%3CT%3E.storeReceivedDate(storeReceivedDate:%20Boolean):%20T), and set your client's cache resolver to [
@@ -46,7 +49,7 @@ val apolloClient = ApolloClient.builder()
46
49
47
50
> Expiration dates and received dates can be both stored to combine server-controlled and client-controlled expiration strategies.
48
51
49
-
The **maximum age** of fields can be configured either programmatically, or declaratively in the schema. This is done by passing a [`MaxAgeProvider`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized.api/-max-age-provider/index.html?query=interface%20MaxAgeProvider) to the `CacheControlCacheResolver`.
52
+
The **max age** of fields can be configured either programmatically, or declaratively in the schema. This is done by passing a [`MaxAgeProvider`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized.api/-max-age-provider/index.html?query=interface%20MaxAgeProvider) to the `CacheControlCacheResolver`.
50
53
51
54
### Global max age
52
55
@@ -102,7 +105,7 @@ extend type Book @cacheControlField(name: "cachedTitle", maxAge: 30)
102
105
extend type Reader @cacheControlField(name: "book", inheritMaxAge: true)
103
106
```
104
107
105
-
This will generate a map in `yourpackage.cache.Cache.maxAges`, that you can pass to the `SchemaCoordinatesMaxAgeProvider`:
108
+
This generates a map in `yourpackage.cache.Cache.maxAges`, that you can pass to the `SchemaCoordinatesMaxAgeProvider`:
If stale fields are acceptable up to a certain value, you can set a maximum staleness duration. This duration is the maximum time that a stale field will be resolved without resulting in a cache miss. To set this duration, call [`.maxStale(Duration)`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/max-stale.html?query=fun%20%3CT%3E%20MutableExecutionOptions%3CT%3E.maxStale(maxStale:%20Duration):%20T) either globally on your client, or per operation:
119
-
120
-
```kotlin
121
-
val response = client.query(MyQuery())
122
-
.fetchPolicy(FetchPolicy.CacheOnly)
123
-
.maxStale(1.hours)
124
-
.execute()
125
-
```
126
-
127
-
### `isStale`
128
-
129
-
With `maxStale`, it is possible to get data from the cache even if it is stale. To know if the response contains stale fields, you can check [`CacheInfo.isStale`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/-cache-info/is-stale.html):
Copy file name to clipboardExpand all lines: Writerside/topics/garbage-collection.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ The garbage collection feature allows to remove unused data from the cache to re
7
7
A field is considered stale if its **received date** is older than its (client controlled) max age, or if its (server controlled)
8
8
**expiration date** has passed.
9
9
10
-
See [](cache-control.md) for more information about staleness.
10
+
See [](expiration.md) for more information about staleness.
11
11
12
12
Stale fields can be removed from the cache by calling the [`ApolloStore.removeStaleFields()`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/remove-stale-fields.html) function.
Copy file name to clipboardExpand all lines: Writerside/topics/migration-guide.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,7 +196,7 @@ interface CacheResolver {
196
196
197
197
`resolveField` can also now return a `ResolvedValue` when metadata should be returned with the resolved value (e.g. staleness).
198
198
199
-
If you wish to use the [Cache Control](cache-control.md) feature, a [`CacheControlCacheResolver`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized.api/-cache-control-cache-resolver/index.html) should be used.
199
+
If you wish to use the [Expiration](expiration.md) feature, a [`CacheControlCacheResolver`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized.api/-cache-control-cache-resolver/index.html) should be used.
200
200
You can call the generated `cache()` extension on `ApolloClient.Builder` which will use it by default if max ages are configured in the schema.
When you execute a query, options control how the query is executed. `ApolloClient` returns an `ApolloResponse` that satisfies the options or an exceptional response otherwise (`response.exception` is not null).
4
+
5
+
## `fetchPolicy`
6
+
7
+
Fetch policy controls how and if the cache is used. The default is `CacheFirst`.
8
+
9
+
### `CacheFirst`
10
+
11
+
A response is fetched from the cache first. If no valid response cannot be found, the network is queried.
12
+
13
+
### `CacheOnly`
14
+
15
+
The response is fetched from the cache. If no valid response cannot be found, `response.exception` is set.
16
+
17
+
### `NetworkOnly`
18
+
19
+
The response is fetched from the network. If no valid response cannot be found, `response.exception` is set.
20
+
21
+
### `NetworkFirst`
22
+
23
+
A response is fetched from the network first. If no valid response cannot be found, the cache is queried.
24
+
25
+
## `serverErrorsAsCacheMisses`
26
+
27
+
Sets whether GraphQL errors in the cache should be treated as cache misses. When true (the default), if any field is an Error in the cache, the returned response will have a null data and a non-null exception of type `ApolloGraphQLException`.
28
+
29
+
## `throwOnCacheMiss`
30
+
31
+
Sets whether missing fields from the cache should result in an exception. When true (the default), if any field is missing in the cache, the returned response will have a null data and a non-null exception of type `CacheMissException`.
32
+
33
+
Set this to false to allow partial responses from the cache, where _some_ or _all_ of the fields may be missing.
34
+
35
+
## `maxStale`
36
+
37
+
If stale fields are acceptable up to a certain value, you can set a maximum staleness duration. This duration is the maximum time that a stale field will be resolved without resulting in a cache miss. To set this duration, call [`.maxStale(Duration)`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/max-stale.html?query=fun%20%3CT%3E%20MutableExecutionOptions%3CT%3E.maxStale(maxStale:%20Duration):%20T) either globally on your client, or per operation:
38
+
39
+
```kotlin
40
+
val response = client.query(MyQuery())
41
+
.fetchPolicy(FetchPolicy.CacheOnly)
42
+
.maxStale(1.hours)
43
+
.execute()
44
+
```
45
+
46
+
### `isStale`
47
+
48
+
With `maxStale`, it is possible to get data from the cache even if it is stale. To know if the response contains stale fields, you can check [`CacheInfo.isStale`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/-cache-info/is-stale.html):
0 commit comments