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: Writerside/topics/expiration.md
+12-30Lines changed: 12 additions & 30 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`.
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.
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
+
## `allowCachedErrors`
26
+
27
+
Sets whether to allow GraphQL errors to be returned from the cache. If set to false, if any field is an Error in the cache, the returned response has a null data and a non-null exception of type ApolloGraphQLException.
28
+
29
+
## `allowCachedPartialResults`
30
+
31
+
Sets whether to allow partial results to be returned from the cache. If set to false, if any field is missing in the cache, the returned response has a null data and a non-null exception of type CacheMissException.
32
+
33
+
## `maxStale`
34
+
35
+
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:
36
+
37
+
```kotlin
38
+
val response = client.query(MyQuery())
39
+
.fetchPolicy(FetchPolicy.CacheOnly)
40
+
.maxStale(1.hours)
41
+
.execute()
42
+
```
43
+
44
+
### `isStale`
45
+
46
+
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