Redis Tutorial Part 3 – Redis Data structures and working with Node.js ioredis

Redis Data structures Key-value stores mostly support string keys and string data but Redis does a lot more than that. Redis is not just a simple key-value string store It supports complex kinds of data structures, not just string. We will look more into this property of Redis in this tutorial and see how to utilize this using Node.js and ioredis module. Get the ioredis module installed on your app from   1 String – Basic kind of string. Lets us say the name of a person. We will see how we can store this value using ioredis We set value bar into key foo using set and get the value using get.

2 Binary Safe String – Can contain any type of data represented as a string: PNG images or serialized objects, for example. Here we have a base64 encoded data of an image.We store this data in redis.Remember a value can’t be bigger than 512 MB. base64 of png – iVBORw0KGgoAAAANSUhEUgAAACMAAAAjCAYAAAAe2bNZAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gUfFBM7nAl7igAAAPVJREFUWMNj1G/R/88wSAATwyACLMgcYzljojXefHmT4cvPLwzU1A93TJRpFEOZWxnRhj37+IzBa4oXA7n6b7y8wRAxJwK7Y3g5eBkYGBgYNl7ayPD843O8BmXYZjBI8UuhiMH0Ews0xDXwRxMDAwPDpoubGM4+OkvQMcM+AY86ZtQxo46hSXXAwMDA4Kfvx2AibzKwjvn84zMDAwMDg7+eP9HVATKA6SelOkEHjMhNCFJC5NmHZxgGUqqfcbQ9M9qeobQ9QyzA1Z4htj1EVHuGUkBJe2i0Ohh1zKhjRkZ7hlJASXuIhVrtEUrbQ6PtmdHcRA4AAAq6hb9r4YF9AAAAAElFTkSuQmCC

We can store buffer data as well in redis..

3 Lists – Collections of elements ordered according to the order of insertion

    4 Sets – Sets of data. Here we have a…

Redis Tutorial Part 1 – Introduction

Redis – Remote Dictionary Server Redis (Remote Dictionary Server) is an opensource key-value store. It can be used as a database, cache and message broker. Redis is an in-memory store which means that it primarily relies on main memory/primary memory for data storage in comparison to other database storages like Mysql that relies on disk storage.   Redis is written in C language and it supports cross-platform. In-memory databases like Redis are faster compared to disk storage databases like Mysql. Redis is a key-value store. Which means that data is stored like name=’John’ The value john an be accessed using the key name. Key value is a string. Redis is single-threaded(One operation at a time) fast and scalable. The speed allows redis to be used as cache memory. Install Redis on Linux sudo apt-get update sudo apt-get install redis-server For Redis server redis-server For Redis Command Line Interface redis-cli To get configuration for redis goto redis-cli.type CONFIG type of configuration to get configuration values in redis.conf.   There are two processes in Redis architecture. REDIS-CLIENT – Manages client interaction REDIS-SERVER – Manages data storage in the memory and forms the major part of the Redis architecture. Single Redis instance can manage multiple redis…

Angular interview questions – part 1

1 What is typescript? It is a strict syntactical superset of JavaScript, and adds optionaltype-checking along with the latest ECMAScript features. 2 What is two way data binding in angular? Two-way Binding. Data binding in AngularJS is the synchronization between the model and the view. When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well. Binding   3 Types of bindings in angular ? 1 Interpolation -eg- <h1>{{}}</h1> //interpolation is in braces 2 Two way binding – <input [(ngModel)]=””/> 3 One way binding – <h1 [innerText]=””></h1> //Bindings like inner text 4 Event Binding – <button ng-click=”userData()”>Send</h1> //Binding events like click   4 What are authGuards? With AuthGuards/RouterGuards we can prevent users from accessing areas that they’re not allowed to access.Or prevent navigating away. There are four kinds of guards.You have to import it from the @angular/router module CanActivate Checks to see if a user can visit a route. CanActivateChild Checks to see if a user can visit a routes children. CanDeactivate Checks to see if a user can exit a route. Resolve Performs route data retrieval before route activation. CanLoad Checks to see if a user can…

Top 10 Javascript interview questions : Concepts every javascript developer should know
interview questions , Javascript / August 20, 2018

1  Explain let,var and const variable declaration and explain their scope .what is strict mode? 2  What are callbacks and callback hell in javascript . What are higher order functions? 3 What is  hositing in javascript ? 4 What are prototypes and explain Prototypal inhertiance ? 5 What are Closures in javascript? 6 What are arrow functions / fat arrow functions  in Javascript? 7 What doe this keyword indicate in javascript? 8 What are promises in JavaScript? 9 What is bubbling and propogation in Javascript? 10 What are generators in Javascript? ANSWERS 1  The scope of var is the current execution context.That is, scope of var is global,if used outside a function and the scope is the function block if used inside the function. See more here  Understanding variable scope(let,var,const),hoisting in Javascript and Temporal Dead Zone(TDZ) 2  In  most programming languages , functions can accept other function(s) as arguments or return a function .Such functions are called Higher order functions.In JavaScript most APIs and Modules can accept function as an argument to a function .The functions are called  callbacks.see more here Understanding Callbacks and Callback hell in JavaScript : asynchronous behaviour of Javascript Part -1 3 To hoist means to pull or move up . Javascript hoisting means variable and function declarations are pulled to the top of the scope while…

Join two tables and get count in mysql:Example with hasMany relations in sequelize

