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: src/content/docs/kv/api/read-key-value-pairs.mdx
+63-66Lines changed: 63 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,6 @@ pcx_content_type: concept
3
3
title: Read key-value pairs
4
4
sidebar:
5
5
order: 4
6
-
7
6
---
8
7
9
8
To get the value for a given key, call the `get()` method of the [KV binding](/kv/concepts/kv-bindings/) on any [KV namespace](/kv/concepts/kv-namespaces/) you have bound to your Worker code:
@@ -14,30 +13,31 @@ env.NAMESPACE.get(key);
14
13
15
14
The `get()` method returns a promise you can `await` on to get the value. If the key is not found, the promise will resolve with the literal value `null`.
16
15
17
-
#### Example
16
+
#### Example
18
17
19
18
An example of reading a key from within a Worker:
20
19
21
20
```js
22
21
exportdefault {
23
-
asyncfetch(request, env, ctx) {
24
-
try {
25
-
constvalue=awaitenv.NAMESPACE.get("first-key");
26
-
27
-
if (value ===null) {
28
-
returnnewResponse("Value not found", {status:404});
29
-
}
30
-
returnnewResponse(value);
31
-
} catch (e) {
32
-
returnnewResponse(e.message, {status:500});
33
-
}
34
-
},
22
+
asyncfetch(request, env, ctx) {
23
+
try {
24
+
constvalue=awaitenv.NAMESPACE.get("first-key");
25
+
26
+
if (value ===null) {
27
+
returnnewResponse("Value not found", {status:404});
28
+
}
29
+
returnnewResponse(value);
30
+
} catch (e) {
31
+
returnnewResponse(e.message, {status:500});
32
+
}
33
+
},
35
34
};
36
35
```
37
36
38
37
## Reference
39
38
40
39
The following methods are provided to read from KV:
40
+
41
41
-[get()](#get-method)
42
42
-[getWithMetadata()](#getwithmetadata-method)
43
43
@@ -53,26 +53,26 @@ The `get()` method returns a promise you can `await` on to get the value. If the
53
53
54
54
#### Parameters
55
55
56
-
*`key`: `string`
57
-
* The key of the KV pair.
58
-
*`type`: `"text"|"json"|"arrayBuffer"|"stream"`
59
-
* Optional. The type of the value to be returned. `text` is the default.
60
-
*`options`: `{
56
+
-`key`: `string`
57
+
- The key of the KV pair.
58
+
-`type`: `"text"|"json"|"arrayBuffer"|"stream"`
59
+
- Optional. The type of the value to be returned. `text` is the default.
60
+
-`options`: `{
61
61
cacheTtl: number,
62
62
type:"text"|"json"|"arrayBuffer"|"stream"
63
63
}`
64
-
* Optional. Object containing the `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
64
+
- Optional. Object containing the `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
70
+
-`text`: A `string` (default).
71
+
-`json`: An object decoded from a JSON string.
72
+
-`arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
73
+
-`stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
74
74
75
-
The `get()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
75
+
The `get()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
76
76
77
77
### `getWithMetadata()` method
78
78
@@ -82,59 +82,56 @@ To get the value for a given key along with its metadata, call the `getWithMetad
Metadata is a serializable value you append to each KV entry.
86
-
85
+
Metadata is a serializable value you append to each KV entry.
87
86
88
87
#### Parameters
89
88
90
-
*`key`: `string`
91
-
* The key of the KV pair.
92
-
*`type`: `"text"|"json"|"arrayBuffer"|"stream"`
93
-
* Optional. The type of the value to be returned. `string` is the default.
94
-
*`options`: `{
89
+
-`key`: `string`
90
+
- The key of the KV pair.
91
+
-`type`: `"text"|"json"|"arrayBuffer"|"stream"`
92
+
- Optional. The type of the value to be returned. `text` is the default.
93
+
-`options`: `{
95
94
cacheTtl: number,
96
95
type:"text"|"json"|"arrayBuffer"|"stream"
97
96
}`
98
-
* Optional. Object containing the `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
97
+
- Optional. Object containing the `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
* An object containing the value and the metadata for the requested KV pair. The type of the value attribute will depend on the `type` parameter provided for the `getWithMetadata()` command as follows:
108
-
*`text`: A `string` (default).
109
-
*`json`: An object decoded from a JSON string.
110
-
*`arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
111
-
*`stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
106
+
- An object containing the value and the metadata for the requested KV pair. The type of the value attribute will depend on the `type` parameter provided for the `getWithMetadata()` command as follows:
107
+
-`text`: A `string` (default).
108
+
-`json`: An object decoded from a JSON string.
109
+
-`arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
110
+
-`stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
112
111
113
112
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
114
113
115
-
The `getWithMetadata()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
116
-
114
+
The `getWithMetadata()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
117
115
118
-
#### Example
116
+
#### Example
119
117
120
118
An example of reading a key with metadata from within a Worker:
returnnewResponse("Value not found", {status:404});
130
-
}
131
-
returnnewResponse(value);
132
-
}
133
-
catch (e)
134
-
{
135
-
returnnewResponse(e.message, {status:500});
136
-
}
137
-
},
122
+
asyncfetch(request, env, ctx) {
123
+
try {
124
+
const { value, metadata } =
125
+
awaitenv.NAMESPACE.getWithMetadata("first-key");
126
+
127
+
if (value ===null) {
128
+
returnnewResponse("Value not found", { status:404 });
129
+
}
130
+
returnnewResponse(value);
131
+
} catch (e) {
132
+
returnnewResponse(e.message, { status:500 });
133
+
}
134
+
},
138
135
};
139
136
```
140
137
@@ -148,22 +145,22 @@ For large values, the choice of `type` can have a noticeable effect on latency a
148
145
149
146
### CacheTtl parameter
150
147
151
-
`cacheTtl` is a parameter that defines the length of time in seconds that a KV result is cached in the global network location it is accessed from.
148
+
`cacheTtl` is a parameter that defines the length of time in seconds that a KV result is cached in the global network location it is accessed from.
152
149
153
-
Defining the length of time in seconds is useful for reducing cold read latency on keys that are read relatively infrequently. `cacheTtl` is useful if your data is write-once or write-rarely.
150
+
Defining the length of time in seconds is useful for reducing cold read latency on keys that are read relatively infrequently. `cacheTtl` is useful if your data is write-once or write-rarely.
154
151
155
152
:::note[Hot and cold read]
156
153
157
-
A hot read means that the data is cached on Cloudflare's edge network using the [CDN](/cache/). A cold read means that the data is not cached, therefore you have to fetch the data from the storage provider. Both existing key-value pairs and non-existent key-value pairs (also known as negative lookups) are cached at the edge.
158
-
154
+
A hot read means that the data is cached on Cloudflare's edge network using the [CDN](https://developers.cloudflare.com/cache/), whether it is in a local cache or a regional cache. A cold read means that the data is not cached, so the data must be fetched from the central stores. Both existing key-value pairs and non-existent key-value pairs (also known as negative lookups) are cached at the edge.
159
155
:::
160
156
161
157
`cacheTtl` is not recommended if your data is updated often and you need to see updates shortly after they are written, because writes that happen from other global network locations will not be visible until the cached value expires.
162
158
163
-
The `cacheTtl` parameter must be an integer greater than or equal to `60`, which is the default.
159
+
The `cacheTtl` parameter must be an integer greater than or equal to `60`, which is the default.
164
160
165
161
The effective `cacheTtl` of an already cached item can be reduced by getting it again with a lower `cacheTtl`. For example, if you did `NAMESPACE.get(key, {cacheTtl:86400})` but later realized that caching for 24 hours was too long, you could `NAMESPACE.get(key, {cacheTtl:300})` or even `NAMESPACE.get(key)` and it would check for newer data to respect the provided `cacheTtl`, which defaults to 60 seconds.
166
162
167
163
## Other methods to access KV
168
164
169
165
You can [read key-value pairs from the command line with Wrangler](/kv/reference/kv-commands/#get) and [from the API](/api/operations/workers-kv-namespace-read-key-value-pair).
Copy file name to clipboardExpand all lines: src/content/docs/kv/concepts/how-kv-works.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ Initial reads from a location do not have a cached value. Data must be read from
21
21
22
22
:::note[Hot and cold read]
23
23
24
-
A hot read means that the data is cached on Cloudflare's edge network using the [CDN](https://developers.cloudflare.com/cache/). A cold read means that the data is not cached, therefore you have to fetch the data from the storage provider.
24
+
A hot read means that the data is cached on Cloudflare's edge network using the [CDN](https://developers.cloudflare.com/cache/), whether it is in a local cache or a regional cache. A cold read means that the data is not cached, so the data must be fetched from the central stores.
25
25
:::
26
26
27
27

@@ -55,7 +55,7 @@ Visibility of changes takes longer in locations which have recently read a previ
55
55
:::note
56
56
57
57
KV is not ideal for applications where you need support for atomic operations or where values must be read and written in a single transaction.
58
-
If you need stronger consistency guarantees, consider using [Durable Objects](/durable-objects/).
58
+
If you need stronger consistency guarantees, consider using [Durable Objects](/durable-objects/).
59
59
:::
60
60
61
61
An approach to achieve write-after-write consistency is to send all of your writes for a given KV key through a corresponding instance of a Durable Object, and then read that value from KV in other Workers. This is useful if you need more control over writes, but are satisfied with KV's read characteristics described above.
0 commit comments