{"id":26158,"date":"2022-06-26T11:46:30","date_gmt":"2022-06-26T01:46:30","guid":{"rendered":"https:\/\/database.guide\/?p=26158"},"modified":"2022-06-27T11:55:43","modified_gmt":"2022-06-27T01:55:43","slug":"redis-pttl-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-pttl-command-explained\/","title":{"rendered":"Redis PTTL Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>PTTL<\/code> command returns the remaining time to live of a given key, in milliseconds. <\/p>\n\n\n\n<p>If the key doesn&#8217;t have a timeout, an integer reply of <code>-1<\/code> is returned. If the key doesn&#8217;t exist, <code>-2<\/code> is returned.<\/p>\n\n\n\n<p><code>PTTL<\/code> works the same as the <code><a href=\"https:\/\/database.guide\/redis-ttl-command-explained\/\" data-type=\"post\" data-id=\"26179\">TTL<\/a><\/code> command, except that it returns the result 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>PTTL key<\/code><\/pre>\n\n\n\n<p>Where <code><em>key<\/em><\/code> is the key for which to get the time to live.<\/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 beverage \"Water\" 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. <\/p>\n\n\n\n<p>We can now use the <code>PTTL<\/code> command to check the key&#8217;s time to live:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL beverage<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 86376856<\/pre>\n\n\n\n<p>We can see that the time to live is slightly lower than the timeout that we set. That&#8217;s because <code>PTTL<\/code> returns the current time to live. Therefore, if we wait a minute or so and then run the command again, the time to live has decreased further:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL beverage<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 86330093<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Volatile Keys<\/h2>\n\n\n\n<p>In Redis, a key with a timeout is considered <em>volatile<\/em>. A key without a timeout is considered <em>non-volatile<\/em>. When we run the <code>PTTL<\/code> command against a non-volatile key (i.e. a key without a timeout), we get an integer reply of <code>-1<\/code>.<\/p>\n\n\n\n<p>To test this, let&#8217;s remove the time to live from our key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PERSIST beverage<\/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>Now let&#8217;s run the <code>PTTL<\/code> command against that key again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL beverage<\/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 <code>-1<\/code> as expected.<\/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, an integer reply of <code>-2<\/code> is returned (although in Redis 2.6 and below, it returns <code>-1<\/code>).<\/p>\n\n\n\n<p>To test this, let&#8217;s delete the key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEL beverage<\/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 now let&#8217;s try to run <code>PTTL<\/code> against it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL beverage<\/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, we get an integer reply of <code>-2<\/code>.<\/p>\n\n\n\n<p>As mentioned, in Redis 2.6 and below, it returns <code>-1<\/code> in such cases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the PTTL command returns the remaining time to live of a given key, in milliseconds. If the key doesn&#8217;t have a timeout, an integer reply of -1 is returned. If the key doesn&#8217;t exist, -2 is returned. PTTL works the same as the TTL command, except that it returns the result in milliseconds &#8230; <a title=\"Redis PTTL Command Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/redis-pttl-command-explained\/\" aria-label=\"Read more about Redis PTTL 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-26158","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\/26158","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=26158"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26158\/revisions"}],"predecessor-version":[{"id":26256,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26158\/revisions\/26256"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}