{"id":25799,"date":"2022-06-12T12:10:53","date_gmt":"2022-06-12T02:10:53","guid":{"rendered":"https:\/\/database.guide\/?p=25799"},"modified":"2022-06-14T16:03:02","modified_gmt":"2022-06-14T06:03:02","slug":"redis-set-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-set-command-explained\/","title":{"rendered":"Redis SET Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>SET<\/code> command sets a key to hold a given string value.<\/p>\n\n\n\n<p>If the key already holds a value, it is overwritten with the new value. Also, any previous time to live associated with the key is discarded (assuming a successful <code>SET<\/code> operation).<\/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>SET key value &#91; NX | XX] &#91;GET] &#91; EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]<\/code><\/pre>\n\n\n\n<p>Here&#8217;s an explanation of the various options:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>EX<\/code>\u00a0<em>seconds<\/em><\/td><td>Sets the specified expire time, in seconds.<\/td><\/tr><tr><td><code>PX<\/code>\u00a0<em>millisecond<\/em><\/td><td>Sets the specified expire time, in milliseconds.<\/td><\/tr><tr><td><code>EXAT<\/code>\u00a0<em>timestamp-second<\/em><\/td><td>Sets the specified Unix time at which the key will expire, in seconds.<\/td><\/tr><tr><td><code>PXAT<\/code>\u00a0<em>timestamp-milliseconds<\/em><\/td><td>Sets the specified Unix time at which the key will expire, in milliseconds.<\/td><\/tr><tr><td><code>NX<\/code><\/td><td>Only sets the key if it does not already exist.<\/td><\/tr><tr><td><code>XX<\/code><\/td><td>Only sets the key if it already exists.<\/td><\/tr><tr><td><code>KEEPTTL<\/code><\/td><td>Retains the time to live associated with the key.<\/td><\/tr><tr><td><code>GET<\/code><\/td><td>Returns the old string stored at the key, or nil if the key did not exist. An error is returned and\u00a0<code>SET<\/code>\u00a0aborted if the value stored at key is not a string.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>See the <a rel=\"noreferrer noopener\" href=\"https:\/\/redis.io\/commands\/set\/\" data-type=\"URL\" data-id=\"https:\/\/redis.io\/commands\/set\/\" target=\"_blank\">Redis documentation<\/a> for any changes to the above information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s a basic example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET type \"Dog\"<\/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>In this case, the <code>SET<\/code> operation was successful, and so we got a simple string reply of <code>OK<\/code>.<\/p>\n\n\n\n<p>We can now check the value of the <code>type<\/code> key with the <code><a href=\"https:\/\/database.guide\/redis-get-command-explained\/\" data-type=\"post\" data-id=\"25826\">GET<\/a><\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET type<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Dog\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>NX<\/code> Option<\/h2>\n\n\n\n<p>Here&#8217;s an example of trying to set another value to the same key when using the <code>NX<\/code> option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET type \"Cat\" NX<\/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>The <code>NX<\/code> option prevents the key being set if it already exists. In our case, it already exists and so the result is a null reply, or <code>(nil)<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>XX<\/code> Option<\/h2>\n\n\n\n<p>Here&#8217;s an example of using the <code>XX<\/code> option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET type \"Cat\" XX<\/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>The <code>XX<\/code> option prevents the key being set if it <em>doesn&#8217;t<\/em> already exist. In our case, it already exists and so the key is set.<\/p>\n\n\n\n<p>Here&#8217;s what happens if we try to set key that doesn&#8217;t already exist:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET country \"Thailand\" XX<\/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>The key wasn&#8217;t set, because it doesn&#8217;t already exist.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>GET<\/code> Option<\/h2>\n\n\n\n<p>Here&#8217;s an example of using the <code>GET<\/code> option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET type \"Bird\" GET<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Cat\"<\/pre>\n\n\n\n<p>The <code>GET<\/code> option returns the old value of the key. In our case, we had previously set the <code>type<\/code> key to <code>Cat<\/code> and so that&#8217;s what is returned. <\/p>\n\n\n\n<p>Bear in mind that the key now holds the new value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET type<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Bird\"<\/pre>\n\n\n\n<p>This option can be used as a replacement for the <code>GETSET<\/code> command (which has been regarded as deprecated since Redis 6.2.0).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Set an Expiry Time<\/h2>\n\n\n\n<p>Here&#8217;s an example of using the <code>EX<\/code> option to set an expiry time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET type \"Horse\" EX 5<\/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>The <code>EX<\/code> option allows us to set an expiry time, in seconds. In this case, I set the expiry time to 5 seconds.<\/p>\n\n\n\n<p>Here&#8217;s what happens when I run the <code>GET<\/code> command after waiting for more than 5 seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET type<\/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>So the key no longer exists.<\/p>\n\n\n\n<p>We can alternatively use the <code>PX<\/code> option to specify the expiry time in milliseconds, or the <code>EXAT<\/code> option to set the Unix time at which the key will expire, in seconds, or <code>PXAT<\/code> for milliseconds.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the SET command sets a key to hold a given string value. If the key already holds a value, it is overwritten with the new value. Also, any previous time to live associated with the key is discarded (assuming a successful SET operation).<\/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-25799","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\/25799","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=25799"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25799\/revisions"}],"predecessor-version":[{"id":25919,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25799\/revisions\/25919"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=25799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=25799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=25799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}