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 Jedis client, key(userId, token), keyBytes(userId, token) and loginsKey(userId) are already provided. Your task has eight methods in src/main/java/com/staffcoder/redis/SessionStore.java. A session blob is raw bytes and may hold any byte value, and two requests can arrive at the same moment.
- issue(userId, token, blob, ttlSeconds) stores blob byte for byte under key(userId, token) and returns true.
- issue(userId, token, blob, ttlSeconds) returns false and leaves the stored session untouched when the token already exists, deciding this in one command.
- A session stored by issue(userId, token, blob, ttlSeconds) expires within ttlSeconds seconds.
- read(userId, token) returns the stored blob byte for byte, or null when the token does not exist.
- refresh(userId, token, ttlSeconds) 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(userId, oldToken, newToken) moves a session and its remaining lifetime to newToken only when newToken is free, deciding this in one command, and returns whether it moved.
- revoke(userId, token) removes the session and returns whether one existed.
- recordLogin(userId) adds one login under loginsKey(userId) in one command and returns the new total, starting from 1.
- activeFor(userId) returns each open token of that user exactly once, as the token string rather than the key name, and never another user's token.
- activeFor(userId) reads the keyspace in batches that the server filters to that user's keys, and never lists the whole keyspace in one call.
- cleanup(userId) removes every session and the login counter of that user, leaves other users' keys alone, and returns how many keys it removed.
- cleanup(userId) 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
trueissue("u1", "abc", blob, 60) again
falseactiveFor("u1") after issuing tokens abc and def (tokens come back in any order)
[def, abc]recordLogin("u1") for a user with no logins yet
1cleanup("u1") for a user with two sessions and a login counter
3Note: Refer to the README.md file for detailed implementation requirements.
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(userId, token) and loginsKey(userId) are already provided. Your task has eight methods in src/sessionStore.js. A session blob is a raw Buffer and may hold any byte value, and two requests can arrive at the same moment.
- issue(userId, token, blob, ttlSeconds) stores blob byte for byte under key(userId, token) and resolves to true.
- issue(userId, token, blob, ttlSeconds) resolves to false and leaves the stored session untouched when the token already exists, deciding this in one command.
- A session stored by issue(userId, token, blob, ttlSeconds) expires within ttlSeconds seconds.
- read(userId, token) resolves to the stored blob byte for byte as a Buffer, or null when the token does not exist.
- refresh(userId, token, ttlSeconds) restarts a live session's lifetime without changing its blob and resolves to true. For a token that does not exist it resolves to false and creates nothing.
- rotate(userId, oldToken, newToken) moves a session and its remaining lifetime to newToken only when newToken is free, deciding this in one command, and resolves to whether it moved.
- revoke(userId, token) removes the session and resolves to whether one existed.
- recordLogin(userId) adds one login under loginsKey(userId) in one command and resolves to the new total, starting from 1.
- activeFor(userId) resolves to each open token of that user exactly once, as the token string rather than the key name, and never another user's token.
- activeFor(userId) reads the keyspace in batches that the server filters to that user's keys, and never lists the whole keyspace in one call.
- cleanup(userId) removes every session and the login counter of that user, leaves other users' keys alone, and resolves to how many keys it removed.
- cleanup(userId) frees the removed keys without blocking the server, and never lists the whole keyspace in one call.
Sample Usage
await issue("u1", "abc", blob, 60) for an unused token
trueawait issue("u1", "abc", blob, 60) again
falseawait activeFor("u1") after issuing tokens abc and def (tokens come back in any order)
[ 'def', 'abc' ]await recordLogin("u1") for a user with no logins yet
1await cleanup("u1") for a user with two sessions and a login counter
3Note: Refer to the README.md file for detailed implementation requirements.
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
Trueissue("u1", "abc", blob, 60) again
Falseactive_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
1cleanup("u1") for a user with two sessions and a login counter
3Note: Refer to the README.md file for detailed implementation requirements.
