{"id":2717,"date":"2018-11-07T11:22:32","date_gmt":"2018-11-07T09:22:32","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2717"},"modified":"2018-11-07T11:22:32","modified_gmt":"2018-11-07T09:22:32","slug":"node-js-domino-db-docker-6-using-memcached","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2717","title":{"rendered":"node.js, domino-db &#038; Docker (6): Using memcached"},"content":{"rendered":"<h2>mem.js<\/h2>\n<p>I am using <a href=\"https:\/\/memjs.netlify.com\/\" target=\"_blank\" rel=\"noopener\">mem.js<\/a> as client library for accessing memcached. To use it, the first thing to do is to add the requirement to your package.json:<\/p>\n<pre><code>npm install memjs --save<\/code><\/pre>\n<h2>MemcachedFactory<\/h2>\n<p>Then we can create a simple helper class to have an abstraction layer between our code and the library itself.<\/p>\n<p>1. Create a folder in <em>\/app<\/em> named <em>classes<\/em><\/p>\n<p>2. Create a new file with the name <em>MemcachedFactory.js<\/em><\/p>\n<p>3. Add the following code:<\/p>\n<pre><code>const memjs = require('memjs');\r\n\/**\r\n * Helper class for using Memcache\r\n * \r\n * @author Sven Hasselbach\r\n * @version 0.1\r\n *\/\r\nclass MemcachedFactory {\r\n\r\n    constructor() {\r\n      this.client = memjs.Client.create('127.0.0.1:11211');\r\n    }\r\n\r\n    \/**\r\n     * returns a single instance of the class\r\n     *\/\r\n    static getInstance() {\r\n      if (this.instance == null) {\r\n        this.instance = new MemcachedFactory();\r\n      }\r\n      return this.instance;\r\n    }\r\n\r\n    \/**\r\n     * stores a value in memcache\r\n     * @param {string} key \r\n     *  the key used\r\n     * @param {*} value\r\n     *  the value to store\r\n     * @param {number} ttl \r\n     *  time-to-live in seconds\r\n     *\/\r\n    set(key, value, ttl) {\r\n      this.client.set(key, value, { expires: ttl }, err =&gt; {\r\n        if (err) {\r\n          console.log(err);\r\n          throw err;\r\n        }\r\n      });\r\n    }\r\n    \/**\r\n     * gets a value from memcache\r\n     * \r\n     * @param {string} key \r\n     *  the key used\r\n     * @param {function} callback\r\n     *  the callback containing the value\r\n     *\/\r\n    get(key, callback) {\r\n      this.client.get(key, (err, value) =&gt; {\r\n        if (err) {\r\n          console.error(err);\r\n          callback(err);\r\n        }\r\n        if (value == null) {\r\n          callback(err, null);\r\n        } else {\r\n          callback(err, value.toString());\r\n        }\r\n      });\r\n  }\r\n}\r\nmodule.exports = MemcachedFactory;<\/code><\/pre>\n<p>5. To use the class in our code, we have to add the requirement first:<\/p>\n<pre><code>const mf = require('..\/classes\/MemcachedFactory');<\/code><\/pre>\n<p>6. Here is a small example how the class is used:<\/p>\n<pre><code>mf.getInstance().get(key, (error, value) =&gt; {\r\n  if (error) {\r\n    \/\/ handle error here\r\n  }else{\r\n    \/\/ we have a value\r\n    console.log(`The value is ${value}`); \r\n  }\r\n});<\/code><\/pre>\n<p>The <em>getInstance<\/em> method returns an instance of the class. Then the <em>get<\/em> method is used with the key to retreive, and a callback method which is called when the request to memcached is completed.<\/p>\n<p>The <em>set<\/em> method allows to put a key to memcached, and with ttl we can define how long the key is valid and stored.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mem.js I am using mem.js as client library for accessing memcached. To use it, the first thing to do is to add the requirement to your package.json: npm install memjs &#8211;save MemcachedFactory Then we can create a simple helper class &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2717\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[139,9],"tags":[140,143,132],"class_list":["post-2717","post","type-post","status-publish","format-standard","hentry","category-es6","category-javascript","tag-es6","tag-memcached","tag-node-js"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2717"}],"version-history":[{"count":3,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2717\/revisions"}],"predecessor-version":[{"id":2720,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2717\/revisions\/2720"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}