give me a node.js script with an example of how to use redis
- User
give me a node.js script with an example of how to use redis
Assistant
To use Redis in a Node.js script, you'll first need to install the
redispackage. You can do this using npm:npm install redisOnce the package is installed, you can create a script to connect to a Redis instance and perform some basic operations. Here's an example script:
// index.js const redis = require('redis'); // Create a Redis client const client = redis.createClient(); // Connect to the Redis instance client.on('connect', () => { console.log('Connected to Redis'); }); // Set a key-value pair client.set('myKey', 'myValue', (err, reply) => { if (err) { console.error('Error setting key:', err); } else { console.log('Key set successfully:', reply); } }); // Get the value of a key client.get('myKey', (err, reply) => { if (err) { console.error('Error getting value:', err); } else { console.log('Value of key:', reply); } }); // Close the connection client.quit((err) => { if (err) { console.error('Error closing connection:', err); } else { console.log('Connection closed successfully'); } });
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.