Skip to content

Commit 296b18e

Browse files
committed
blog: add cache cleaner mixin
1 parent 9d81b14 commit 296b18e

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

blog/mixins/cache.cleaner.mixin.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
module.exports = function(serviceNames) {
4+
const events = {};
5+
6+
serviceNames.forEach(name => {
7+
events[`cache.clean.${name}`] = function() {
8+
if (this.broker.cacher) {
9+
this.logger.debug(`Clear local '${this.name}' cache`);
10+
this.broker.cacher.clean(`${this.name}.*`);
11+
}
12+
};
13+
});
14+
15+
return {
16+
events
17+
};
18+
};

blog/moleculer.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ module.exports = {
66
// Append hostname to nodeID. It will be unique when scale up instances in Docker
77
nodeID: (process.env.NODEID ? process.env.NODEID + "-" : "") + os.hostname().toLowerCase(),
88
metrics: true,
9-
cacher: process.env.NODE_ENV == "production"
9+
cacher: true
1010
};

blog/services/likes.service.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ const _ = require("lodash");
44
const DbService = require("moleculer-db");
55
const MongooseAdapter = require("moleculer-db-adapter-mongoose");
66
const Like = require("../models/like.model");
7+
const CacheCleaner = require("../mixins/cache.cleaner.mixin");
78
const Fakerator = require("fakerator");
89
const fake = new Fakerator();
910

1011
module.exports = {
1112
name: "likes",
12-
mixins: [DbService],
13+
mixins: [DbService, CacheCleaner(["users", "posts"])],
1314
adapter: new MongooseAdapter(process.env.MONGO_URI || "mongodb://localhost/moleculer-blog"),
1415
model: Like,
1516

@@ -56,7 +57,10 @@ module.exports = {
5657

5758
return this.Promise.all(promises)
5859
.then(() => this.adapter.count())
59-
.then(count => this.logger.info(`Generated ${count} likes!`));
60+
.then(count => {
61+
this.logger.info(`Generated ${count} likes!`);
62+
this.clearCache();
63+
});
6064

6165
})
6266
.catch(err => {

blog/services/posts.service.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ const _ = require("lodash");
44
const DbService = require("moleculer-db");
55
const MongooseAdapter = require("moleculer-db-adapter-mongoose");
66
const Post = require("../models/post.model");
7+
const CacheCleaner = require("../mixins/cache.cleaner.mixin");
78
const Fakerator = require("fakerator");
89
const fake = new Fakerator();
910

1011
module.exports = {
1112
name: "posts",
12-
mixins: [DbService],
13+
mixins: [DbService, CacheCleaner(["users", "likes"])],
1314
adapter: new MongooseAdapter(process.env.MONGO_URI || "mongodb://localhost/moleculer-blog"),
1415
model: Post,
1516

@@ -73,8 +74,10 @@ module.exports = {
7374
coverPhoto: fake.random.number(1, 20) + ".jpg",
7475
createdAt: fakePost.created
7576
};
76-
}))
77-
.then(posts => this.logger.info(`Generated ${posts.length} posts!`));
77+
})).then(posts => {
78+
this.logger.info(`Generated ${posts.length} posts!`);
79+
this.clearCache();
80+
});
7881

7982
}).catch(err => {
8083
if (err.name == "ServiceNotFoundError") {

blog/services/users.service.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { MoleculerError } = require("moleculer").Errors;
77
const DbService = require("moleculer-db");
88
const MongooseAdapter = require("moleculer-db-adapter-mongoose");
99
const User = require("../models/user.model");
10+
const CacheCleaner = require("../mixins/cache.cleaner.mixin");
1011
const Fakerator = require("fakerator");
1112
const fake = new Fakerator();
1213

@@ -30,7 +31,7 @@ function hashPassword(password) {
3031

3132
module.exports = {
3233
name: "users",
33-
mixins: [DbService],
34+
mixins: [DbService, CacheCleaner(["users"])],
3435
adapter: new MongooseAdapter(process.env.MONGO_URI || "mongodb://localhost/moleculer-blog"),
3536
model: User,
3637

@@ -81,7 +82,10 @@ module.exports = {
8182
author: false
8283
};
8384
})))
84-
.then(users => this.logger.info(`Generated ${users.length} users!`));
85+
.then(users => {
86+
this.logger.info(`Generated ${users.length} users!`);
87+
this.clearCache();
88+
});
8589
}
8690
},
8791

0 commit comments

Comments
 (0)