Created
December 8, 2018 02:54
-
-
Save rangercyh/4b176a9d76f084ac00d82e1b985cf37b to your computer and use it in GitHub Desktop.
redis process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redis = require('redis'); | |
var leveldb = require('level'); | |
var root = "./"; | |
var dbfile = root + "backup.db"; | |
var channel = [ | |
"__keyevent@0__:set", | |
"__keyevent@0__:hset", | |
"__keyevent@0__:hmset", | |
"__keyevent@0__:incrby" | |
]; | |
function beginSubscribe(db) { | |
var client = redis.createClient(6379, '127.0.0.1', { auth_pass: "tinygame-redis.33455432" }); | |
var fetchClient = redis.createClient(6379, '127.0.0.1', { auth_pass: "tinygame-redis.33455432" }); | |
client.once("connect", function() { | |
for (var i = 0; i < channel.length; ++i) { | |
client.subscribe(channel[i]); | |
} | |
}); | |
client.on('message', function(c, key) { | |
var keyStr = ""; | |
console.log(c, key, arguments); | |
if (c === channel[0] || c === channel[3]) { | |
fetchClient.get(key, function(err, res) { | |
if (err) { | |
console.log(c, 'get fail', key); | |
} else { | |
console.log(c, 'fetch data = ', key, res); | |
keyStr = 's|' + key; | |
console.log(keyStr); | |
db.put(keyStr, res); | |
} | |
}); | |
} | |
if (c === channel[1] || c === channel[2]) { | |
fetchClient.hgetall(key, function(err, res) { | |
if (err) { | |
console.log(c, 'hgetall fail', key); | |
} else { | |
console.log(c, 'fetch data = ', key, res); | |
keyStr = 'h|' + key; | |
console.log(keyStr); | |
db.put(keyStr, JSON.stringify(res)); | |
} | |
}); | |
} | |
}); | |
client.on('end', function() { | |
console.log('redis server shutdown'); | |
fetchClient.quit(); | |
}); | |
client.on("error", function(e) { | |
console.log(e); | |
}); | |
fetchClient.on("error", function(e) { | |
console.log(e); | |
}); | |
} | |
leveldb(dbfile, function(err, db) { | |
if (err) { | |
console.log('open dbfile fail', err); | |
process.exit(0); | |
} | |
beginSubscribe(db); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redis = require('redis'); | |
var leveldb = require('level'); | |
var root = "./"; | |
var dbfile = root + "backup.db"; | |
function syncDB(db) { | |
var client = redis.createClient(6379, '127.0.0.1', { auth_pass: "tinygame-redis.33455432" }); | |
client.once("connect", function() { | |
db.createReadStream() | |
.on('data', function(data) { | |
console.log(data.key, '=', data.value); | |
var pos = data.key.indexOf('|'); | |
var op = data.key.slice(0, pos); | |
var key = data.key.slice(pos + 1); | |
if (op === 's') { | |
client.set(key, data.value, function(err) { | |
if (err) { | |
console.log('redis set error', err); | |
console.log(key, data.value); | |
} else { | |
console.log('redis set success'); | |
console.log(key, data.value); | |
} | |
}); | |
} | |
if (op === 'h') { | |
client.hmset(key, JSON.parse(data.value), function(err) { | |
if (err) { | |
console.log('redis hmset error', err); | |
console.log(key, data.value); | |
} else { | |
console.log('redis hmset success'); | |
console.log(key, data.value); | |
} | |
}); | |
} | |
}) | |
.on('error', function(err) { | |
console.log('err = ', err); | |
}) | |
.on('close', function() { | |
console.log('stream close'); | |
}) | |
.on('end', function() { | |
console.log('stream end'); | |
}); | |
}); | |
client.on('end', function() { | |
console.log('redis server shutdown'); | |
client.quit(); | |
}); | |
client.on("error", function(e) { | |
console.log(e); | |
}); | |
} | |
leveldb(dbfile, function(err, db) { | |
if (err) { | |
console.log('open dbfile fail', err); | |
process.exit(0); | |
} | |
syncDB(db); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment