Skip to content

Commit 5b2290d

Browse files
committed
add cache commands
1 parent 6add841 commit 5b2290d

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
<a name="0.6.6"></a>
2+
# 0.6.6 (2021-06-27)
3+
4+
## Changes
5+
- add `cache keys` & `cache clear` commands.
6+
17
--------------------------------------------------
28
<a name="0.6.5"></a>
39
# 0.6.5 (2021-05-20)
410

511
## Changes
612
- improve `info` packet to show metrics `reporter` and tracing `exporter`.
713
- fix 'actions' command separation.
8-
-
14+
915
--------------------------------------------------
1016
<a name="0.6.4"></a>
1117
# 0.6.4 (2020-04-09)

src/commands/cache.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"use strict";
2+
3+
const kleur = require("kleur");
4+
const _ = require("lodash");
5+
const { table, getBorderCharacters } = require("table");
6+
const { match } = require("../utils");
7+
8+
module.exports = function(vorpal, broker) {
9+
// Register cache commands
10+
vorpal
11+
.removeIfExist("cache keys")
12+
.command("cache keys", "List keys of cache entries")
13+
.option("-f, --filter <match>", "filter keys")
14+
.action((args, done) => {
15+
if (!broker.cacher) {
16+
console.log(kleur.red().bold("No cacher."));
17+
return done();
18+
}
19+
20+
if (!_.isFunction(broker.cacher.getCacheKeys)) {
21+
console.log(kleur.yellow().bold("Cacher is not compatible. Missing 'getCacheKeys' method."));
22+
return done();
23+
}
24+
25+
broker.cacher.getCacheKeys().then((entries) => {
26+
const data = [
27+
[
28+
kleur.bold("Key"),
29+
//kleur.bold("Expires at"),
30+
]
31+
];
32+
33+
entries.sort((a, b) => a.key.localeCompare(b.key));
34+
35+
//let hLines = [];
36+
37+
entries.forEach(item => {
38+
39+
if (args.options.filter && !match(item.key, args.options.filter))
40+
return;
41+
42+
data.push([
43+
item.key,
44+
//item.expiresAt
45+
]);
46+
});
47+
48+
const tableConf = {
49+
border: _.mapValues(getBorderCharacters("honeywell"), char => kleur.gray(char)),
50+
columns: {},
51+
//drawHorizontalLine: (index, count) => index == 0 || index == 1 || index == count || hLines.indexOf(index) !== -1
52+
};
53+
54+
console.log(table(data, tableConf));
55+
56+
done();
57+
}).catch(err => {
58+
console.error(err);
59+
done();
60+
});
61+
});
62+
63+
64+
// Clear cache
65+
vorpal
66+
.removeIfExist("cache clear")
67+
.command("cache clear [pattern]", "Clear cache entries")
68+
.action((args, done) => {
69+
if (broker.cacher) {
70+
broker.cacher.clean(args.pattern).then(() => {
71+
console.log(kleur.yellow().bold(args.pattern ? "Cacher cleared entries by pattern." : "Cacher cleared all entries."));
72+
done();
73+
}).catch(err => {
74+
console.error(err);
75+
done();
76+
});
77+
return;
78+
}
79+
80+
console.log(kleur.red().bold("No cacher."));
81+
done();
82+
});
83+
};

src/commands/clear.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module.exports = function(vorpal, broker) {
88
.removeIfExist("clear")
99
.command("clear [pattern]", "Clear cache entries")
1010
.action((args, done) => {
11+
console.warn(kleur.yellow().bold("The 'clear' command is deprecated. Use the 'cache clear' instead."));
12+
1113
if (broker.cacher) {
1214
broker.cacher.clean(args.pattern).then(() => {
1315
console.log(kleur.yellow().bold(args.pattern ? "Cacher cleared entries by pattern." : "Cacher cleared all entries."));

0 commit comments

Comments
 (0)