{"id":26137,"date":"2022-06-25T11:39:33","date_gmt":"2022-06-25T01:39:33","guid":{"rendered":"https:\/\/database.guide\/?p=26137"},"modified":"2022-06-27T11:57:30","modified_gmt":"2022-06-27T01:57:30","slug":"redis-pexpiretime-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-pexpiretime-command-explained\/","title":{"rendered":"Redis PEXPIRETIME Command Explained"},"content":{"rendered":"\n<p>The Redis <code>PEXPIRETIME<\/code> command returns the absolute Unix timestamp in milliseconds at which the given key will expire. This is the number of milliseconds since 00:00:00 UTC on 1 January 1970 until the expiry time of the key.<\/p>\n\n\n\n<p>This works exactly the same as <code><a href=\"https:\/\/database.guide\/redis-expiretime-command-explained\/\" data-type=\"post\" data-id=\"26130\">EXPIRETIME<\/a><\/code>, but it returns the Unix timestamp in milliseconds instead of seconds.<\/p>\n\n\n\n<p>The <code>PEXPIRETIME<\/code> command was introduced in Redis 7.0.0.<\/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>PEXPIRETIME key<\/code><\/pre>\n\n\n\n<p>Where <code><em>key<\/em><\/code> is the key for which to get the expiry time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we set a key with an expiry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET dinner \"Pasta\" PX 86400000<\/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>Here, I used the <code>PX<\/code> argument of the <code><a href=\"https:\/\/database.guide\/redis-set-command-explained\/\" data-type=\"post\" data-id=\"25799\">SET<\/a><\/code> command to set an expiry in milliseconds. I could have alternatively used the <code>EX<\/code> argument to set it in milliseconds. Either way, it wouldn&#8217;t change the results we get with <code>PEXPIRETIME<\/code> (assuming the same expiry time).<\/p>\n\n\n\n<p>Another way to do it would be with the <code><a href=\"https:\/\/database.guide\/redis-expire-command-explained\/\" data-type=\"post\" data-id=\"26111\">EXPIRE<\/a><\/code> or <code><a href=\"https:\/\/database.guide\/redis-expireat-command-explained\/\" data-type=\"post\" data-id=\"26123\">EXPIREAT<\/a><\/code> commands (for seconds), or with the <code><a href=\"https:\/\/database.guide\/redis-pexpire-command-explained\/\" data-type=\"post\" data-id=\"26152\">PEXPIRE<\/a><\/code> or <code><a href=\"https:\/\/database.guide\/redis-pexpireat-command-explained\/\" data-type=\"post\" data-id=\"26154\">PEXPIREAT<\/a><\/code> commands (for milliseconds). <\/p>\n\n\n\n<p>Anyway, we can now use the <code>PEXPIRETIME<\/code> command to check that key&#8217;s expiry time in Unix time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PEXPIRETIME dinner<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1655949518046<\/pre>\n\n\n\n<p>This is the absolute Unix timestamp of the key&#8217;s expiry. If we run the statement again at a later time, we get exactly the same result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PEXPIRETIME dinner<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1655949518046<\/pre>\n\n\n\n<p>The absolute Unix timestamp of the key&#8217;s expiry is not to be confused with the current time to live (TTL) of the key.<\/p>\n\n\n\n<p>Let&#8217;s run the <code><a href=\"https:\/\/database.guide\/redis-pttl-command-explained\/\" data-type=\"post\" data-id=\"26158\">PTTL<\/a><\/code> command against it to check its time to live in milliseconds:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL dinner<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 86324550<\/pre>\n\n\n\n<p>We get an integer reply with the current time to live (in milliseconds). If we run the same command again, we get a different result, due to the expiry getting nearer (and therefore, the time to live getting shorter):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL dinner<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 86302301<\/pre>\n\n\n\n<p>This is in contrast to the absolute Unix timestamp of the expiry. <\/p>\n\n\n\n<p>There&#8217;s also a <code><a href=\"https:\/\/database.guide\/redis-ttl-command-explained\/\" data-type=\"post\" data-id=\"26179\">TTL<\/a><\/code> command that returns the time to live in seconds.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Keys Without an Expiry<\/h2>\n\n\n\n<p>If the key has no expiry, <code>PEXPIRETIME<\/code> returns an integer reply of <code>-1<\/code>.<\/p>\n\n\n\n<p>To test this, let&#8217;s remove the expiry of our key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PERSIST dinner<\/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>The <code><a href=\"https:\/\/database.guide\/redis-persist-command-explained\/\" data-type=\"post\" data-id=\"26149\">PERSIST<\/a><\/code> command removes any timeout on the given key. We got an integer reply of <code>1<\/code>, which means that it successfully removed the timeout on our key.<\/p>\n\n\n\n<p>Now let&#8217;s run <code>PEXPIRETIME<\/code> against that key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PEXPIRETIME dinner<\/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>As expected, we get an integer reply of <code>-1<\/code>, which means that the key doesn&#8217;t have an expiry set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Keys<\/h2>\n\n\n\n<p>If the key doesn&#8217;t exist, <code>PEXPIRETIME<\/code> returns an integer reply of <code>-2<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s delete our key: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEL dinner<\/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>The key has been deleted.<\/p>\n\n\n\n<p>And now let&#8217;s try to run <code>PEXPIRETIME<\/code> against the deleted key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PEXPIRETIME dinner<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) -2<\/pre>\n\n\n\n<p>As expected, an integer reply of <code>-2<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Redis PEXPIRETIME command returns the absolute Unix timestamp in milliseconds at which the given key will expire. This is the number of milliseconds since 00:00:00 UTC on 1 January 1970 until the expiry time of the key. This works exactly the same as EXPIRETIME, but it returns the Unix timestamp in milliseconds instead of &#8230; <a title=\"Redis PEXPIRETIME Command Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/redis-pexpiretime-command-explained\/\" aria-label=\"Read more about Redis PEXPIRETIME 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-26137","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\/26137","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=26137"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26137\/revisions"}],"predecessor-version":[{"id":26260,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26137\/revisions\/26260"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}