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

Session Token Store

easy100%1 of 1 solved

Problem statement

Every web server signs people in through one Redis-backed session store, so it has to issue each token once, keep it alive only while it is used, and count logins per user. Support also lists and clears one user's sessions on a database holding millions of keys, without stopping it to find out.

Requirements

The connected Redis client, key(user_id, token) and logins_key(user_id) are already provided. Your task has eight methods in src/session_store.py. A session blob is raw bytes and may hold any byte value, and two requests can arrive at the same moment.

  • issue(user_id, token, blob, ttl_seconds) stores blob byte for byte under key(user_id, token) and returns True.
  • issue(user_id, token, blob, ttl_seconds) returns False and leaves the stored session untouched when the token already exists, deciding this in one command.
  • A session stored by issue(user_id, token, blob, ttl_seconds) expires within ttl_seconds seconds.
  • read(user_id, token) returns the stored blob byte for byte, or None when the token does not exist.
  • refresh(user_id, token, ttl_seconds) restarts a live session's lifetime without changing its blob and returns True. For a token that does not exist it returns False and creates nothing.
  • rotate(user_id, old_token, new_token) moves a session and its remaining lifetime to new_token only when new_token is free, deciding this in one command, and returns whether it moved.
  • revoke(user_id, token) removes the session and returns whether one existed.
  • record_login(user_id) adds one login under logins_key(user_id) in one command and returns the new total, starting from 1.
  • active_for(user_id) returns each open token of that user exactly once, as the token str rather than the key name, and never another user's token.
  • active_for(user_id) reads the keyspace in batches that the server filters to that user's keys, and never lists the whole keyspace in one call.
  • cleanup(user_id) removes every session and the login counter of that user, leaves other users' keys alone, and returns how many keys it removed.
  • cleanup(user_id) frees the removed keys without blocking the server, and never lists the whole keyspace in one call.
Sample Usage

issue("u1", "abc", blob, 60) for an unused token

True

issue("u1", "abc", blob, 60) again

False

active_for("u1") after issuing tokens abc and def (tokens come back in any order)

['def', 'abc']

record_login("u1") for a user with no logins yet

1

cleanup("u1") for a user with two sessions and a login counter

3

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 solvedeasyKey Naming & ScanningGive a sprawling keyspace one namespaced scheme, then iterate it without stopping the server: drive SCAN's cursor to completion with MATCH and COUNT rather than calling KEYS, discarding the duplicates a cursor can return. Rename keys with and without overwriting, set expiries, and remove large keys with the non-blocking delete.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.