{"id":1230,"date":"2015-02-16T08:35:37","date_gmt":"2015-02-16T08:35:37","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1230"},"modified":"2015-08-20T11:54:00","modified_gmt":"2015-08-20T11:54:00","slug":"redis-with-node-js","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/nodejs\/redis-with-node-js\/","title":{"rendered":"How to use Redis with Node.js"},"content":{"rendered":"<blockquote><p><a href=\"http:\/\/redis.io\/\">Redis<\/a>\u00a0<span style=\"color: #333333;\">is an open source, BSD licensed, advanced\u00a0<\/span><span style=\"font-weight: bold; color: #333333;\">key-value cache<\/span><span style=\"color: #333333;\">\u00a0and\u00a0<\/span><span style=\"font-weight: bold; color: #333333;\">store<\/span><span style=\"color: #333333;\">. It is often referred to as a\u00a0<\/span><span style=\"font-weight: bold; color: #333333;\">data structure server<\/span><span style=\"color: #333333;\">\u00a0since keys can contain\u00a0<\/span><a style=\"color: #0066aa;\" href=\"http:\/\/redis.io\/topics\/data-types-intro#strings\">strings<\/a><span style=\"color: #333333;\">,\u00a0<\/span><a style=\"color: #0066aa;\" href=\"http:\/\/redis.io\/topics\/data-types-intro#hashes\">hashes<\/a><span style=\"color: #333333;\">,\u00a0<\/span><a style=\"color: #0066aa;\" href=\"http:\/\/redis.io\/topics\/data-types-intro#lists\">lists<\/a><span style=\"color: #333333;\">,\u00a0<\/span><a style=\"color: #0066aa;\" href=\"http:\/\/redis.io\/topics\/data-types-intro#sets\">sets<\/a><span style=\"color: #333333;\">,\u00a0<\/span><a style=\"color: #0066aa;\" href=\"http:\/\/redis.io\/topics\/data-types-intro#sorted-sets\">sorted sets<\/a><span style=\"color: #333333;\">,\u00a0<\/span><a style=\"color: #0066aa;\" href=\"http:\/\/redis.io\/topics\/data-types-intro#bitmaps\">bitmaps<\/a><span style=\"color: #333333;\">\u00a0and\u00a0<\/span><a style=\"color: #0066aa;\" href=\"http:\/\/redis.io\/topics\/data-types-intro#hyperloglogs\">hyperloglogs<\/a><span style=\"color: #333333;\">.<\/span><\/p><\/blockquote>\n<p>Comparison with Memcache and Memcached can be found <a href=\"http:\/\/anton.logvinenko.name\/en\/blog\/redis-vs-memcache-vs-memcached.html\">here<\/a>, and the naming distinction can be found <a href=\"http:\/\/anton.logvinenko.name\/en\/blog\/redis-vs-memcache-vs-memcached.html\">here<\/a>.<\/p>\n<p>You can download the installation zip file <a href=\"http:\/\/redis.io\/download\">here<\/a>\u00a0(I used the Windows version), and extract it to <strong>C:\\redis<\/strong>. To run the Redis server (in order to be able to save to it and query it) you have to execute the <strong>redis-server.exe<\/strong> from this directory.<\/p>\n<p>To use Redis with Node.js you have install the <a href=\"https:\/\/github.com\/mranney\/node_redis\">redis module<\/a>:<\/p>\n<pre class=\"lang:default decode:true\">npm install redis<\/pre>\n<p>A simple\u00a0usage example:<\/p>\n<pre class=\"lang:default decode:true \">var redis = require(\"redis\"),\r\n    client = redis.createClient();\r\n\r\n\/\/ if you'd like to select database 3, instead of 0 (default), call\r\n\/\/ client.select(3, function() { \/* ... *\/ });\r\n\r\nclient.on(\"error\", function (err) {\r\n    console.log(\"Error \" + err);\r\n});\r\n\r\nclient.set(\"string key\", \"string val\", redis.print);\r\nclient.hset(\"hash key\", \"hashtest 1\", \"some value\", redis.print);\r\nclient.hset([\"hash key\", \"hashtest 2\", \"some other value\"], redis.print);\r\nclient.hkeys(\"hash key\", function (err, replies) {\r\n    console.log(replies.length + \" replies:\");\r\n    replies.forEach(function (reply, i) {\r\n        console.log(\"    \" + i + \": \" + reply);\r\n    });\r\n    client.quit();\r\n});<\/pre>\n<p>A <strong>lot<\/strong>\u00a0more examples <a href=\"https:\/\/github.com\/mranney\/node_redis\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u00a0is an open source, BSD licensed, advanced\u00a0key-value cache\u00a0and\u00a0store. It is often referred to as a\u00a0data structure server\u00a0since keys can contain\u00a0strings,\u00a0hashes,\u00a0lists,\u00a0sets,\u00a0sorted sets,\u00a0bitmaps\u00a0and\u00a0hyperloglogs. Comparison with Memcache and Memcached can be&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1232,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1230"}],"version-history":[{"count":5,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1230\/revisions"}],"predecessor-version":[{"id":2137,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1230\/revisions\/2137"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1232"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}