The Easiest Way to Import Yahoo! Mail into Gmail

Have you decided to finally switch one of your Yahoo! emails over to Gmail? Here’s a quick guide on the most painless way. *All conclusions/solutions in this article are subject to change, as Yahoo…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Supercharging NodeJS with Redis

We want our server side to have super-heroic performance. More than often, database transactions or third party network requests can be bottlenecks, but they are important resources. We can’t just stop using them. The solution must be a way to cache frequent costly transactions in the primary memory. Here comes Redis, the in-memory data structure store.

We can use redis to store a plethora of data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. We can store data, structured as any of the above in the RAM, and then use it as a in memory database which will give a significant performance boost.

We will now see, how we can use redis with nodejs. I wont be going over how to install redis on different platforms, i think a quick googling would be enough to get that information.

We will see how to store and fetch different data structures one by one.

Install it in your nodejs project.

By default, redis.createClient() will use 127.0.0.1 and 6379 as the hostname and port respectively. If you have a different host/port you can supply them as following:

Now, you can listen for the connect event, for when the connection is completed.

We can store simple strings with

Or

We can also pass a callback to check the completion.

If there is some error, then err will have what happened.

To retrieve a value.

reply will have whatever we have stored in there. If the key does not exist reply will be empty.

Many times storing simple values won’t solve your problem. Hash maps are a common representation on objects. For that you can use hmset() function as following:

The above snippet stores a hash in Redis that maps each food type to a food. The first argument to hmset() is the name of the key. Subsequent arguments represent key-value pairs. Similarly, hgetall() is used to retrieve the value of the key. If the key is found, the second argument to the callback will contain the value which is an object.

We can also use the following syntax.

All the properties have to be strings, Redis does not support nested objects.

If you want to store a list of items, you can use Redis lists. To store a list use the following syntax:

The above snippet creates a list called foods and pushes two elements to it. So, the length of the list is now two. As you can see I have passed an argsarray to rpush. The first item of the array represents the name of the key while the rest represent the elements of the list. You can also use lpush()instead of rpush() to push the elements to the left.

To retrieve the elements of the list you can use the lrange() function as following:

Just note that you get all the elements of the list by passing -1 as the third argument to lrange(). If we want just a range we can set set the third argument to the end index.

Sets are similar to lists, but the difference is that they don’t allow duplicates. So, if you don’t want any duplicate elements in your list you can use a set. Here is how we can modify our previous snippet to use a set instead of list.

As you can see, the sadd() function creates a new set with the specified elements. Here, the length of the set is three, so during setting the value the reply is 3. To retrieve the members of the set, use the smembers() function as following:

Do note that the order is not preserved while fetching the set.

Don’t cache everything! You simple don’t have unlimited RAM, so go easy on it. Choose only the resources that are frequently accessed, and not every request or transaction that is made.

Keep building better stuff!! And always remember, performance is the way to the users hearts.

If you loved the content, go on hit some claps. ❤

Add a comment

Related posts:

The Little Things That Make Us Content

Grandpa made beef stew today! Grandpa has been an expert beef stew maker for more than 40 years. Grandpa started making beef stew when he took over the cooking torch from Grandma. Grandma cooked…

The Underwater Cloud

Out world seems to be getting smaller day by day. We can communicate with someone on the other side of the world in a matter of milliseconds. This data transfer is made possible not through air…

Sigue siendo Inconcebible

La semana pasada Ani me pidió que viera con ella una serie y la verdad es que en el primer capítulo aborté la misión, ver escenas de abuso sexual que además están dramatizadas para la pantalla no era…