{"id":26149,"date":"2022-06-26T11:50:29","date_gmt":"2022-06-26T01:50:29","guid":{"rendered":"https:\/\/database.guide\/?p=26149"},"modified":"2022-06-27T11:55:33","modified_gmt":"2022-06-27T01:55:33","slug":"redis-persist-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-persist-command-explained\/","title":{"rendered":"Redis PERSIST Command Explained"},"content":{"rendered":"\n<p>The Redis <code>PERSIST<\/code> command removes any existing timeout on a given key. It returns an integer reply of <code>1<\/code> if the timeout was removed, or <code>0<\/code> if the key doesn&#8217;t exist or doesn&#8217;t have an associated timeout.<\/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>PERSIST key<\/code><\/pre>\n\n\n\n<p>Where <code>key<\/code> is the key for which to remove the expiry.<\/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 lunch \"Salad\" EX 86400<\/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>EX<\/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 seconds. <\/p>\n\n\n\n<p>Now let&#8217;s check the key&#8217;s 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) 86349<\/pre>\n\n\n\n<p>There is still plenty of time before this key expires.<\/p>\n\n\n\n<p>In Redis 7.0.0 and higher, 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 check the absolute Unix time that it will expire:<\/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) 1655961287<\/pre>\n\n\n\n<p>OK, now let&#8217;s use the <code>PERSIST<\/code> command to remove the expiry associated with the key:<\/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>We get an integer reply of <code>1<\/code>, which means the expiry was removed.<\/p>\n\n\n\n<p>Now when we use the <code>TTL<\/code> command, we get <code>-1<\/code>:<\/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>This means that the key exists, but doesn&#8217;t have an expiry.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Key Has No Expiry<\/h2>\n\n\n\n<p>If the key has no expiry, <code>PERSIST<\/code> returns <code>0<\/code>.<\/p>\n\n\n\n<p>To test this, let&#8217;s run <code>PERSIST<\/code> against the same key, now that we&#8217;ve removed its timeout:<\/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) 0<\/pre>\n\n\n\n<p>As expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Key Doesn&#8217;t Exist<\/h2>\n\n\n\n<p>If the key doesn&#8217;t exist, <code>PERSIST<\/code> returns <code>0<\/code>. To test this, let&#8217;s delete the key, and then run <code>PERSIST<\/code> against it.<\/p>\n\n\n\n<p>Delete the key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEL 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>This means that the key was deleted.<\/p>\n\n\n\n<p>Now let&#8217;s run <code>PERSIST<\/code> against it:<\/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) 0<\/pre>\n\n\n\n<p>As expected, we get an integer reply of <code>0<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Redis PERSIST command removes any existing timeout on a given key. It returns an integer reply of 1 if the timeout was removed, or 0 if the key doesn&#8217;t exist or doesn&#8217;t have an associated timeout.<\/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-26149","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\/26149","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=26149"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26149\/revisions"}],"predecessor-version":[{"id":26228,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26149\/revisions\/26228"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}