Last update:2024-08-14 20:00:49
Edge KV is a distributed key-value storage service that allows you to store data on CDNetworks’ edge nodes close to your users, enabling low-latency, high-availability, and strongly consistent data access. You can use Edge KV to store various types of data, such as:
Creates an Edge KV instance object.
new EdgeKV({ namespace: "your-namespace" });
| Parameter | Type | Default Value | Description |
|---|---|---|---|
namespace |
string |
- | The namespace of the storage bucket. Multiple functions or applications can share one namespace to achieve data sharing. It cannot be empty;otherwise, the default value will be used. |
get()Reads the value of the specified key from Edge KV.
get(key, { type: "text" })
| Parameter | Type | Description |
|---|---|---|
key |
string |
The name of the key to read. It cannot be empty and has a maximum length of 512 bytes. |
type |
string |
"text": Returns a string (default) "json": Returns an object parsed from a JSON string "arrayBuffer": Returns an ArrayBuffer, suitable for binary data "stream": Returns a ReadableStream, suitable for reading large files |
Returns a Promise that you should await to ensure the operation is completed.
null is returned.get operation to fail, an error exception is rejected.addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const edgeKV = new EdgeKV({ namespace: "your-namespace" });
let value = await edgeKV.get("key");
if (value === null) {
return new Response("EdgeKV get: key not found", { status: 404 });
} else {
return new Response(value);
}
} catch (e) {
return new Response("EdgeKV get error" + e.toString(), { status: 500 });
}
}
put()Creates a new key-value pair, or updates the value of an existing key.
put(key, value, { expiration: secondsSinceEpoch })
| Parameter | Type | Description |
|---|---|---|
key |
string |
The name of the key to create or update. It cannot be empty and has a maximum length of 512 bytes. |
value |
string \| ReadableStream \| ArrayBuffer |
The value of the key. The maximum length is 10MB. |
expiration |
int |
The expiration timestamp, default is permanent, in seconds (representing absolute time). |
Returns a Promise that you should await to ensure the operation is completed:
true is resolved.put operation to fail, an error exception is rejected.addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const edgeKV = new EdgeKV({ namespace: "your-namespace" });
await edgeKV.put("key", "value");
return new Response("EdgeKV put success");
} catch (e) {
return new Response("EdgeKV put error" + e.toString(), { status: 500 });
}
}
delete()Deletes the specified key and its corresponding value.
delete(key)
| Parameter | Type | Description |
|---|---|---|
key |
string |
The name of the key to be deleted. It cannot be empty and has a maximum length of 512 bytes. |
Returns a Promise that you should await to ensure the operation is completed:
true is resolved.delete operation to fail, an error exception is rejected.addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
try {
const edgeKV = new EdgeKV({ namespace: "your-namespace" });
await edgeKV.delete("key");
return new Response("EdgeKV delete success");
} catch (e) {
return new Response("EdgeKV delete error" + e.toString(), { status: 500 });
}
}
list()Enumerates keys within the namespace that meet the specified criteria.
list({ startKey: "key1", endKey: "key2", limit: 10 })
| Parameter | Type | Description |
|---|---|---|
startKey |
string |
The starting key, which is the minimum lexicographical order of the string. The operation will include keys equal to this value. |
endKey |
string |
The ending key, which is up to the last element in lexicographical order. The operation will not include keys equal to this value. |
limit |
int |
The maximum number of keys to return. This is an optional return condition. If this parameter is not specified, all keys that meet the criteria will be returned. |
Returns a Promise that you should await to ensure the operation is completed:
list operation is successful, an array of keys is resolved.list operation to fail, an error exception is rejected.addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const edgeKV = new EdgeKV({ namespace: "your-namespace" });
const keys = await edgeKV.list({ startKey: "key1", endKey: "key2", limit: 10 });
for (let k of keys) {
console.log(k);
}
return new Response("EdgeKV List success");
} catch (e) {
return new Response("EdgeKV List error" + e.toString(), { status: 500 });
}
}
count()Counts the number of keys within the namespace that meet the specified criteria.
count({ startKey: "key1", endKey: "key2" })
| Parameter | Type | Description |
|---|---|---|
startKey |
string |
The starting key, which is the minimum lexicographical order of the string. The operation will include keys equal to this value. |
endKey |
string |
The ending key, which is up to the last element in lexicographical order. The operation will not include keys equal to this value. |
Returns a Promise that you should await to ensure the operation is completed:
count operation is successful, an integer representing the number of keys is resolved.count operation to fail, an error exception is rejected.addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const edgeKV = new EdgeKV({ namespace: "your-namespace" });
const keyNum = await edgeKV.count({ startKey: "key1", endKey: "key2" });
return new Response(`EdgeKV Count success: ${keyNum}`);
} catch (e) {
return new Response("EdgeKV Count error" + e.toString(), { status: 500 });
}
}