0Pricing
Caching Strategies: Redis + CDN + Edge Computing · レッスン

RedisのTTLと有効期限

Redisによるキーの有効期限の扱い方を学びます。TTLの設定、EXPIREおよびSETEXコマンド、遅延期限切れとアクティブ期限切れの仕組み、maxmemoryのエビクションポリシーとTTLの関係を扱います。

「RedisのTTLと有効期限」はCoddyKit上の無料Caching Strategies: Redis + CDN + Edge Computingレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCaching Strategies: Redis + CDN + Edge Computing学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Caching Strategies: Redis + CDN + Edge Computingコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Expire Cache Keys?

Cached data goes stale. Time To Live (TTL) lets Redis automatically remove a key after a set duration, so your cache stays reasonably fresh without manual cleanup.

Setting a TTL with EXPIRE

The EXPIRE command sets a timeout in seconds on an existing key. After that time, Redis deletes the key automatically.

SET user:1 "Alice"
EXPIRE user:1 60
TTL user:1

SET with EX in One Step

You can set a value and its TTL together. SET key value EX seconds (or the older SETEX) avoids a race between writing and expiring.

SET session:abc "data" EX 300

Checking Remaining TTL

The TTL command returns seconds left. PTTL returns milliseconds. Special return values matter:

  • -1: key exists but has no expiry
  • -2: key does not exist
def interpret_ttl(value):
    if value == -1:
        return 'no expiry set'
    if value == -2:
        return 'key does not exist'
    return str(value) + 's remaining'

print(interpret_ttl(-2))
print(interpret_ttl(45))

Removing an Expiry

The PERSIST command removes the TTL from a key, making it permanent again. Useful when a value should stop expiring after some condition is met.

PERSIST user:1

Lazy Expiration

Redis does not check every key constantly. With lazy expiry, an expired key is only removed when someone tries to access it. Until then it lingers in memory.

Active Expiration

To reclaim memory from keys nobody touches, Redis also runs a background sampler that periodically removes a batch of expired keys. This is active expiry. Together with lazy expiry it keeps memory in check.

TTL vs Eviction

TTL is voluntary removal at a set time. Eviction happens when Redis hits its maxmemory limit and must drop keys to make room. They are different mechanisms that often work together.

volatile vs allkeys Policies

Eviction policies decide what to drop under memory pressure:

  • volatile-lru: evict least-recently-used keys that have a TTL
  • allkeys-lru: evict LRU among all keys

For a pure cache, allkeys policies are common.

Choosing TTL Values

Pick TTLs from the data's tolerance for staleness: seconds for fast-changing prices, hours for catalogs, days for rarely-changing config. Add jitter to avoid synchronized mass expiry.

import random
base = 3600
print('TTL:', base + random.randint(0, 300), 'seconds')

Common Pitfall: No TTL

Forgetting to set a TTL turns a cache into an ever-growing store that eventually exhausts memory. For cache use cases, almost every key should have an expiry or rely on an allkeys eviction policy.

Quick Check

What does the Redis command TTL mykey return if the key exists but has no expiration set?

Recap

You learned TTL and expiration in Redis:

  • EXPIRE / SET ... EX set timeouts; TTL/PTTL inspect them; PERSIST removes them.
  • Redis uses lazy plus active expiration.
  • Eviction policies (volatile vs allkeys) handle memory pressure.
  • Always set TTLs (or an allkeys policy) for cache keys.

Good expiration keeps a cache both fresh and bounded.

よくある質問

「RedisのTTLと有効期限」レッスンは無料ですか?

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

「RedisのTTLと有効期限」で何を学びますか?

Redisによるキーの有効期限の扱い方を学びます。TTLの設定、EXPIREおよびSETEXコマンド、遅延期限切れとアクティブ期限切れの仕組み、maxmemoryのエビクションポリシーとTTLの関係を扱います。 ブラウザで直接実行するハンズオンコードでCaching Strategies: Redis + CDN + Edge Computingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Caching Strategies: Redis + CDN + Edge Computingを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのCaching Strategies: Redis + CDN + Edge Computingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「RedisのTTLと有効期限」レッスンにはどのくらい時間がかかりますか?

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

このCaching Strategies: Redis + CDN + Edge Computingレッスンでコードを書いて実行できますか?

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

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

  1. Redisキャッシュ入門
  2. キャッシュにおけるRedisデータ構造
  3. Redisキャッシュの基本操作
  4. RedisのTTLと有効期限
← Caching Strategies: Redis + CDN + Edge Computingに戻る