{"id":25892,"date":"2022-07-03T10:56:03","date_gmt":"2022-07-03T00:56:03","guid":{"rendered":"https:\/\/database.guide\/?p=25892"},"modified":"2022-07-03T10:56:03","modified_gmt":"2022-07-03T00:56:03","slug":"redis-setrange-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-setrange-command-explained\/","title":{"rendered":"Redis SETRANGE Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>SETRANGE<\/code> command allows us to overwrite part of a string at a given key, starting at a specified offset. It overwrites the old value from the specified offset, for the entire length of the new value.<\/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>SETRANGE key offset value<\/code><\/pre>\n\n\n\n<p>Where <code>key<\/code> is the key to set, <code>offset<\/code> is where the new string will start, and <code>value<\/code> is the new value to start at that offset.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we set a key to the following value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET country \"New Zealand\"<\/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>And we confirm that it holds that value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET country<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"New Zealand\"<\/pre>\n\n\n\n<p>Let&#8217;s now use <code>SETRANGE<\/code> to update that value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SETRANGE country 4 \"Caledonia\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 13<\/pre>\n\n\n\n<p>The command returns an integer reply that specifies the length of the string after it was modified by the <code>SETRANGE<\/code> command.<\/p>\n\n\n\n<p>Now let&#8217;s get the value again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET country<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"New Caledonia\"<\/pre>\n\n\n\n<p>It has updated the string as expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Offset is Larger than the Original String<\/h2>\n\n\n\n<p>If the offset is larger than the length of the original string, the string is padded with zero-bytes to make the offset fit.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SETRANGE country 20 \"Thailand\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 28<\/pre>\n\n\n\n<p>Now let&#8217;s get the value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET country<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"New Caledonia\\x00\\x00\\x00\\x00\\x00\\x00\\x00Thailand\"<\/pre>\n\n\n\n<p>The string has been padded with zero-bytes to make the offset fit. Depending on where you run it, you may see the same result as above, or you may see the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"New CaledoniaThailand\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Non Existent Keys<\/h2>\n\n\n\n<p>Non-existent keys are considered empty strings, so the <code>SETRANGE<\/code> command will make sure it holds a string large enough to be able to set the value at the specified offset.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SETRANGE city 10 \"Bangkok\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 17<\/pre>\n\n\n\n<p>Now let&#8217;s get the value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET city<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00Bangkok\"<\/pre>\n\n\n\n<p>Again, given the string has been padded with zero-bytes, you might see this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Bangkok\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Negative Offset<\/h2>\n\n\n\n<p>Using a negative value for the offset produces an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SETRANGE city -3 \"Los Angeles\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) ERR offset is out of range<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the SETRANGE command allows us to overwrite part of a string at a given key, starting at a specified offset. It overwrites the old value from the specified offset, for the entire length of the new value.<\/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-25892","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\/25892","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=25892"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25892\/revisions"}],"predecessor-version":[{"id":25907,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25892\/revisions\/25907"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=25892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=25892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=25892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}