기본 Redis 캐시 작업
캐시 항목을 설정하고 조회하고 업데이트하고 삭제하는 기본 Redis 명령어와 TTL 관리 방법을 학습합니다.
기본 Redis 캐시 작업은(는) CoddyKit의 무료 Caching Strategies: Redis + CDN + Edge Computing 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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:homepageThis 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:sessionThe 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:sessionThese 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 mykeyWhat 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!
자주 묻는 질문
“기본 Redis 캐시 작업” 강의는 무료인가요?
네 — “기본 Redis 캐시 작업” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Caching Strategies: Redis + CDN + Edge Computing 강의 전체를 잠금 해제할 수 있습니다. Caching Strategies: Redis + CDN + Edge Computing 강의에는 총 4개의 강의가 포함되어 있습니다.
“기본 Redis 캐시 작업”에서 뭘 배우나요?
캐시 항목을 설정하고 조회하고 업데이트하고 삭제하는 기본 Redis 명령어와 TTL 관리 방법을 학습합니다. 브라우저에서 직접 실행하는 실습 코드로 Caching Strategies: Redis + CDN + Edge Computing을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Caching Strategies: Redis + CDN + Edge Computing을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Caching Strategies: Redis + CDN + Edge Computing은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“기본 Redis 캐시 작업” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Caching Strategies: Redis + CDN + Edge Computing 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Caching Strategies: Redis + CDN + Edge Computing 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Redis 캐싱 소개
- 캐시를 위한 Redis 데이터 구조
- 기본 Redis 캐시 작업
- Redis의 TTL 및 만료