Skip to content
Lesson 20 of 29

Step 1 of 3 · Reading · ~2 min

Key Management

Advanced Features

Cross-cutting key management

Up to now every command has been scoped to one data type. DEL, EXISTS, KEYS, TYPE, and RENAME are different: they operate on the keyspace itself, regardless of what kind of value lives behind each key. This is where the payoff of having kept strings, lists, hashes, sets, and zsets in separate dicts (or, alternatively, one unified dict of tagged values) becomes obvious — these commands need to reason across all of them at once.

A unified view of the keyspace

If you've been storing each type in its own dict, write one helper that checks all of them together:

python

Every command in this lesson is now a thin wrapper around key_type plus a lookup in the right store.

DEL and EXISTS: counting, not booleans

Both take a variable number of keys and count occurrences, not just presence:

python

The subtlety in EXISTS: if the same key is passed twice (EXISTS foo foo) and it exists, the answer is 2, not 1 — it's a per-argument count, not a set-membership check. Iterate the raw argument list, don't dedupe it first.

KEYS *

Real Redis's KEYS supports glob patterns; this exercise only requires * (match everything), so it's a straightforward union of every store's key names:

python

Encode as a RESP array, one bulk string per key name. Order doesn't matter for correctness (real Redis doesn't guarantee it either), but if your test harness sorts before comparing, that's the escape hatch — you don't need to sort yourself.

TYPE

Reply is a RESP simple string (+, not $) naming the type, or +none\r\n for a key that doesn't exist anywhere:

python

RENAME

Move the value from one key to another within its own store, and error if the source is missing — this is one case where a keyspace command DOES need to know which store the key lives in, so it can move it inside that same dict:

python

If dst already exists (possibly as a different type), real Redis silently overwrites it — match that behavior rather than erroring, and remember to remove dst from whichever other store it might currently live in, so a key never ends up registered under two types at once.

Up nextMULTI & EXEC — TransactionsAdvanced Features

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…