Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

How do I use catbox-memory #790

Closed
ShayBox opened this issue May 15, 2019 · 7 comments
Closed

How do I use catbox-memory #790

ShayBox opened this issue May 15, 2019 · 7 comments
Labels

Comments

@ShayBox
Copy link

ShayBox commented May 15, 2019

I'm trying to setup a basic catbox cache that stores the output for a few seconds in memory, nothing too advanced, as far as I can tell I need @hapi/hapi, @hapi/catbox, and @hapi/catbox-memory.

const server = new Server({
	port,
	cache: {
		name: 'realtime',
		engine: require('@hapi/catbox'),
	},
});
const realtimeCache = server.cache({
	cache: 'realtime',
	expiresIn: 1000,
	segment: 'realtime',
	generateFunc: () => device.getPowerUsage(),
	generateTimeout: 2000,
});

server
	.route({
		method: 'GET',
		path: '/tplink/realtime',
		handler: realtimeCache.get('realtime'),
	});
(node:1420) UnhandledPromiseRejectionWarning: TypeError: this.connection.validateSegmentName is not a function
    at module.exports.validateSegmentName (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/catbox/lib/client.js:48:32)
    at new module.exports.internals.Policy (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/catbox/lib/policy.js:71:35)
    at module.exports.internals.Core._cachePolicy (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/core.js:563:16)
    at internals.Server.policy [as cache] (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/server.js:542:27)
    at Object.start (/home/shaybox/Documents/Javascript/API/build/Server.js:52:38)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:1420) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1420) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@hueniverse
Copy link

Your engine is wrong. Should be require('@hapi/catbox-memory')

@ShayBox
Copy link
Author

ShayBox commented May 15, 2019

Updated Server

		const server = new Server({
			port,
			routes: {
				cors: true,
			},
			cache: {
				name: 'realtime',
				engine: new catboxEngine(),
			},
		});

Error

(node:52174) UnhandledPromiseRejectionWarning: Error: Invalid handler options  {
  "value" [1]: -- missing --
}

[1] "value" must be a Function
[2] "value" must have 1 children
    at Object.exports.apply (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/config.js:19:15)
    at new module.exports.internals.Route (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/route.js:76:32)
    at internals.Server._addRoute (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/server.js:485:23)
    at internals.Server.route (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/server.js:478:22)
    at Object.start (/home/shaybox/Documents/Javascript/API/build/Server.js:64:14)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:52174) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:52174) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

EDIT: It is using catbox-memory now, it said it needed to be constructed

@hueniverse
Copy link

What version of hapi are you on? See hapijs/hapi#3871 for info on how to configure the cache.

@ShayBox
Copy link
Author

ShayBox commented May 15, 2019

    "@hapi/catbox-memory": "^4.1.0",
    "@hapi/hapi": "^18.3.1",
    "@types/hapi__catbox-memory": "^4.1.0",
    "@types/hapi__hapi": "^18.2.1",

It should be the correct version of catbox-memory for hapi

@ShayBox
Copy link
Author

ShayBox commented May 15, 2019

I changed it to the example on that page

		const server = new Server({
			port,
			routes: {
				cors: true,
			},
		});

		server.cache.provision({
			provider: {
					constructor: require('@hapi/catbox-memory'),
					options: {
							partition: 'x',
							maxByteSize: 10000,
					},
			},
			name: 'countries',
	});
		const realtimeCache = server.cache({
			cache: 'countries',
			expiresIn: 1000,
			segment: 'realtime',
			generateFunc: () => device.getPowerUsage(),
			generateTimeout: 2000,
		});

		server
			.route({
				method: 'GET',
				path: '/tplink/realtime',
				handler: realtimeCache.get('realtime'),
			});

And it still gives this error

(node:80674) UnhandledPromiseRejectionWarning: Error: Invalid handler options  {
  "value" [1]: -- missing --
}

[1] "value" must be a Function
[2] "value" must have 1 children
    at Object.exports.apply (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/config.js:19:15)
    at new module.exports.internals.Route (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/route.js:76:32)
    at internals.Server._addRoute (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/server.js:485:23)
    at internals.Server.route (/home/shaybox/Documents/Javascript/API/node_modules/@hapi/hapi/lib/server.js:478:22)
    at Object.start (/home/shaybox/Documents/Javascript/API/build/Server.js:69:14)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:80674) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:80674) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

EDIT: This is the full code, it would probably help more then just segments https://paste.nomsy.net/obotaweqoy.js

@hueniverse
Copy link

Looks like line 80 in your code, handler is missing () => .

@ShayBox
Copy link
Author

ShayBox commented May 15, 2019

I have never seen in any examples seen a handler option, but I'll try when I'm able to.

EDIT: Nevermind, I see what you mean now, I thought you meant my cache needed a handler function but my route was missing the () =>, Thanks it works now 👍

@ShayBox ShayBox closed this as completed May 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants