EdgeKV

最終更新日: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:

  • User session data
  • Application configuration
  • Cached data
  • Other data that requires fast access

Constructor

Creates an Edge KV instance object.

Definition

new EdgeKV({ namespace: "your-namespace" });

Parameter Description

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.

Definition

get(key, { type: "text" })

Parameter Description

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

Return Value

Returns a Promise that you should await to ensure the operation is completed.

  • If the key does not exist, null is returned.
  • If an exception occurs that causes the get operation to fail, an error exception is rejected.

Code Example

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.

Definition

put(key, value, { expiration: secondsSinceEpoch })

Parameter Description

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).

Return Value

Returns a Promise that you should await to ensure the operation is completed:

  • If the operation is successful, true is resolved.
  • If an exception occurs that causes the put operation to fail, an error exception is rejected.

Code Example

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.

Definition

delete(key)

Parameter Description

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.

Return Value

Returns a Promise that you should await to ensure the operation is completed:

  • If the delete operation is successful, true is resolved.
  • If an exception occurs that causes the delete operation to fail, an error exception is rejected.

Code Example

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.

Definition

list({ startKey: "key1", endKey: "key2", limit: 10 })

Parameter Description

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.

Return Value

Returns a Promise that you should await to ensure the operation is completed:

  • If the list operation is successful, an array of keys is resolved.
  • If an exception occurs that causes the list operation to fail, an error exception is rejected.

Code Example

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.

Definition

count({ startKey: "key1", endKey: "key2" })

Parameter Description

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.

Return Value

Returns a Promise that you should await to ensure the operation is completed:

  • If the count operation is successful, an integer representing the number of keys is resolved.
  • If an exception occurs that causes the count operation to fail, an error exception is rejected.

Code Example

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 });
  }
}