Expirations
You might be wondering, what if I know my data doesn't change every second, but does tend to change every few minutes? What if I want to make sure I'm letting the cached data update periodically? Well, you're in luck, there are a bunch of things we can do. First, lets start with the most basic option. We can set our data to expire after a specified number of seconds. Like most things in Redis, this is incredibly simple.
We simply call expire
on our key and pass in an argument specifying the number of seconds.
Note: ttl
is the command for "time to live" and lets us know how much longer this key's value has before it is set to nil.
As you can see, we told Redis to let the value in key expire after 10 seconds. I was able to get a value from key during that 10 seconds, but afterwards, I got nil as my value.