{"id":25959,"date":"2022-06-27T12:01:57","date_gmt":"2022-06-27T02:01:57","guid":{"rendered":"https:\/\/database.guide\/?p=25959"},"modified":"2022-06-27T12:01:57","modified_gmt":"2022-06-27T02:01:57","slug":"redis-incr-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-incr-command-explained\/","title":{"rendered":"Redis INCR Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>INCR<\/code> command increments the value of a specified key by one.<\/p>\n\n\n\n<p>If the key doesn&#8217;t exist, <code>INCR<\/code> creates the key with a value of <code>0<\/code> and then increments it by one.<\/p>\n\n\n\n<p>An error occurs if the key contains a value of the wrong type or contains a string that cannot be represented as integer.&nbsp;<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>INCR key<\/code><\/pre>\n\n\n\n<p>Where <code><em>key<\/em><\/code> is the key to increment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we set a key like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET pageviews \"20\"<\/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>Let&#8217;s quickly take a look at it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET pageviews<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"20\"<\/pre>\n\n\n\n<p>OK, now let&#8217;s increment it by one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCR pageviews<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 21<\/pre>\n\n\n\n<p>We get an integer reply with the value of the key after the <code>INCR<\/code> operation was performed.<\/p>\n\n\n\n<p>Let&#8217;s run the <code><a href=\"https:\/\/database.guide\/redis-get-command-explained\/\" data-type=\"post\" data-id=\"25826\">GET<\/a><\/code> command again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET pageviews<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"21\"<\/pre>\n\n\n\n<p>As expected, the original value has been incremented by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Negative Values<\/h2>\n\n\n\n<p>Redis does not have a dedicated integer type. However, the string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation. Therefore, we can increment negative values just as we can increment positive values.<\/p>\n\n\n\n<p>Set a negative value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET points \"-7\"<\/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>Let&#8217;s quickly take a look at it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET points<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"-7\"<\/pre>\n\n\n\n<p>OK, now let&#8217;s increment it by one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCR points<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) -6<\/pre>\n\n\n\n<p>The value has been incremented by one.<\/p>\n\n\n\n<p>We can check this at any time by running the <code>GET<\/code> command again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET points<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"-6\"<\/pre>\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>INCR<\/code> creates the key with a value of <code>0<\/code> and then increments it by one.<\/p>\n\n\n\n<p>Let&#8217;s delete our <code>score<\/code> key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEL points<\/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>The <code>DEL<\/code> command returns the number of keys that were deleted &#8211; in this case 1.<\/p>\n\n\n\n<p>Now let&#8217;s try to increment that (non-existent) key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCR points<\/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>So we can see that the <code>points<\/code> key has been recreated with a value of 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Value is the Wrong Type<\/h2>\n\n\n\n<p>As mentioned, an error occurs if the key contains a value of the wrong type or contains a string that cannot be represented as integer.<\/p>\n\n\n\n<p>To test this, let&#8217;s try incrementing a value that can&#8217;t be represented as an integer.<\/p>\n\n\n\n<p>Set the amount:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET pageviews \"Twenty\"<\/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>Let&#8217;s quickly take a look at it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET pageviews<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Twenty\"<\/pre>\n\n\n\n<p>OK, now let&#8217;s try to increment it by one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCR pageviews<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) ERR value is not an integer or out of range<\/pre>\n\n\n\n<p>An error occurred as expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the INCR command increments the value of a specified key by one. If the key doesn&#8217;t exist, INCR creates the key with a value of 0 and then increments it by one. An error occurs if the key contains a value of the wrong type or contains a string that cannot be represented &#8230; <a title=\"Redis INCR Command Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/redis-incr-command-explained\/\" aria-label=\"Read more about Redis INCR Command Explained\">Read more<\/a><\/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-25959","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\/25959","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=25959"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25959\/revisions"}],"predecessor-version":[{"id":26272,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25959\/revisions\/26272"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=25959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=25959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=25959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}