redis / node-redis

Redis Node.js client
https://redis.js.org/
MIT License
16.94k stars 1.89k forks source link

Invoke GET method with promisify but return redis server info. #1519

Open jeckzang opened 4 years ago

jeckzang commented 4 years ago

Issue

I use promisify to create a getAsync function, and I use this function to get data from redis, some code like below: const getAsync = util.promisify(redisClient.GET).bind(redisClient); let userDataUuid = await getAsync(buildUserNameCacheKey(TID, userName)); but in my log this "userDataUuid" is below, it sames redis server info.

Server^M

redis_version:5.0.5^M redis_git_sha1:00000000^M redis_git_dirty:0^M redis_build_id:442b43d467cd2b03^M

and here is my retry_strategy, I need retry evertytime, I think maybe retry logic made that server info response. retry_strategy: function(options) { logger.info("Test Redis connection!"); // reconnect after return Math.min(options.attempt * 100, 30000); }


Environment

ljluestc commented 3 months ago
const redis = require('redis');
const util = require('util');

// Create a new Redis client
const redisClient = redis.createClient({
    retry_strategy: function(options) {
        if (options.error && options.error.code === "ECONNREFUSED") {
            // End reconnecting on a specific error and flush all commands with a individual error
            return new Error('The server refused the connection');
        }
        if (options.total_retry_time > 1000 * 60 * 60) {
            // End reconnecting after a specific timeout and flush all commands with a individual error
            return new Error('Retry time exhausted');
        }
        if (options.attempt > 10) {
            // End reconnecting with built in error
            return undefined;
        }
        // reconnect after
        return Math.min(options.attempt * 100, 3000);
    }
});

// Promisify the get method
const getAsync = util.promisify(redisClient.get).bind(redisClient);

// Function to retrieve data
async function fetchData(key) {
    try {
        const value = await getAsync(key);
        console.log('Retrieved value:', value);
        return value;
    } catch (error) {
        console.error('Failed to fetch data:', error);
    }
}

// Usage
fetchData('your_redis_key_here');