-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgettingStarted.js
55 lines (42 loc) · 1.74 KB
/
gettingStarted.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require('dotenv').config();
const memjs = require('memjs');
(async () => {
if (!process.env.MEMCACHED_HOST) {
throw Error('You should first fill the .env-example file and rename it to .env');
}
// Connect to Memcached server
// Note that, for now, memjs doesn't support TLS encryption.
console.log();
console.log('🔌 Connecting to Memcached...');
console.log();
const { MEMCACHED_HOST, MEMCACHED_PORT_CLEAR, MEMCACHED_USER, MEMCACHED_PASSWORD } = process.env;
const memcached = memjs.Client.create(
`${MEMCACHED_USER}:${MEMCACHED_PASSWORD}@${MEMCACHED_HOST}:${MEMCACHED_PORT_CLEAR}`,
{
timeout: 5,
keepAlive: true
}
);
// This is a good practice to close Memcached connection when the Node.js process receives the signal "TERM".
process.once('SIGTERM', () => memcached.quit());
// MemJS client will expose these methods: get, set, add, replace, delete, increment, decrement, append, prepend, touch, flush, statsWithKey, stats, resetStats, quit, close
// Set a key
console.log('➡️ Setting key "stackhero-example-key" to value "abcd" with an expiration of 600 seconds');
await memcached.set('stackhero-example-key', 'abcd', { expires: 600 });
console.log();
// Get a key
console.log('➡️ Getting key "stackhero-example-key" value...');
const response = await memcached.get('stackhero-example-key');
console.log(`⬅️ Key "stackhero-example-key" has value "${response.value.toString()}"`);
console.log();
// Disconnect from Memcached
console.log('-'.repeat(80));
console.log('👋 Disconnecting from memcached');
memcached.quit();
console.log();
})().catch(error => {
console.error('');
console.error('🐞 An error occurred!');
console.error(error);
process.exit(1);
});