| Start |
|---|
| Getting Started node_redis, as you may have guessed, is the Redis client for Node.js (opens new window). You can install it via npm using the following command. Once you have installed node_redis module you are good to go. Let’s create a simple file, app.js, and see how to connect with Redis from Node.js. app.js 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 perform some action once a connection has been established. Basically, you just need to listen for connect events as shown below. So, the following snippet goes into app.js: Now, type node app in the terminal to run the app. Make sure your Redis server is up and running before running this snippet. |
| Storing Key-Value Pairs Now that you know how to connect with Redis from Node.js, let’s see how to store key-value pairs in Redis storage. Storing Strings All the Redis commands are exposed as different functions on the client object. To store a simple string use the following syntax: Or The above snippets store a simple string AngularJS against the key framework. You should note that both the snippets do the same thing. The only difference is that the first one passes a variable number of arguments while the later passes an args array to If the operation failed for some reason, the
Storing Hash Many times storing simple values won’t solve your problem. You will need to store hashes (objects) in Redis. For that you can use The above snippet stores a hash in Redis that maps each technology to its framework. The first argument to Note that Redis doesn’t support nested objects. All the property values in the object will be coerced into strings before getting stored. You can also use the following syntax to store objects in Redis: An optional callback can also be passed to know when the operation is completed. All the functions (commands) can be called with uppercase/lowercase equivalents. For example, 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 frameworks and pushes two elements to it. So, the length of the list is now two. As you can see I have passed an To retrieve the elements of the list you can use the Just note that you get all the elements of the list by passing -1 as the third argument to Storing Sets 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 This snippet will retrieve all the members of the set. Just note that the order is not preserved while retrieving the members. This was a list of the most important data structures found in every Redis powered app. Apart from strings, lists, sets, and hashes, you can store sorted sets, hyperLogLogs, and more in Redis. If you want a complete list of commands and data structures, visit the official Redis documentation. Remember that almost every Redis command is exposed on the client object offered by the node_redis module. |
| Some more important operations supported by node_redis. Checking the Existence of Keys Sometimes you may need to check if a key already exists and proceed accordingly. To do so you can use Deleting and Expiring Keys At times you will need to clear some keys and reinitialize them. To clear the keys, you can use del command as shown below: You can also give an expiration time to an existing key as following: The above snippet assigns an expiration time of 30 seconds to the key key1. Incrementing and Decrementing Redis also supports incrementing and decrementing keys. To increment a key use The RemarksWe have covered the basic and most commonly used operations in node_redis. You can use this module to leverage the full power of Redis and create really sophisticated Node.js apps. You can build many interesting things with this library such as a strong caching layer, a powerful Pub/Sub messaging system and more. To know more about the library check out their documentation (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference