Execute List Operations with Node.js

Learn how to execute list commands using the redis npm package, and develop an API utilizing Redis caching.

We created a simple API that performs Redis operations on string data. Let’s now discuss lists and create a simple API using lists in Redis. We’ll create an API that tracks the user's search terms. Let's say that we have a search engine and must keep track of the five latest search terms and display them. To keep things simpler, we’re only going to create an API that accepts the search term and returns the five recent search terms using Redis. We’ll follow the steps mentioned below in order to implement the functionality:

  1. We fetch the list of terms that are searched by a user from Redis by the key name searchQuery.

  2. If there are five or more search terms in the list, then we remove the search terms from the left because we want to maintain the recent search terms. In our list, the first search term is stored at the first position of the list, the second search term at the second position, and so on. So when the sixth search term appears, we remove the search term from the first position (leftmost element in the list). After removing the leftmost element, we insert the new search term at the rightmost position of the list and return the response with a message.

  3. If there are fewer than five search terms, we just insert the new search term at the rightmost position of the list and return the response with a message.

Implement the function to get recently searched terms

We’ll implement a function that returns the five latest search terms. Before that, let’s discuss the Redis functions that we’re going to use to build the API:

  • lRange(): This function is used to execute the LRANGE command. The function accepts three parameters: the key name denoting the list, the start index from where to fetch the elements, and the end index up to where to fetch the elements from the list. This function returns all the elements, inclusive of the start and end indexes, from the list.

  • lPop(): This function is used to execute the LPOP command. The function accepts one parameter, which is the key name denoting the list. This function removes the element from the leftmost position of the list.

  • rPush(): This function is used to execute the RPUSH command. The function accepts two parameters: the key name of the list and either a single element or a list of elements to be inserted into the list. Remember that the insertion happens from the right side of the list.

  • rPop() and lPush(): We’re not going to use these functions, but we should discuss them because we’ve already discussed the corresponding commands.

    • The rPop() function is used to execute the RPOP command. The function accepts one parameter, which is the key name denoting the list. This function removes the element from the rightmost position of the list.

    • The lPush() function is used to execute the LPUSH command. The function accepts two parameters: the key name of the list and either a single element or a list of elements to be inserted into the list. Remember that the insertion happens from the left side of the list.

Let’s now move to the code.

Get hands-on with 1200+ tech skills courses.