Problem statement
Two workers can try to claim the same job at the same time, and a read followed by a write leaves a gap where both of them succeed. Redis can decide each of these writes, and every counter update, in a single command.
Requirements
The connected Jedis client and keyFor(token), loginsKey(userId) and spendKey(userId) are already provided. Your task has seven methods in src/main/java/com/staffcoder/redis/TokenClaim.java. Each write must be one Redis command so another worker cannot change the result between a read and a write.
- claim(token, owner, ttlSeconds) stores the owner only when the token is absent and returns whether it stored the owner.
- A successful claim(token, owner, ttlSeconds) gives the token a positive lifetime no greater than the requested number of seconds.
- refreshIfPresent(token, value) replaces an existing token and returns whether it replaced one. It does nothing for an absent token.
- swap(token, value) replaces the value and returns the previous value, or null when there was none.
- recordLogin(userId) increases the integer counter and returns the new total.
- addLogins(userId, n) adds n to the integer counter, and undoLogin(userId) subtracts one.
- recordLogin(userId) on a counter that holds non-integer text raises an error to the caller and leaves the stored text unchanged.
- addSpend(userId, amount) adds a decimal amount and returns the new total.
Sample Usage
claim("job-1", "worker-a", 60)
trueclaim("job-1", "worker-b", 60) while worker-a holds the token
falseaddSpend("u1", 10.50)
10.5recordLogin("u3") when the counter holds not-a-number
ERR value is not an integer or out of rangeNote: Refer to the README.md file for detailed implementation requirements.
Two workers can try to claim the same job at the same time, and a read followed by a write leaves a gap where both of them succeed. Redis can decide each of these writes, and every counter update, in a single command.
Requirements
The connected Redis client and keyFor(token), loginsKey(userId) and spendKey(userId) are already provided. Your task has seven methods in src/tokenClaim.js. Each write must be one Redis command so another worker cannot change the result between a read and a write.
- claim(token, owner, ttlSeconds) stores the owner only when the token is absent and returns whether it stored the owner.
- A successful claim(token, owner, ttlSeconds) gives the token a positive lifetime no greater than the requested number of seconds.
- refreshIfPresent(token, value) replaces an existing token and returns whether it replaced one. It does nothing for an absent token.
- swap(token, value) replaces the value and returns the previous value, or null when there was none.
- recordLogin(userId) increases the integer counter and returns the new total.
- addLogins(userId, n) adds n to the integer counter, and undoLogin(userId) subtracts one.
- recordLogin(userId) on a counter that holds non-integer text rejects with an error and leaves the stored text unchanged.
- addSpend(userId, amount) adds a decimal amount and returns the new total.
Sample Usage
claim("job-1", "worker-a", 60)
trueclaim("job-1", "worker-b", 60) while worker-a holds the token
falseaddSpend("u1", 10.50)
10.5recordLogin("u3") when the counter holds not-a-number
ReplyError: ERR value is not an integer or out of rangeNote: Refer to the README.md file for detailed implementation requirements.
Two workers can try to claim the same job at the same time, and a read followed by a write leaves a gap where both of them succeed. Redis can decide each of these writes, and every counter update, in a single command.
Requirements
The connected Redis client and key_for(token), logins_key(user_id) and spend_key(user_id) are already provided. Your task has seven methods in src/token_claim.py. Each write must be one Redis command so another worker cannot change the result between a read and a write.
- claim(token, owner, ttl_seconds) stores the owner only when the token is absent and returns whether it stored the owner.
- A successful claim(token, owner, ttl_seconds) gives the token a positive lifetime no greater than the requested number of seconds.
- refresh_if_present(token, value) replaces an existing token and returns whether it replaced one. It does nothing for an absent token.
- swap(token, value) replaces the value and returns the previous value, or None when there was none.
- record_login(user_id) increases the integer counter and returns the new total.
- add_logins(user_id, n) adds n to the integer counter, and undo_login(user_id) subtracts one.
- record_login(user_id) on a counter that holds non-integer text raises an error to the caller and leaves the stored text unchanged.
- add_spend(user_id, amount) adds a decimal amount and returns the new total.
Sample Usage
claim("job-1", "worker-a", 60)
Trueclaim("job-1", "worker-b", 60) while worker-a holds the token
Falseadd_spend("u1", 10.50)
10.5record_login("u3") when the counter holds not-a-number
redis.exceptions.ResponseError: value is not an integer or out of rangeNote: Refer to the README.md file for detailed implementation requirements.
