{"id":26152,"date":"2022-06-23T12:00:41","date_gmt":"2022-06-23T02:00:41","guid":{"rendered":"https:\/\/database.guide\/?p=26152"},"modified":"2022-06-27T11:58:18","modified_gmt":"2022-06-27T01:58:18","slug":"redis-pexpire-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-pexpire-command-explained\/","title":{"rendered":"Redis PEXPIRE Command Explained"},"content":{"rendered":"\n<p>The Redis <code>PEXPIRE<\/code> command sets a timeout on a given key in milliseconds. After the timeout has expired, the key will be deleted.<\/p>\n\n\n\n<p>The <code>PEXPIRE<\/code> command works exactly the same as the <code><a href=\"https:\/\/database.guide\/redis-expire-command-explained\/\" data-type=\"post\" data-id=\"26111\">EXPIRE<\/a><\/code> command, except that it returns the timeout in milliseconds instead of seconds.<\/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>PEXPIRE key milliseconds &#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>milliseconds<\/code><\/td><td>The number of milliseconds 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 breakfast \"Muesli\"<\/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 PTTL command against it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL breakfast<\/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>The <code><a href=\"https:\/\/database.guide\/redis-pttl-command-explained\/\" data-type=\"post\" data-id=\"26158\">PTTL<\/a><\/code> command returns a key&#8217;s time to live in milliseconds. We can alternatively use the <code><a href=\"https:\/\/database.guide\/redis-ttl-command-explained\/\" data-type=\"post\" data-id=\"26179\">TTL<\/a><\/code> command to return the time to live in seconds.<\/p>\n\n\n\n<p>Now let&#8217;s use <code>PEXPIRE<\/code> to set an expiry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PEXPIRE breakfast 86400000<\/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 got an integer reply of <code>1<\/code>, which means it was successful. <\/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>PTTL breakfast<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 86351862<\/pre>\n\n\n\n<p>We still have plenty of time left before it expires.<\/p>\n\n\n\n<p>We can also use the <code><a href=\"https:\/\/database.guide\/redis-pexpiretime-command-explained\/\" data-type=\"post\" data-id=\"26137\">PEXPIRETIME<\/a><\/code> command to get the absolute Unix time of its expiry. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PEXPIRETIME breakfast<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1655978136926<\/pre>\n\n\n\n<p>The <code><a href=\"https:\/\/database.guide\/redis-expiretime-command-explained\/\" data-type=\"post\" data-id=\"26130\">EXPIRETIME<\/a><\/code> command does the same thing, but returns the result in seconds.<\/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>PEXPIRE breakfast 100000000 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 breakfast<\/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>PEXPIRE breakfast 100000000 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>PTTL breakfast<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 99988589<\/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>PEXPIRE breakfast 500000000 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 use <code>PTTL<\/code> to check the current time to live:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL breakfast<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 499969158<\/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>PEXPIRE breakfast 400000000 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>PEXPIRE breakfast 500000000 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 the same as the 500000000 that I had previously used to set the timeout. However, time has elapsed since then, and the time to live has therefore decreased. Therefore, my new value happens 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>PEXPIRE breakfast 600000000 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>PEXPIRE breakfast 400000000 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 PEXPIRE command sets a timeout on a given key in milliseconds. After the timeout has expired, the key will be deleted. The PEXPIRE command works exactly the same as the EXPIRE command, except that it returns the timeout in milliseconds instead of seconds.<\/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-26152","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\/26152","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=26152"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26152\/revisions"}],"predecessor-version":[{"id":26262,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26152\/revisions\/26262"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}