캐시 제거 및 만료
캐시 크기 관리, 만료 시간(TTL) 설정, 캐시 무효화 처리를 위한 기법을 익힙니다.
캐시 제거 및 만료은(는) CoddyKit의 무료 Redis Caching & Messaging (Pub/Sub, Streams) 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Redis Caching & Messaging (Pub/Sub, Streams) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Cache Expiration Matters
Caching is great for speed, but cached data can become stale, meaning it no longer reflects the true source of information. Also, caches have limited memory.
This lesson explores how to manage these issues using expiration and eviction techniques in Redis.
Introducing Time To Live (TTL)
Time To Live (TTL) defines how long a cached item remains valid. Once its TTL expires, Redis automatically removes the key.
- Prevents stale data from being served indefinitely.
- Frees up memory for newer, more relevant data.
Setting TTL with EXPIRE
The EXPIRE command sets an expiration time, in seconds, on an existing key. After the set time, the key will be automatically deleted.
Try setting a key and then giving it a 10-second expiration:
SET myapp:user:1 "John Doe"
EXPIRE myapp:user:1 10
TTL myapp:user:1SETEX: Set and Expire Together
Instead of two commands (SET then EXPIRE), you can use SETEX to set a key's value and its expiration time atomically (in one go).
This is generally preferred for new keys as it's more efficient.
SETEX myapp:product:5 20 "LaptopX"
TTL myapp:product:5Checking TTL & Persistence
You can check a key's remaining TTL with the TTL command. It returns the remaining seconds, or -2 if the key doesn't exist, -1 if it exists but has no expiration.
The PERSIST command removes the expiration from a key, making it permanent again.
SET temp:token "abc123"
EXPIRE temp:token 5
TTL temp:token
PERSIST temp:token
TTL temp:tokenMillisecond Precision with PEXPIRE
For more precise control, PEXPIRE and PSETEX work just like their second-based counterparts, but accept expiration times in milliseconds.
This can be useful for very short-lived caches or specific timing requirements.
SET mskey:data "important info"
PEXPIRE mskey:data 5000
PTTL mskey:dataAutomatic Eviction Policies
What happens when your Redis cache runs out of memory? Redis can be configured to automatically evict (remove) keys to free up space.
This behavior is controlled by the maxmemory setting and an eviction policy.
Common Eviction Policies
Redis offers several eviction policies configured via maxmemory-policy:
noeviction: Returns errors on write operations when memory limit is reached.allkeys-lru: Evicts least recently used (LRU) keys from all keys.volatile-lru: Evicts LRU keys *only* from those with an explicit TTL set.allkeys-random: Evicts random keys from all keys.
Manual Cache Invalidation
Sometimes, data changes unexpectedly, or you need to ensure a cache entry is immediately removed. In such cases, you can manually invalidate a key.
The DEL command removes one or more specified keys from Redis immediately.
SET cache:user:1 "user_data_old"
GET cache:user:1
DEL cache:user:1
GET cache:user:1Quick Check: Expiration
You have a Redis key named session:user:123. You want it to expire in exactly 30 minutes from now. Which command would achieve this?
Recap: Eviction & Expiration
In this lesson, you mastered key techniques for managing cached data in Redis:
- Using
EXPIRE,SETEX, andPEXPIREto set time-based expirations. - Understanding
TTLandPERSISTto manage key lifetimes. - Learning about automatic eviction policies to handle memory limits.
- Implementing manual cache invalidation with
DELfor immediate updates.
These skills are crucial for maintaining fresh data and efficient memory usage in your Redis cache!
자주 묻는 질문
“캐시 제거 및 만료” 강의는 무료인가요?
네 — “캐시 제거 및 만료” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Redis Caching & Messaging (Pub/Sub, Streams) 강의 전체를 잠금 해제할 수 있습니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
“캐시 제거 및 만료”에서 뭘 배우나요?
캐시 크기 관리, 만료 시간(TTL) 설정, 캐시 무효화 처리를 위한 기법을 익힙니다. 브라우저에서 직접 실행하는 실습 코드로 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Redis Caching & Messaging (Pub/Sub, Streams)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Redis Caching & Messaging (Pub/Sub, Streams)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“캐시 제거 및 만료” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Redis Caching & Messaging (Pub/Sub, Streams) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.