{"id":25858,"date":"2022-06-21T11:42:55","date_gmt":"2022-06-21T01:42:55","guid":{"rendered":"https:\/\/database.guide\/?p=25858"},"modified":"2022-06-21T11:42:55","modified_gmt":"2022-06-21T01:42:55","slug":"redis-psetex-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-psetex-command-explained\/","title":{"rendered":"Redis PSETEX Command Explained"},"content":{"rendered":"\n<p>The Redis <code>PSETEX<\/code> command sets a key to hold a given string value, and sets that key to time out after a given number of milliseconds.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Note:<\/strong> The <code><a href=\"https:\/\/database.guide\/redis-set-command-explained\/\" data-type=\"post\" data-id=\"25799\">SET<\/a><\/code> command can do the same thing, and it&#8217;s possible that the <code>PSETEX<\/code> command may be deprecated at some point. Therefore, it&#8217;s probably a good idea to use <code>SET<\/code> instead of <code>PSETEX<\/code> if possible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax for <code>PSETEX<\/code> goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PSETEX key milliseconds value<\/code><\/pre>\n\n\n\n<p>Where <code>key<\/code> is the key to set, <code>milliseconds<\/code> is the number of milliseconds before it expires, and <code>value<\/code> is the value to set.<\/p>\n\n\n\n<p>It&#8217;s the equivalent of the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET mykey value\nEXPIRE mykey milliseconds<\/code><\/pre>\n\n\n\n<p>Which is basically the same as doing this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET mykey value PX milliseconds<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s a simple example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PSETEX lunch 30000 \"Red Curry\"<\/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>Now, if I&#8217;m quick and use <code>GET<\/code> to return the value before the 30000 milliseconds is up, I can see the value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Red Curry\"<\/pre>\n\n\n\n<p>But if I wait until after the 30000 milliseconds is up, I get a null result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(nil)<\/pre>\n\n\n\n<p>As mentioned, we can do the same thing with the <code>SET<\/code> command, so it&#8217;s probably better to use that in case <code>PSETEX<\/code> is deprecated at some point.<\/p>\n\n\n\n<p>There&#8217;s also a <code>SETEX<\/code> command that does the same thing as <code>PSETEX<\/code>, except that <code>SETEX<\/code> uses seconds.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Redis PSETEX command sets a key to hold a given string value, and sets that key to time out after a given number of milliseconds.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,84,20],"class_list":["post-25858","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-string","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25858","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=25858"}],"version-history":[{"count":2,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25858\/revisions"}],"predecessor-version":[{"id":25860,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25858\/revisions\/25860"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=25858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=25858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=25858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}