Book Image

Redis 4.x Cookbook

Book Image

Redis 4.x Cookbook

Overview of this book

Redis is considered the world's most popular key-value store database. Its versatility and the wide variety of use cases it enables have made it a popular choice of database for many enterprises. Based on the latest version of Redis, this book provides both step-by-step recipes and relevant the background information required to utilize its features to the fullest. It covers everything from a basic understanding of Redis data types to advanced aspects of Redis high availability, clustering, administration, and troubleshooting. This book will be your great companion to master all aspects of Redis. The book starts off by installing and configuring Redis for you to get started with ease. Moving on, all the data types and features of Redis are introduced in detail. Next, you will learn how to develop applications with Redis in Java, Python, and the Spring Boot web framework. You will also learn replication tasks, which will help you to troubleshoot replication issues. Furthermore, you will learn the steps that need to be undertaken to ensure high availability on your cluster and during production deployment. Toward the end of the book, you will learn the topmost tasks that will help you to troubleshoot your ecosystem efficiently, along with extending Redis by using different modules.
Table of Contents (21 chapters)
Title Page
Dedication
Packt Upsell
Foreword
Contributors
Preface
13
Windows Environment Setup
Index

Setting expiration on keys


In the recipe Managing keys in Chapter 2,Data Types, we learned that Redis keys can be deleted by using the DEL or UNLINK command. Besides deleting keys manually, we can also ask Redis to delete keys automatically by setting the timeout on keys. In this recipe, we will illustrate how to set the timeout on Redis keys and explain the key expiration mechanism in Redis.

Getting ready…

You need to finish the installation of the Redis Server as we described in the Downloading and Installing Redis recipe in Chapter 1, Getting Started with Redis.

How to do it...

To show how to set expiration on keys, we store the IDs of five restaurants that are closest to the user's current location in a Redis list. As the user's location may change frequently, we should set a timeout on this Redis list. After the timeout has expired, we will fetch the restaurant list again using the current location:

  1. Create a Redis list of restaurant IDs at key closest_restaurant_ids:
127.0.0.1:6379> LPUSH...