setrange
SETRANGE
Write data at a specified position
Writes data starting at the specified position. Offset starts from 0.
Usage is setrange key offset value.
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
|
View animation
|
offset cannot be negative.
|
Memory Usage Test
Memory usage bytes
| Megabytes | Bytes | Power of 2 |
| 2MB | 2,097,151 | 221 -1 |
| 4MB | 4,194,303 | 222 -1 |
| 8MB | 8,388,607 | 223 -1 |
| 16MB | 16,777,215 | 224 -1 |
| 32MB | 33,554,431 | 225 -1 |
| 64MB | 67,108,863 | 226 -1 |
| 128MB | 134,217,727 | 227 -1 |
| 256MB | 268,435,455 | 228 -1 |
| 512MB | 536,870,911 | 229 -1 |
Redis Server 3.2.2 and CentOS 7
| Commands | Increase size(MB) | Increase size(bytes) | Return value |
| setrange key2 2097151 R | 2.5MB | 2,621,520 | 2,097,152 |
| setrange key4 4194303 R | 5MB | 5,242,960 | 4,194,304 |
| setrange key8 8388607 R | 10MB | 10,485,840 | 8,388,608 |
| setrange key16 16777215 R | 20MB | 20,971,600 | 16,777,216 |
| setrange key32 33554431 R | 40MB | 41,943,120 | 33,554,432 |
| setrange key64 67108863 R | 80MB | 83,886,376 | 67,108,864 |
| setrange key124 134217727 R | 160MB | 167,772,456 | 134,217,728 |
| setrange key256 268435455 R | 320MB | 335,544,616 | 268,435,456 |
| setrange key512 536870911 R | 640MB | 671,088,936 | 536,870,912 |
If you enter 536,870,912 bytes (229), the following error occurs.
That is why the table uses a power of 2 minus 1.
127.0.0.1:7000> setrange key512 536870912 R
(error) ERR string exceeds maximum allowed size (512MB)
Syntax
SETRANGE key offset value
- Available since version 2.2.0.
- Time complexity depends on the string being processed.
For a short string it can be treated as O(1); for a long string it is O(M), where M is the length of the value.