Consider this Node.js code using Redis to cache a value and then retrieve it:
import { createClient } from 'redis';
async function cacheExample() {
const client = createClient();
await client.connect();
await client.set('key1', 'value1', { EX: 10 });
const result = await client.get('key1');
await client.disconnect();
return result;
}
cacheExample().then(console.log);What will be printed to the console?
import { createClient } from 'redis'; async function cacheExample() { const client = createClient(); await client.connect(); await client.set('key1', 'value1', { EX: 10 }); const result = await client.get('key1'); await client.disconnect(); return result; } cacheExample().then(console.log);
Think about what happens immediately after setting a key with expiration.
The code sets a key 'key1' with value 'value1' and expiration of 10 seconds. Since the get happens immediately after setting, the key exists and returns 'value1'.
Given the Redis client in Node.js, which code snippet correctly sets a key 'session' with value 'abc123' that expires after 5 minutes?
Check the official Redis client method signature for expiration options.
Option A uses the correct object syntax with EX (seconds) as a number. Option A is invalid syntax. Option A uses PX (milliseconds) but 300 ms is too short for 5 minutes; it should be 300000 ms. Option A passes EX as a string which is invalid.
Examine this code snippet:
import { createClient } from 'redis';
const client = createClient();
async function getCache() {
const value = await client.get('missingKey');
console.log(value.length);
}
getCache();What error will this code throw and why?
import { createClient } from 'redis'; const client = createClient(); async function getCache() { const value = await client.get('missingKey'); console.log(value.length); } getCache();
What does Redis return when a key does not exist?
If the key does not exist, Redis returns null. Trying to access .length on null causes a TypeError.
Consider this code snippet:
import { createClient } from 'redis';
async function incrementCounter() {
const client = createClient();
await client.connect();
await client.set('count', '5');
const count = await client.incr('count');
await client.disconnect();
return count;
}
incrementCounter().then(console.log);What will be printed to the console?
import { createClient } from 'redis'; async function incrementCounter() { const client = createClient(); await client.connect(); await client.set('count', '5'); const count = await client.incr('count'); await client.disconnect(); return count; } incrementCounter().then(console.log);
What does the Redis INCR command do to the string value?
Redis INCR converts the string to a number, increments it by 1, and returns the new value. So '5' becomes 6.
You have a Node.js app running on multiple servers. Each server connects to the same Redis instance for caching. Which statement is true about the cache behavior?
Think about how Redis works as a centralized cache.
Redis is a centralized cache server. All connected clients share the same data. So when one server sets a cache key, others see it immediately.