0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · レッスン

キャッシュの追い出しと有効期限

キャッシュサイズの管理、有効期限(TTL)の設定、キャッシュの無効化に関する技法を身につけます。

「キャッシュの追い出しと有効期限」はCoddyKit上の無料Redis Caching & Messaging (Pub/Sub, Streams)レッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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:1

SETEX: 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:5

Checking 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:token

Millisecond 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:data

Automatic 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:1

Quick 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, and PEXPIRE to set time-based expirations.
  • Understanding TTL and PERSIST to manage key lifetimes.
  • Learning about automatic eviction policies to handle memory limits.
  • Implementing manual cache invalidation with DEL for immediate updates.

These skills are crucial for maintaining fresh data and efficient memory usage in your Redis cache!

よくある質問

「キャッシュの追い出しと有効期限」レッスンは無料ですか?

はい。「キャッシュの追い出しと有効期限」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Redis Caching & Messaging (Pub/Sub, Streams)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Redis Caching & Messaging (Pub/Sub, Streams)コースには全4レッスンが含まれています。

「キャッシュの追い出しと有効期限」で何を学びますか?

キャッシュサイズの管理、有効期限(TTL)の設定、キャッシュの無効化に関する技法を身につけます。 ブラウザで直接実行するハンズオンコードでRedis Caching & Messaging (Pub/Sub, Streams)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Redis Caching & Messaging (Pub/Sub, Streams)を始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのRedis Caching & Messaging (Pub/Sub, Streams)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「キャッシュの追い出しと有効期限」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このRedis Caching & Messaging (Pub/Sub, Streams)レッスンでコードを書いて実行できますか?

はい。すべてのRedis Caching & Messaging (Pub/Sub, Streams)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. なぜキャッシュするのか?キャッシュ入門
  2. 基本的なキャッシュパターンの実装
  3. キャッシュの追い出しと有効期限
  4. キャッシュスタンピードとThundering Herdを防ぐ
← Redis Caching & Messaging (Pub/Sub, Streams)に戻る