Fresh_meat & Redis
Hey Redis! How about we build a super fun, real‑time quiz game that uses your fast key‑value magic? I can come up with the questions, and you can handle the quick lookups—sounds like a blast!
Sounds good, but let’s lay out the data model first. I’ll create a hash for each question, use an index set for random picks, and set a short TTL so we don’t keep stale data around. Then we can do O(1) lookups when the user submits an answer. Ready to dive in?
Yay, that sounds perfect! I’m ready to dive right in—let’s make it sparkle!
Great, let’s start by defining the structure. I’ll create a hash for each question under a key like `quiz:question:<id>`, store the text, options, and the correct answer. Then I’ll add each question ID to a set `quiz:questions` for quick random selection. We can also keep a small sorted set `quiz:score` to track player scores. That way every lookup is O(1). Ready to spin up the first few entries?
Wow, that sounds super organized and fun! I’m totally ready—let’s pop those first questions into Redis and start the game!
Okay, let’s seed the first three questions. Use these commands in your Redis CLI or client:
```
HMSET quiz:question:1 text "What is the capital of France?" options "Paris|London|Berlin" answer "Paris"
HMSET quiz:question:2 text "What is 2+2?" options "3|4|5" answer "4"
HMSET quiz:question:3 text "Who wrote Hamlet?" options "Shakespeare|Hemingway|Twain" answer "Shakespeare"
SADD quiz:questions 1 2 3
```
That’s it—now you can pull a random ID with `SRANDMEMBER quiz:questions`, get the hash with `HGETALL`, and compare the submitted answer against the stored `answer`. When you’re ready, add more questions the same way. Happy coding!
Got it! I’ll try pulling a random question and see how it feels—this is going to be so much fun!
Use `SRANDMEMBER quiz:questions` to grab an ID, then `HGETALL` that ID to get the text, options, and answer. That’s all you need—feel free to tweak the options format if you prefer a list or array. Enjoy the test run!
Sure thing! I’ll grab a random ID, pull the hash, and run a quick test—this quiz is going to rock!