Welcome back! In this lesson, we will explore a crucial feature of Redis: key expiration. This topic builds on our Redis knowledge and adds another tool to our kit for managing data efficiently in high-performance applications.
You will learn how to set expiration times on your Redis keys. This is useful for many situations, such as caching data, managing session lifetimes, or any scenario where you want data to automatically expire after a certain period. We will learn how to set expiration times on keys and check the remaining time-to-live (TTL
) for a key.
Here's a quick preview of what you will be doing:
To set a key with an expiration time, you can use the set
method with the EX
parameter:
JavaScript1import { createClient } from 'redis'; 2 3const client = createClient(); 4 5const key = 'session:12345'; 6await client.connect(); 7await client.set(key, 'data', { EX: 2 }); 8 9const ttl = await client.ttl(key); 10console.log(`Time-to-live for session key: ${ttl} seconds`); 11 12setTimeout(async () => { 13 const value = await client.get(key); 14 console.log(`Value: ${value}`); // null 15 client.disconnect(); 16}, 3000); 17
The above code snippet shows how to set a key (session:12345
) with a value (data
) that expires after 2 seconds.
To check the remaining time-to-live (TTL
) for a key, you can use the ttl
method with the key name as the parameter.
After waiting for the expiration time, you can verify that the key no longer exists. This code waits 3 seconds and then attempts to get the value of the key, which should return null
because the key has expired.
Another useful method is expire
, which allows you to set the expiration time for a key after it has been created:
JavaScript1await client.set(key, 'data'); 2await client.expire(key, 2); 3 4const ttl = await client.ttl(key); 5console.log(`Time-to-live for session key after setting expire: ${ttl} seconds`);
This code snippet sets the key session:12345
with a value of data
and then sets the expiration time to 2 seconds. We will explore this method in more detail in the practice section.
Key expiration is an essential feature for managing limited memory resources efficiently. By setting expiration times, you can ensure that outdated data is removed automatically without manual intervention. This can significantly improve your application's performance and reliability.
By mastering key expiration, you can build more intelligent caching mechanisms, manage user sessions effectively, and handle temporary data seamlessly. This concept is a critical aspect of maintaining high-performance applications that need to run efficiently over time.
Exciting, right? Let's move on to the practice section and start applying these concepts hands-on.