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'll cover the following...
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:
We fetch the list of terms that are searched by a user from Redis by the key name
searchQuery
.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.
If there are fewer than five search terms, we just insert the new search term at the rightmost position of the list and ...