-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
// 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);Metadata
Metadata
Assignees
Labels
No labels