SETRANGE

Redis Developer Course Redis Cloud Service Redis-Gate Server

Overwrite part of a string at key starting at the specified offset

Overwrites part of the value(string) stored at key, starting at the specified offset, for the entire length(bytes) of value.   If the offset is larger than the current length of the value at key, the value is padded with zero-bytes to make offset fit.   Non-existing keys are considered as empty string, so this command will make sure it holds a string large enough to be able to set value at offset.   Offset can start from 0.
Usage is setrange key offset value.

Return value

Integer reply: the length(bytes) of the string after it was modified by the command.

Example

command>set key "This is MySQL Server"
result>OK
command>setrange key 8 Redis
result>20
command>get key
result>This is Redis Server

Animation


Zero-bytes padding

Example

command>setrange key 3 Redis
result>8
command>get key
result>"\x00\x00\x00Redis"

Negative offset

Example

command>setrange key -3 Redis
result>(error) ERR offset is out of range
Offset can not be negative.

Memory Usage Test

Memory usage bytes

MegabytesBytesPower of 2
2MB2,097,151221 -1
4MB4,194,303222 -1
8MB8,388,607223 -1
16MB16,777,215224 -1
32MB33,554,431225 -1
64MB67,108,863226 -1
128MB134,217,727227 -1
256MB268,435,455228 -1
512MB536,870,911229 -1

Redis Server 3.2.2 and CentOS 7

CommandsIncrease size(MB)Increase size(bytes)Return value
setrange key2 2097151 R2.5MB2,621,5202,097,152
setrange key4 4194303 R5MB5,242,9604,194,304
setrange key8 8388607 R10MB10,485,8408,388,608
setrange key16 16777215 R20MB20,971,60016,777,216
setrange key32 33554431 R40MB41,943,12033,554,432
setrange key64 67108863 R80MB83,886,37667,108,864
setrange key124 134217727 R160MB167,772,456134,217,728
setrange key256 268435455 R320MB335,544,616268,435,456
setrange key512 536870911 R640MB671,088,936536,870,912

바이트 536,870,912(229)을 입력하면 다음과 같은 에러가 발생한다.   그래서 2의 거듭제곱 바이트에 -1을 한 것이다.
If you enter bytes 536,870,912(229), the following error occurs.   That's why we put -1 to the power of 2.

127.0.0.1:7000> setrange key512 536870912 R
(error) ERR string exceeds maximum allowed size (512MB)


Syntax

SETRANGE key offset value

  • Available since 2.2.0.
  • Time complexity(Logical processing time): O(1), not counting the time taken to copy the new string in place.   Usually, this string is very small so the amortized complexity is O(1).   Otherwise, complexity is O(M) with M being the length of the value argument.
  • This document is based on Redis server version 3.2.2.
Related Commands SET, GETRANGE, STRLEN
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

To practice the command, click here to run Redis Web Client


<< STRLEN SETRANGE GETRANGE >>

Click for a question or to see comments.  Comments :    Viewed :

Email 답글이 올라오면 이메일로 알려드리겠습니다.