StaffCoder
Redis
RedisJava 25Node.js 24Python 3.13
1 module · 4 challengesSign up
Module 1 · Strings & Key Basics

Key Naming & Scanning

easy100%1 of 1 solved

Problem statement

Session keys named without a shared scheme cannot be found one user at a time, and listing the whole keyspace in one call stops the server for everyone else. A namespaced key scheme and a cursor walk let a worker find one user's keys in small batches.

Requirements

The connected Redis client and size() are already provided. Your task has six methods in src/keyspace.py. A colon-separated namespace lets the cursor-based SCAN command filter one user's keys on the server, a small batch at a time.

  • key(user_id, token) returns session:tok:<user_id>:<token>.
  • scan_match(pattern, count) returns every key that matches pattern exactly once, as str, even when the server returns the same key in more than one batch.
  • scan_match(pattern, count) walks the cursor until it returns to 0, sends pattern as MATCH and count as COUNT on every call, and never uses KEYS.
  • rename(source, target) moves a value and its lifetime to the target in one command, replacing an existing target.
  • rename_if_free(source, target) moves a value in one command only when the target is free, and returns whether it moved.
  • expire(key, seconds) gives a key a lifetime and returns whether the key existed.
  • drop_big(*keys) deletes the keys with the non-blocking delete command and returns how many keys were removed.
Sample Usage

key("u1", "abc")

session:tok:u1:abc

scan_match("session:tok:u1:*", 10) with keys session:tok:u1:a, session:tok:u1:b and session:tok:u2:c stored (keys come back in any order)

['session:tok:u1:b', 'session:tok:u1:a']

rename_if_free("session:tok:u1:a", "session:tok:u1:b") when the target exists

False

expire("session:tok:u1:missing", 60)

False

Note: Refer to the README.md file for detailed implementation requirements.

More in Strings & Key Basics

See the module
First KeysMove a session store off a per-process map and into Redis. Store session values under a defined key, read them back exactly as written including blobs that are not text, report whether a key exists and what type it holds, and delete keys — returning the number Redis actually removed.40%2 of 5 solvedeasyConditional Sets & CountersDecide a claim in one command instead of two. Write a key only when it is absent, only when it already exists, or while returning the value it replaced — each with a single SET — and keep per-user counters with INCR, INCRBY, DECR and INCRBYFLOAT, letting Redis's own error surface when a counter holds something that is not a number.50%1 of 2 solvedeasySession Token StoreBuild the session store the whole service signs people in with: issue a token exactly once with an expiry, refresh it while it is in use, revoke it on sign-out, count logins per user, and audit a user's open sessions on a live keyspace — using a cursor, never KEYS, and freeing keys without making other clients wait.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.