คลังข้อมูล Cloudflare KV
ใช้ Cloudflare Workers KV สำหรับจัดเก็บข้อมูลแบบคีย์-ค่าใกล้ผู้ใช้ เหมาะสำหรับแคชและการกำหนดค่า
คลังข้อมูล Cloudflare KV เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 dashboardStoring 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, anddelete()to remove data.
KV is a fundamental tool for building fast, scalable, and efficient edge applications!
คำถามที่พบบ่อย
บทเรียน “คลังข้อมูล Cloudflare KV” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “คลังข้อมูล Cloudflare KV” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “คลังข้อมูล Cloudflare KV”
ใช้ Cloudflare Workers KV สำหรับจัดเก็บข้อมูลแบบคีย์-ค่าใกล้ผู้ใช้ เหมาะสำหรับแคชและการกำหนดค่า คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “คลังข้อมูล Cloudflare KV” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม
ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คลังข้อมูล Cloudflare KV
- อธิบาย Durable Objects
- การผสาน Deno กับพื้นที่จัดเก็บใกล้ผู้ใช้
- สืบค้นด้วย D1 SQL ที่เอดจ์