{"id":26111,"date":"2022-06-23T12:00:36","date_gmt":"2022-06-23T02:00:36","guid":{"rendered":"https:\/\/database.guide\/?p=26111"},"modified":"2022-06-27T11:58:25","modified_gmt":"2022-06-27T01:58:25","slug":"redis-expire-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-expire-command-explained\/","title":{"rendered":"Redis EXPIRE Command Explained"},"content":{"rendered":"\n<p>The Redis <code>EXPIRE<\/code> command sets a timeout on a given key in seconds. After the timeout has expired, the key will be deleted.<\/p>\n\n\n\n<p>Redis also has a <code><a href=\"https:\/\/database.guide\/redis-pexpire-command-explained\/\" data-type=\"post\" data-id=\"26152\">PEXPIRE<\/a><\/code> command that works the same as <code>EXPIRE<\/code>, except that it returns the timeout in milliseconds instead of seconds.<\/p>\n\n\n\n<p>A key with a timeout is said to be <em>volatile<\/em> in Redis terminology.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE key seconds &#91; NX | XX | GT | LT]<\/code><\/pre>\n\n\n\n<p>Explanation of arguments:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>key<\/code><\/td><td>The key for which to set the timeout.<\/td><\/tr><tr><td><code>seconds<\/code><\/td><td>The number of seconds for the timeout.<\/td><\/tr><tr><td><code>NX<\/code><\/td><td>Sets an expiry only when the key has no expiry.<\/td><\/tr><tr><td><code>XX<\/code><\/td><td>Sets an expiry only when the key has an existing expiry.<\/td><\/tr><tr><td><code>GT<\/code><\/td><td>Sets an expiry only when the new expiry is greater than current one.<\/td><\/tr><tr><td><code>LT<\/code><\/td><td>Sets an expiry only when the new expiry is less than current one.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The <code>NX<\/code>, <code>XX<\/code>, <code>GT<\/code>, and <code>LT<\/code> arguments were introduced in Redis 7.0.0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we set a key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET lunch \"Cordon Bleu\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>We didn&#8217;t set an expiry for this key, so when I run the TTL command against it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TTL lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) -1<\/pre>\n\n\n\n<p>We get an integer reply of <code>-1<\/code> (which means it has no expiry).<\/p>\n\n\n\n<p>Now let&#8217;s use <code>EXPIRE<\/code> to set an expiry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE lunch 86400<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>I got an integer reply of <code>1<\/code>, which means it was successful. Here, I set the expiry to 86400 seconds, which is a day. This means that if I want to use the key, I&#8217;ll need to do so within 86400 seconds of its expiry being set.<\/p>\n\n\n\n<p>Let&#8217;s check its time to live:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TTL lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 86375<\/pre>\n\n\n\n<p>So we still have plenty of time left.<\/p>\n\n\n\n<p>We can also use the <code><a href=\"https:\/\/database.guide\/redis-expiretime-command-explained\/\" data-type=\"post\" data-id=\"26130\">EXPIRETIME<\/a><\/code> command to get the absolute Unix time of its expiry. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRETIME lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1655978393<\/pre>\n\n\n\n<p>The <code><a href=\"https:\/\/database.guide\/redis-pexpiretime-command-explained\/\" data-type=\"post\" data-id=\"26137\">PEXPIRETIME<\/a><\/code> command does the same thing, but returns the result in milliseconds.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>NX<\/code> Argument<\/h2>\n\n\n\n<p>The <code>NX<\/code> argument was introduced in Redis 7.0.0. <\/p>\n\n\n\n<p>We can use the <code>NX<\/code> argument to set the timeout only if the key doesn&#8217;t already have an expiry. <\/p>\n\n\n\n<p>To demonstrate this, let&#8217;s first try to set the expiry on the key that we used in the previous example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE lunch 100000 NX<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 0<\/pre>\n\n\n\n<p>It doesn&#8217;t work. We get an integer reply of <code>0<\/code>, which means that the timeout wasn&#8217;t set.<\/p>\n\n\n\n<p>Let&#8217;s remove the expiry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PERSIST lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>And try again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE lunch 100000 NX<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>This time we were successful.<\/p>\n\n\n\n<p>Let&#8217;s check the time to live:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TTL lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 99978<\/pre>\n\n\n\n<p>Yes, it worked as expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>XX<\/code> Argument<\/h2>\n\n\n\n<p>The <code>XX<\/code> argument was introduced in Redis 7.0.0. <\/p>\n\n\n\n<p>We can use the <code>XX<\/code> argument to set the timeout only if the key already has an expiry.<\/p>\n\n\n\n<p>Let&#8217;s try updating the same key from the previous examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE lunch 500000 XX<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>Looks like it worked. <\/p>\n\n\n\n<p>Let&#8217;s check the current TTL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TTL lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 499977<\/pre>\n\n\n\n<p>Looks good.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>GT<\/code> Argument<\/h2>\n\n\n\n<p>The <code>GT<\/code> argument was introduced in Redis 7.0.0. <\/p>\n\n\n\n<p>We can use the <code>GT<\/code> argument to set the timeout only if the new expiry is greater than current one.<\/p>\n\n\n\n<p>To test this, let&#8217;s try updating our key with a smaller timeout:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE lunch 400000 GT<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 0<\/pre>\n\n\n\n<p>It didn&#8217;t work, because the new expiry is smaller than the current one.<\/p>\n\n\n\n<p>Let&#8217;s try again, with a larger expiry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE lunch 499818 GT<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>This time it worked, because our expiry is greater than the current one.<\/p>\n\n\n\n<p>Note that my new expiry is smaller than the 500000 that I had previously used to set the timeout. However, time has elapsed since then, and the time to live has continued to decrease. My new value happened to be higher than the current time to live.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>LT<\/code> Argument<\/h2>\n\n\n\n<p>The <code>LT<\/code> argument was introduced in Redis 7.0.0. <\/p>\n\n\n\n<p>We can use the <code>LT<\/code> argument to set the timeout only if the new expiry is less than current one.<\/p>\n\n\n\n<p>To test this, let&#8217;s try updating our key with a larger timeout:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE lunch 500000 LT<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 0<\/pre>\n\n\n\n<p>It didn&#8217;t work, because the new expiry is larger than the current one.<\/p>\n\n\n\n<p>Let&#8217;s try again, with a smaller expiry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRE lunch 400000 LT<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>This time it worked, because our expiry is less than the current one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Redis EXPIRE command sets a timeout on a given key in seconds. After the timeout has expired, the key will be deleted. Redis also has a PEXPIRE command that works the same as EXPIRE, except that it returns the timeout in milliseconds instead of seconds. A key with a timeout is said to be &#8230; <a title=\"Redis EXPIRE Command Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/redis-expire-command-explained\/\" aria-label=\"Read more about Redis EXPIRE Command Explained\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,20],"class_list":["post-26111","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=26111"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26111\/revisions"}],"predecessor-version":[{"id":26232,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26111\/revisions\/26232"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}