Skip to content

Private cache #1267

Answered by icebob
524c asked this question in Q&A
Dec 9, 2023 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

To cover it, in my project I created a custom Cacher middleware. In my case I add a project ID to the cache keys as prefix. Here is the code of my middleware:

// project-cacher.middleware.js
const _ = require("lodash");
const { isFunction, isObject } = require("moleculer").Utils;
const Cachers = require("moleculer").Cachers;

module.exports = function ProjectCacherMiddleware(opts = {}) {
	let cacher;

	return {
		name: "ProjectCacher",

		created(broker) {
			if (broker.cacher) {
				throw new Error(
					"Built-in cacher is enabled! Project Cacher can't work besides built-in cacher!"
				);
			}

			cacher = Cachers.resolve(opts);
			cacher.init(broker);

			broker.cacher = cacher;
		},

		

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@524c
Comment options

Answer selected by 524c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants