0Pricing
Edge Computing with Cloudflare Workers & Deno · 강의

Cloudflare KV 저장소

엣지에서 키-값 저장소로 사용할 수 있는 Cloudflare Workers KV를 활용합니다. 캐싱과 구성 관리에 적합합니다.

Cloudflare KV 저장소은(는) CoddyKit의 무료 Edge Computing with Cloudflare Workers & Deno 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Edge Computing with Cloudflare Workers & Deno 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Edge Data with KV Store

Welcome to Cloudflare KV! It's a highly distributed key-value store that runs on Cloudflare's global network, right at the edge.

This means your data is stored incredibly close to your users, leading to extremely fast access and low latency. It's perfect for dynamic content, configuration, and caching.

How Key-Value Works

A key-value store is like a simple dictionary or map. You store data as unique key-value pairs. The key is a unique identifier (a string), and the value is the data associated with that key.

  • Keys: Think of them as unique names, like "user_id_123" or "website_config".
  • Values: These are the actual data, which can be strings, numbers, JSON objects (as strings), or even binary data.

KV's Edge Advantages

Why use KV at the edge?

  • Speed: Data is served from the nearest Cloudflare data center, reducing latency.
  • Simplicity: It's schemaless, making it easy to store and retrieve data without complex database schemas.
  • Scalability: Automatically scales to handle massive traffic and global distribution.
  • Cost-Effective: Often more economical for specific use cases compared to traditional databases.

Practical KV Applications

Cloudflare KV is versatile for many edge-specific tasks:

  • Caching: Storing frequently accessed API responses, HTML snippets, or static assets.
  • Configuration: Managing dynamic feature flags, A/B test settings, or redirect rules.
  • Personalization: Storing simple user preferences like theme choices or language settings.
  • Rate Limiting: Keeping track of request counts per user or IP address.

Creating a KV Namespace

Before using KV, you need to create a Namespace. A namespace is a logical container for your key-value pairs, keeping your data organized.

You can create a new KV Namespace through the Cloudflare dashboard or using the Wrangler CLI (Cloudflare's command-line tool).

Binding KV to Your Worker

To use a KV Namespace in your Worker, you must bind it in your wrangler.toml configuration file. This makes the KV namespace accessible as a global variable in your Worker script.

Here's how you define a binding:

name = "my-worker"
main = "src/index.js"
compatibility_date = "2024-01-01"

[[kv_namespaces]]
binding = "MY_KV_STORE" # The variable name in your Worker
id = "YOUR_NAMESPACE_ID" # Get this from Cloudflare dashboard

Storing Data with `put`

The put() method is used to store data in your KV Namespace. It takes a key and a value as arguments. Values can be strings, ArrayBuffers, or ReadableStreams.

Try running this example Worker:

export default {
  async fetch(request, env, ctx) {
    const key = "user_setting";
    const value = "dark_mode";
    
    // Store the value in the MY_KV_STORE namespace
    await env.MY_KV_STORE.put(key, value);
    
    return new Response(`Stored '${value}' for key '${key}'`);
  },
};

Retrieving Data with `get`

To retrieve a value, you use the get() method, providing the key. If the key doesn't exist, get() will return null.

This example tries to retrieve the setting we just stored:

export default {
  async fetch(request, env, ctx) {
    const key = "user_setting";
    
    // Retrieve the value from the MY_KV_STORE namespace
    const value = await env.MY_KV_STORE.get(key);
    
    if (value === null) {
      return new Response(`Key '${key}' not found.`, { status: 404 });
    }
    
    return new Response(`Retrieved: '${value}'`);
  },
};

Removing Data with `delete`

If you need to remove a key-value pair from your namespace, use the delete() method with the key you wish to remove.

Here's how to delete the stored setting:

export default {
  async fetch(request, env, ctx) {
    const key = "user_setting";
    
    // Delete the key-value pair from the MY_KV_STORE namespace
    await env.MY_KV_STORE.delete(key);
    
    return new Response(`Key '${key}' deleted.`);
  },
};

Listing Keys in KV

Cloudflare KV also provides a list() method to retrieve a list of keys within a namespace. This can be useful for administrative tasks or for understanding the contents of your KV store.

It often returns an object containing an array of keys and a list_complete boolean.

// Example of listing keys (inside a Worker's fetch handler)
const { keys } = await env.MY_KV_STORE.list();

// To get just the names:
const keyNames = keys.map(k => k.name);
console.log(keyNames);

KV Operations Check

Which of the following statements about Cloudflare KV are true?

Recap: Cloudflare KV Store

In this lesson, you've learned about Cloudflare KV, a powerful key-value store optimized for the edge. We covered:

  • What KV is and its advantages for low-latency data access.
  • Common use cases like caching and configuration.
  • How to create a KV Namespace and bind it to a Cloudflare Worker.
  • The core operations: put() to store, get() to retrieve, and delete() to remove data.

KV is a fundamental tool for building fast, scalable, and efficient edge applications!

자주 묻는 질문

“Cloudflare KV 저장소” 강의는 무료인가요?

네 — “Cloudflare KV 저장소” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Edge Computing with Cloudflare Workers & Deno 강의 전체를 잠금 해제할 수 있습니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.

“Cloudflare KV 저장소”에서 뭘 배우나요?

엣지에서 키-값 저장소로 사용할 수 있는 Cloudflare Workers KV를 활용합니다. 캐싱과 구성 관리에 적합합니다. 브라우저에서 직접 실행하는 실습 코드로 Edge Computing with Cloudflare Workers & Deno을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Edge Computing with Cloudflare Workers & Deno을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Edge Computing with Cloudflare Workers & Deno은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“Cloudflare KV 저장소” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Edge Computing with Cloudflare Workers & Deno 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Edge Computing with Cloudflare Workers & Deno 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Cloudflare KV 저장소
  2. Durable Objects 이해하기
  3. Deno와 엣지 저장소 통합
  4. 엣지에서 D1 SQL로 질의하기
← Edge Computing with Cloudflare Workers & Deno(으)로 돌아가기