0Pricing
Caching Strategies: Redis + CDN + Edge Computing · บทเรียน

การดำเนินการแคชพื้นฐานในรีดิส

เรียนรู้คำสั่งพื้นฐานของรีดิสสำหรับกำหนดค่า เรียกดู อัปเดต และลบรายการแคช รวมถึงการจัดการอายุการใช้งาน (TTL)

การดำเนินการแคชพื้นฐานในรีดิส เป็นบทเรียน Caching Strategies: Redis + CDN + Edge Computing ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Caching Strategies: Redis + CDN + Edge Computing และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro to Cache Operations

Welcome! In this lesson, we'll dive into the fundamental operations for managing data in your Redis cache. Think of it as learning the basic "read, write, update, and delete" for your cached information.

Mastering these commands is key to effectively using Redis to speed up your applications and reduce database load.

Storing Data: The SET Command

The most basic operation is storing data. In Redis, you use the SET command to store a key-value pair.

  • Key: A unique identifier for your data (e.g., user:123:name).
  • Value: The actual data you want to store (e.g., "Alice").

It's like putting a label on a box and storing something inside.

SET myapp:homepage "Welcome to our site!"

This command stores "Welcome to our site!" under the key myapp:homepage.

Retrieving Data: The GET Command

Once data is stored, you'll need to retrieve it. The GET command fetches the value associated with a specific key.

If the key exists, Redis returns its value. If the key doesn't exist or has expired, Redis returns nil (nothing).

GET myapp:homepage

This would return "Welcome to our site!" if the key exists.

Data Lifespan: Time To Live (TTL)

Cached data shouldn't live forever! Time To Live (TTL) defines how long an item should remain in the cache before it's automatically removed. This prevents stale data and saves memory.

  • TTL is typically measured in seconds.
  • Once the TTL expires, the key is automatically deleted by Redis.

It's like setting an alarm for your cached data.

Setting Expiration with SETEX

You can set a TTL right when you store the data using SETEX or PSETEX.

  • SETEX key seconds value: Sets an expiration in whole seconds.
  • PSETEX key milliseconds value: Sets an expiration in milliseconds (for finer control).

This is very common for caching temporary session tokens or frequently changing data.

SETEX user:123:session 300 "active"

This key will expire in 5 minutes (300 seconds).

EXPIRE, PERSIST & TTL Commands

You can also set or change the TTL for an existing key, or check how much time is left.

  • EXPIRE key seconds: Sets a TTL for an existing key.
  • PERSIST key: Removes the expiration from a key, making it permanent.
  • TTL key: Returns the remaining TTL in seconds (or -1 if no TTL, -2 if key doesn't exist).
EXPIRE user:123:session 60
TTL user:123:session

The first command updates the TTL to 60 seconds. The second checks it.

Updating Data: Overwriting with SET

To "update" a cache entry, you simply use the SET command again with the same key. Redis will overwrite the old value with the new one.

If the key had a TTL, it will be removed unless you explicitly set a new one with SETEX or EXPIRE.

SET user:123:name "Bob"
SET user:123:name "Robert"

After the second command, GET user:123:name would return "Robert".

Deleting Data: The DEL Command

When data is no longer needed or becomes completely invalid, you can remove it from the cache using the DEL command.

  • DEL key: Deletes a single key.
  • DEL key1 key2 key3: Deletes multiple keys at once.

This frees up memory and ensures your cache only holds relevant data.

DEL myapp:homepage
DEL user:123:session user:456:session

These commands remove the specified keys from Redis.

Practical Flow: Caching a Page

Let's simulate a simple caching flow for a web page using these commands:

SET page:about "HTML content for about page" EX 3600
GET page:about
# ... time passes ...
DEL page:about # If content changes, invalidate it!

Here, we set the page content with a 1-hour TTL, then retrieve it. If the page is updated, we explicitly delete the old cache entry.

Quick Check on Cache Commands

Consider the following sequence of Redis commands:

SET mykey "valueA"
EXPIRE mykey 5
SET mykey "valueB"
TTL mykey

What will be the output of the final TTL mykey command?

Recap & Next Steps

Great job! You've learned the essential Redis commands for managing your cache:

  • SET: To store key-value pairs.
  • GET: To retrieve values.
  • SETEX/PSETEX: To set a value with an expiration.
  • EXPIRE/PEXPIRE/PERSIST: To manage existing key expirations.
  • DEL: To remove keys.

These commands are the building blocks for any Redis caching strategy. Next, we'll explore more advanced data structures Redis offers for caching!

คำถามที่พบบ่อย

บทเรียน “การดำเนินการแคชพื้นฐานในรีดิส” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การดำเนินการแคชพื้นฐานในรีดิส” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Caching Strategies: Redis + CDN + Edge Computing ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การดำเนินการแคชพื้นฐานในรีดิส”

เรียนรู้คำสั่งพื้นฐานของรีดิสสำหรับกำหนดค่า เรียกดู อัปเดต และลบรายการแคช รวมถึงการจัดการอายุการใช้งาน (TTL) คุณปฏิบัติ Caching Strategies: Redis + CDN + Edge Computing ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Caching Strategies: Redis + CDN + Edge Computing หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Caching Strategies: Redis + CDN + Edge Computing บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การดำเนินการแคชพื้นฐานในรีดิส” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Caching Strategies: Redis + CDN + Edge Computing นี้ได้ไหม

ได้ บทเรียน Caching Strategies: Redis + CDN + Edge Computing ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บทนำสู่การแคชด้วยรีดิส
  2. โครงสร้างข้อมูลของรีดิสสำหรับแคช
  3. การดำเนินการแคชพื้นฐานในรีดิส
  4. TTL และการหมดอายุใน Redis
← กลับไปที่ Caching Strategies: Redis + CDN + Edge Computing