...
|
...
|
@@ -11,6 +11,18 @@ class ApiCache { |
|
|
this.memcached = new Memcached(host);
|
|
|
}
|
|
|
|
|
|
setKey(key, value, ttl) {
|
|
|
this._log(`setting ${key}`);
|
|
|
|
|
|
this.memcached.set(key, value, ttl, (err) => {
|
|
|
if (err) {
|
|
|
this._log(`set ${key} fail`)
|
|
|
} else {
|
|
|
this._log(`set ${key} success`)
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
delKey(key) {
|
|
|
this._log(`deleting ${key}`)
|
|
|
|
...
|
...
|
@@ -34,8 +46,73 @@ class ApiCache { |
|
|
});
|
|
|
}
|
|
|
|
|
|
find(condition) {
|
|
|
let count = 0;
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
this.memcached.items((err, result) => {
|
|
|
if (err) console.error(err);
|
|
|
|
|
|
if (result.length === 0) {
|
|
|
this._log('empty items')
|
|
|
}
|
|
|
// for each server...
|
|
|
result.forEach(itemSet => {
|
|
|
var keys = Object.keys(itemSet);
|
|
|
keys.pop(); // we don't need the "server" key, but the other indicate the slab id's
|
|
|
|
|
|
var len = keys.length;
|
|
|
|
|
|
if (keys.length === 0) {
|
|
|
this._log('empty item set');
|
|
|
}
|
|
|
const keyList = [];
|
|
|
|
|
|
keys.forEach(stats => {
|
|
|
// get a cachedump for each slabid and slab.number
|
|
|
this.memcached.cachedump(itemSet.server, parseInt(stats, 10), itemSet[stats].number, (err, response) => {
|
|
|
|
|
|
if (response) {
|
|
|
if (_.isArray(response)) {
|
|
|
_.each(response, (item) => {
|
|
|
count++;
|
|
|
|
|
|
if (condition(item.key)) {
|
|
|
keyList.push(item.key);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
else {
|
|
|
count++;
|
|
|
|
|
|
if (condition(response.key)) {
|
|
|
keyList.push(response.key);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if (--len === 0) {
|
|
|
this.memcached.getMulti(keyList, (err, data) => {
|
|
|
this.memcached.end();
|
|
|
|
|
|
if (err) {
|
|
|
reject(err);
|
|
|
}
|
|
|
|
|
|
resolve(data);
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
})
|
|
|
})
|
|
|
});
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
stats() {
|
|
|
this.memcached.stats( (err, stats) => {
|
|
|
this.memcached.stats((err, stats) => {
|
|
|
if (err) {
|
|
|
|
|
|
} else {
|
...
|
...
|
@@ -43,7 +120,7 @@ class ApiCache { |
|
|
stats = stats[0];
|
|
|
}
|
|
|
|
|
|
stats.hits_percent = (stats.get_hits / (stats.get_hits + stats.get_misses) * 100).toFixed(2) + '%';
|
|
|
stats.hits_percent = (stats.get_hits / (stats.get_hits + stats.get_misses) * 100).toFixed(2) + '%';
|
|
|
stats.heap_info = (stats.bytes / 1024 / 1024).toFixed(4) + 'mb' + ' / ' + stats.limit_maxbytes / 1024 / 1024 + 'mb';
|
|
|
|
|
|
ws.broadcast(`/api_cache/monit`, {
|
...
|
...
|
@@ -55,7 +132,7 @@ class ApiCache { |
|
|
}
|
|
|
|
|
|
restart() {
|
|
|
this.memcached.stats( (err, stats) => {
|
|
|
this.memcached.stats((err, stats) => {
|
|
|
if (err) {
|
|
|
|
|
|
} else {
|
...
|
...
|
@@ -73,7 +150,7 @@ class ApiCache { |
|
|
conn.on('ready', async() => {
|
|
|
try {
|
|
|
conn.exec(`sudo kill ${pid}`, (err, stream) => {
|
|
|
if(err) {
|
|
|
if (err) {
|
|
|
console.log(err);
|
|
|
} else {
|
|
|
stream.on('close', () => {
|
...
|
...
|
@@ -110,8 +187,8 @@ class ApiCache { |
|
|
|
|
|
let count = 0;
|
|
|
|
|
|
this.memcached.items( ( err, result ) => {
|
|
|
if( err ) console.error( err );
|
|
|
this.memcached.items((err, result) => {
|
|
|
if (err) console.error(err);
|
|
|
|
|
|
if (result.length === 0) {
|
|
|
this._log('empty items')
|
...
|
...
|
@@ -119,25 +196,25 @@ class ApiCache { |
|
|
// for each server...
|
|
|
result.forEach(itemSet => {
|
|
|
|
|
|
var keys = Object.keys( itemSet );
|
|
|
keys.pop(); // we don't need the "server" key, but the other indicate the slab id's
|
|
|
|
|
|
var keys = Object.keys(itemSet);
|
|
|
keys.pop(); // we don't need the "server" key, but the other indicate the slab id's
|
|
|
|
|
|
var len = keys.length;
|
|
|
|
|
|
if (keys.length === 0) {
|
|
|
this._log('empty item set');
|
|
|
}
|
|
|
|
|
|
|
|
|
keys.forEach(stats => {
|
|
|
|
|
|
|
|
|
// get a cachedump for each slabid and slab.number
|
|
|
this.memcached.cachedump( itemSet.server, parseInt(stats, 10), itemSet[stats].number, ( err, response ) => {
|
|
|
this.memcached.cachedump(itemSet.server, parseInt(stats, 10), itemSet[stats].number, (err, response) => {
|
|
|
// dump the shizzle
|
|
|
|
|
|
if (response) {
|
|
|
if (_.isArray(response)) {
|
|
|
_.each(response, keyObj => {
|
|
|
count ++ ;
|
|
|
count++;
|
|
|
if (keyObj.key && (keyObj.key.indexOf(key1) >= 0 || keyObj.key.indexOf(key2) >= 0 || keyObj.key.indexOf(key) >= 0)) {
|
|
|
this.delKey(keyObj.key);
|
|
|
} else {
|
...
|
...
|
@@ -145,7 +222,7 @@ class ApiCache { |
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
count ++;
|
|
|
count++;
|
|
|
if (response.key && (response.key.indexOf(key1) >= 0 || response.key.indexOf(key2) >= 0 || response.key.indexOf(key) >= 0)) {
|
|
|
this.delKey(response.key);
|
|
|
} else {
|
...
|
...
|
@@ -154,7 +231,7 @@ class ApiCache { |
|
|
}
|
|
|
}
|
|
|
|
|
|
len --;
|
|
|
len--;
|
|
|
|
|
|
if (len === 0) {
|
|
|
this.memcached.end();
|
...
|
...
|
|