Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When you use netlink wrapper to encapsulate the redis protocol, you will find that when you get some data, the program will not respond all the time #35

Open
innovational opened this issue Nov 24, 2021 · 0 comments

Comments

@innovational
Copy link

innovational commented Nov 24, 2021

// Debug code
const { SocketClientTCP } = require('netlinkwrapper');
class RedisBase {
    client = null;
    constructor(host, port) {
        this.client = this.client || new SocketClientTCP(port, host);
    }
    send(cmdArr) {
        let cmd = `*${cmdArr.length}\r\n`;
        cmdArr.forEach(item => {
            item = item.toString();
            const len = this.getLength(item);
            cmd += `$${len}\r\n${item}\r\n`;
        });
        this.client.send(cmd);
        const clientReceived = this.client.receive();
        const response = clientReceived.toString();
        if (response) {
            const endIndex = response.indexOf('\r\n');
            return response.substr(endIndex);
        }
        return response;
    }
    auth(auth) {
        return this.send(['auth', auth]);
    }
    get(key) {
        return this.send(['get', key]);
    }
    set(key, value, ttl = 0) {
        const res = this.send(['set', key, value]);
        if (ttl) this.expire(key, ttl);
        return res;
    }
    getLength(str) {
        let totalLength = 0;
        let charCode;
        for (let i = 0; i < str.length; i++) {
            charCode = str.charCodeAt(i);
            if (charCode < 0x007f) {
                totalLength++;
            } else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) {
                totalLength += 2;
            } else if ((0x0800 <= charCode) && (charCode <= 0xffff)) {
                totalLength += 3;
            } else {
                totalLength += 4;
            }
        }
        return totalLength;
    }
}
const strings = '{"\\u8bc6\\u522b\\u7801":10121505070,"Q0":4,"Q2":"\\u7a0b\\u5b58\\u5de7","Q3":"132232195112045525","Q5":"110102020007","Q6":"\\u897f\\u57ce\\u533a","Q7":"\\u5e7f\\u5b89\\u95e8\\u5916\\u8857\\u9053","Q8":"\\u8f66\\u7ad9\\u897f\\u8857\\u793e\\u533a","phone":11111111111}';
const redis = new RedisBase('127.0.0.1',6379);
redis.auth('wenwu');
redis.set('test', strings);
const data = redis.get('test');
console.log(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant