68 lines
1.9 KiB
JavaScript
68 lines
1.9 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const Redis = require('ioredis');
|
|
const dotenv = require('dotenv');
|
|
|
|
// Load environment variables
|
|
dotenv.config({ path: '.env.local' });
|
|
|
|
const redisUrl = process.env.REDIS_URL || 'redis://:mySecretPassword@localhost:6379';
|
|
|
|
// Connect to Redis
|
|
const redis = new Redis(redisUrl, {
|
|
retryStrategy: (times) => {
|
|
const delay = Math.min(times * 50, 2000);
|
|
return delay;
|
|
}
|
|
});
|
|
|
|
// Test functions
|
|
async function testRedisConnection() {
|
|
try {
|
|
// Test basic connection
|
|
console.log('Testing Redis connection...');
|
|
await redis.ping();
|
|
console.log('✅ Redis connection successful!');
|
|
|
|
// Test setting a key
|
|
console.log('\nTesting setting a key...');
|
|
await redis.set('test-key', 'Hello from Redis test script');
|
|
console.log('✅ Successfully set test-key');
|
|
|
|
// Test getting a key
|
|
console.log('\nTesting getting a key...');
|
|
const value = await redis.get('test-key');
|
|
console.log(`✅ Successfully retrieved test-key: "${value}"`);
|
|
|
|
// Test expiry
|
|
console.log('\nTesting key expiration...');
|
|
await redis.set('expiring-key', 'This will expire in 5 seconds', 'EX', 5);
|
|
console.log('✅ Set key with 5 second expiration');
|
|
console.log('Waiting for key to expire...');
|
|
|
|
// Wait for expiration
|
|
await new Promise(resolve => setTimeout(resolve, 6000));
|
|
|
|
const expiredValue = await redis.get('expiring-key');
|
|
if (expiredValue === null) {
|
|
console.log('✅ Key successfully expired');
|
|
} else {
|
|
console.log('❌ Key did not expire as expected');
|
|
}
|
|
|
|
// Clean up
|
|
console.log('\nCleaning up...');
|
|
await redis.del('test-key');
|
|
console.log('✅ Removed test keys');
|
|
|
|
console.log('\n🎉 All Redis tests passed!');
|
|
} catch (error) {
|
|
console.error('❌ Redis test failed:', error);
|
|
} finally {
|
|
// Close connection
|
|
redis.disconnect();
|
|
}
|
|
}
|
|
|
|
// Run the test
|
|
testRedisConnection();
|