Consider we have two tables ‘users‘ and ‘comments‘.Each user can have multiple comments so that ‘users‘ and ‘comments‘  have ‘hasMany‘ relation. The foreign key is user_id in the comments table. The structure of both tables are as follows Users id    | name |    email 1    userone 2    usertwo Comments id    | comment        | user_id 1      commentone      1 2      commenttwo      1 3      commentthree    2 4      commentfour     2 5      commentfive      2 6      commentfive      2 As you can see, the user with id 1 has two comments and the user with id 2 has  4 comments. To get all users with their respective number of comments, we can use the following query.

The result will be 1 userone 2 2 usertwo 4 Sequelize Example   To learn more about sequelize, see the following article For our example, we need to create two model files usersModel.js


  hasMany relation Now we need to define hasMany relations from users to comments, in your db.js file….

Async/await in JavaScript : asynchronous behaviour of Javascript Part -4

For part 1,part 2 and part 3 of this series , see the following links In the previous three articles in this series,we learned about doing asynchronous operations with callbacks,promises and generator functions.In ES7 ,Javascript introduces a new kind of syntax called async/await functions which makes it more easier to do asynchronous operations. Async/await syntax is actulally just syntactic sugar over promises and callbacks The following example shows the basic syntax for performing async/await operations.Here too,we use fetch function which returns a promise.Remember that this block of code is run on chrome 60.Earlier versions of chrome does not support async await. the fetch()  and json() returns promise

Now using async/await

The fetch  here returns a promise so we await for fetch .The json() function also returns a promise so we await for that too.Here we can see that using the async/await made the code more easier to read and understand. As you can see that  async/await helps you to write code in a way that  asynchronous code look and behave a  more like synchronous code.On each await keyword, the function pauses as you can see in the below example

  Async/await is just syntactic sugar over promises and Generators.We…

Generator Functions in JavaScript : asynchronous behaviour of Javascript Part -3

For part 1 and part 2 of this series , see the following links   In ES6 version , a new kind of functions called generator functions were introduced.These special functions helps you to manage asynchronous operations with more ease .Generator functions can be paused and then continued.Generator functions are declared with an asterisk (*) and has an yield keyword which yields data. See the following example to see the proper syntax for using a Generator functions.

    You can see the next method in the generator function prototype.To get the value of the first yield we use the next() method.

  Here you can see that the response object has a key value which gives the value of the yield and a done key which represents the status.Here in our example, value will be 1 and done will be false. We have two yields in the generator function .next() gives false two times and a third time gives done as true.If you add a return to the generator function next() will have key done as true.

  Now, if a return is done before an yield

Here you an see that the second yield will…

Understanding Promises in JavaScript : asynchronous behaviour of Javascript Part -2

In the previous article we learned about using callback for processing asynchronous requests.In ES06 Javascript introduced something called Promises which helps you deal with asynchronous operations in a better way The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value. Promises provide a better way for managing asynchronous operations when compared to traditional callback-based way. Promises represent data that may not be available but will be available later upon completing some asynchronous operation.A promise has 3 states PENDING RESOLVED REJECTED Immediately that state of a Promise is pending.Resolved state means the asynchronous operation is fulfilled.Rejected means that the asynchronous operation was not completed and have some errors. Many Javascript native apis for asynchronous operations support promises since ES06 .Prior to that many libraries including JQuery provided us promises for doing asynchronous operations. Example for promises Javscript fetch operation returns a promise.

You can see that immediately the state of promise is pending.Now for resolving the promise and getting the output Promise.prototype.then() and Promise.prototype.catch() Promise.prototype.then() and Promise.prototype.catch() The promise object has a then method which returns the promise and a catch method which catches the error.See the following set of examples,first on promise…

Understanding Callbacks and Callback hell in JavaScript : asynchronous behaviour of Javascript Part -1

In  most programming languages , functions can accept other function(s) as arguments or return a function .Such functions are called Higher order functions. In JavaScript most APIs and Modules can accept function as an argument to a function .The functions are called  callbacks. Structure of callbacks

Note that JavaScript functions behave as objects.We can set a function as an object property or store a function into a variable. JavaScript is a non-blocking programming language.It doesn’t wait or block  an operation to finish but keeps on  executing.For example consider the case when data is fetched asynchronously,then javascript doesn’t wait for the asynchronous call to get data but  keeps executing the next line of code.So in order to do something with the data we can use callbacks.See the below example in which we use a setimeout to wait for the data just like an XHR request which may take some time to complete

Asynchronous callback with  Jquery and a callback function

  CALLBACK HELL Consider the above example in which we use a callback function which is passed as an argument to the $.get function in Jquery. Now what happens when we want to send subsequent requests is that the code becomes unreadable…

Prototypes and Prototypal inheritance in Javascript

The word prototype means an early model or a primary version of a machine or a product.All further version created from that prototype will inherit the properties of that prototype.Prototype-based programming is a style of object-oriented programming in which behaviour reuse is performed via a process of reusing existing objects via delegation that serve as prototypes.(Wikipedia). Nearly all objects in JavaScript are instances of Object which sits on the top of a prototype chain.The Object.prototype is on the top of the prototype inheritance chain. That means almost all the objects in JavaScript inherit the properties and methods from Object.prototype.JavaScript Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Prototype determines the behaviour of a data object.Properties including methods are inherited from the prototype.See the below array which inherits prototypes from Array.prototype.You can see is array,map,reduce etc which are inherited from Array.prototype Prototypal inheritance In Javascript a function is a first-class Object.All JavaScript objects inherit properties and methods from a prototype.

In the example above, function Person() is called object constructor function.Wth the new keyword,we can create new objects fusing the constructor function.

both myGo and myGoGo has properties first,last,age,eyecolor and  prototype We cannot add a new property to an Person directly.we can add it to its prototype.

Share this page in social media platforms