Summary
Command Name | APPEND |
Usage | Append string at the end |
Group | string |
ACL Category | @write @string @slow |
Time Complexity | O(1) |
Flag | WRITE DENYOOM FAST |
Arity | 3 |
Signature
APPEND <key> <value>
Usage
Append the provided string <value> to the existing string value of a <key>. If the key does not exist then this command creates the key first with an empty value, then appends the provided value to that empty string.
Notes
- In case the <key> does not exist then the APPEND command behaves like the SET command.
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | The key name | key | key |
<value> | The string value to be appended | value | string |
Return Value
Returns length of the full string after append is performed.
Return value | Case for the return value | Type |
---|---|---|
Length of the final string | When append operation is successful | integer |
Error | If the key already exist and the value is not of type string | error |
Notes
- On successful execution, the APPEND command returns the length of the final string, that we get after appending the provided value.
- We get an error in case we try to append to some other data type like list, set, hash etc. (other than string type).
Examples
Here are a few examples of the APPEND command usage-
# Redis APPEND command examples
# Check firstkey, it not exist
127.0.0.1:6379> get firstkey
(nil)
# Append "abc" to the firstkey.
# As firstkey does not already exist, so it will be created and "abc" will be appended to that.
# After append the length of firstkey value is three(3), so "3" is returned
127.0.0.1:6379> append firstkey "abc"
(integer) 3
# Check firstkey, we get "abc"
127.0.0.1:6379> get firstkey
"abc"
# Append "def" to firstkey.
# As firstkey already has "abc" as value, if "def" is appended then firstkey value becomes "abcdef".
# After append the total length of firstkey value is six(6) so "6" is returned as result.
127.0.0.1:6379> append firstkey "def"
(integer) 6
# Check firstkey, we get "abcded"
127.0.0.1:6379> get firstkey
"abcdef"
# Check the length of firstkey and we get six(6)
127.0.0.1:6379> strlen firstkey
(integer) 6
# Let's check with another key, secondkey, it is not set yet.
127.0.0.1:6379> get secondkey
(nil)
# Append a blank string "" to secondkey.
# secondkey will be create and blank sring "" will be appended to it.
# As a result the value os second key becomes a blank string "", and length becomes zero(0)
# Zero(0) is returned as result
127.0.0.1:6379> append secondkey ""
(integer) 0
# Check secondkey
127.0.0.1:6379> get secondkey
""
# Check secondkey length
127.0.0.1:6379> strlen secondkey
(integer) 0
# Create a list
127.0.0.1:6379> lpush mylist abc
(integer) 1
# Try to append string to the list type. Returns error
127.0.0.1:6379> append mylist 98
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- We can append an empty string (“”) using the append command. In that case, the length of the string will be the same as before.
Code Implementations
Here are the usage examples of the Redis APPEND command in different programming languages.
// Redis APPEND command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
// Check firstkey, it not exist
// Command: get firstkey
// Result: (nil)
commandResult, err := rdb.Get(ctx, "firstkey").Result()
if err != nil {
fmt.Println("Command: get firstkey | Error: " + err.Error())
}
fmt.Println("Command: get firstkey | Result: " + commandResult)
// Append "abc" to the firstkey.
// As firstkey does not already exist, so it will be created and "abc" will be appended to that.
// After append the length of firstkey value is three(3), so "3" is returned
// Command: append firstkey "abc"
// Result: (integer) 3
intResult, err := rdb.Append(ctx, "firstkey", "abc").Result()
if err != nil {
fmt.Println("Command: append firstkey "abc" | Error: " + err.Error())
}
fmt.Printf("Command: append firstkey "abc" | Result: %dn", intResult)
// Check firstkey, we get "abc"
// Command: get firstkey
// Result: "abc"
commandResult, err = rdb.Get(ctx, "firstkey").Result()
if err != nil {
fmt.Println("Command: get firstkey | Error: " + err.Error())
}
fmt.Println("Command: get firstkey | Result: " + commandResult)
// Append "def" to firstkey.
// As firstkey already has "abc" as value, if "def" is appended then firstkey value becomes "abcdef".
// After append the total length of firstkey value is six(6) so "6" is returned as result.
// Command: append firstkey "def"
// Result: (integer) 6
intResult, err = rdb.Append(ctx, "firstkey", "def").Result()
if err != nil {
fmt.Println("Command: append firstkey "def" | Error: " + err.Error())
}
fmt.Printf("Command: append firstkey "def" | Result: %dn", intResult)
// Check firstkey, we get "abcded"
// Command: get firstkey
// Result: "abcdef"
commandResult, err = rdb.Get(ctx, "firstkey").Result()
if err != nil {
fmt.Println("Command: get firstkey | Error: " + err.Error())
}
fmt.Println("Command: get firstkey | Result: " + commandResult)
// Check the length of firstkey and we get six(6)
// Command: strlen firstkey
// (integer) 6
intResult, err = rdb.StrLen(ctx, "firstkey").Result()
if err != nil {
fmt.Println("Command: strlen firstkey | Error: " + err.Error())
}
fmt.Printf("Command: strlen firstkey | Result: %dn", intResult)
// Let's check with another key, secondkey, it is not set yet.
// Command: get secondkey
// Result: (nil)
commandResult, err = rdb.Get(ctx, "secondkey").Result()
if err != nil {
fmt.Println("Command: get secondkey | Error: " + err.Error())
}
fmt.Println("Command: get secondkey | Result: " + commandResult)
// Append a blank string "" to secondkey.
// secondkey will be create and blank sring "" will be appended to it.
// As a result the value os second key becomes a blank string "", and length becomes zero(0)
// Zero(0) is returned as result
// Command: append secondkey ""
// Result: (integer) 0
intResult, err = rdb.Append(ctx, "secondkey", "").Result()
if err != nil {
fmt.Println("Command: append secondkey "" | Error: " + err.Error())
}
fmt.Printf("Command: append secondkey "" | Result: %dn", intResult)
// Check secondkey
// Command: get secondkey
// Result: ""
commandResult, err = rdb.Get(ctx, "secondkey").Result()
if err != nil {
fmt.Println("Command: get secondkey | Error: " + err.Error())
}
fmt.Println("Command: get secondkey | Result: " + commandResult)
// Check secondkey length
// Command: strlen secondkey
// Result: (integer) 0
intResult, err = rdb.StrLen(ctx, "secondkey").Result()
if err != nil {
fmt.Println("Command: strlen secondkey | Error: " + err.Error())
}
fmt.Printf("Command: strlen secondkey | Result: %dn", intResult)
// Create a list
// Command: lpush mylist abc
// Result: (integer) 1
lpushCommandResult, err := rdb.LPush(ctx, "mylist", "abc").Result()
if err != nil {
fmt.Println("Command: lpush mylist abc | Error: " + err.Error())
}
fmt.Printf("Command: lpush mylist abc | Result: %vn", lpushCommandResult)
// Try to append string to the list type. Returns error
// Command: append mylist 98
// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
intResult, err = rdb.Append(ctx, "mylist", "98").Result()
if err != nil {
fmt.Println("Command: append mylist 98 | Error: " + err.Error())
}
fmt.Printf("Command: append mylist 98 | Result: %dn", intResult)
}
Output:
Command: get firstkey | Error: redis: nil
Command: get firstkey | Result:
Command: append firstkey "abc" | Result: 3
Command: get firstkey | Result: abc
Command: append firstkey "def" | Result: 6
Command: get firstkey | Result: abcdef
Command: strlen firstkey | Result: 6
Command: get secondkey | Error: redis: nil
Command: get secondkey | Result:
Command: append secondkey "" | Result: 0
Command: get secondkey | Result:
Command: strlen secondkey | Result: 0
Command: lpush mylist abc | Result: 1
Command: append mylist 98 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: append mylist 98 | Result: 0
Notes
- Use the method “Append” from redis-go for the Redis APPEND command usage.
- Signature of the method is-
func (c cmdable) Append(ctx context.Context, key, value string) *IntCmd
// Redis APPEND command example in JavaScript(NodeJS)
import { createClient } from 'redis';
// Create redis client
const redisClient = createClient({
url: 'redis://default:@localhost:6379'
});
redisClient.on('error', err => console.log('Error while connecting to Redis', err));
// Connect Redis client
await redisClient.connect();
/**
* Check firstkey, it not exist
* Command: get firstkey
* Result: (nil)
*/
let commandResult = await redisClient.get("firstkey");
console.log("Command: get firstkey | Result: " + commandResult);
/**
* Append "abc" to the firstkey.
* As firstkey does not already exist, so it will be created and "abc" will be appended to that.
* After append the length of firstkey value is three(3), so "3" is returned
* Command: append firstkey "abc"
* Result: (integer) 3
*/
commandResult = await redisClient.append("firstkey", "abc");
console.log("Command: append firstkey "abc" | Result: " + commandResult);
/**
* Check firstkey, we get "abc"
* Command: get firstkey
* Result: "abc"
*/
commandResult = await redisClient.get("firstkey");
console.log("Command: get firstkey | Result: " + commandResult);
/**
* Append "def" to firstkey.
* As firstkey already has "abc" as value, if "def" is appended then firstkey value becomes "abcdef".
* After append the total length of firstkey value is six(6) so "6" is returned as result.
* Command: append firstkey "def"
* Result: (integer) 6
*/
commandResult = await redisClient.append("firstkey", "def");
console.log("Command: append firstkey "def" | Result: " + commandResult);
/**
* Check firstkey, we get "abcded"
* Command: get firstkey
* Result: "abcdef"
*/
commandResult = await redisClient.get("firstkey");
console.log("Command: get firstkey | Result: " + commandResult);
/**
* Check the length of firstkey and we get six(6)
* Command: strlen firstkey
* (integer) 6
*/
commandResult = await redisClient.strLen("firstkey");
console.log("Command: strlen firstkey | Result: " + commandResult);
/**
* Let's check with another key, secondkey, it is not set yet.
* Command: get secondkey
* Result: (nil)
*/
commandResult = await redisClient.get("secondkey");
console.log("Command: get secondkey | Result: " + commandResult);
/**
* Append a blank string "" to secondkey.
* secondkey will be create and blank sring "" will be appended to it.
* As a result the value os second key becomes a blank string "", and length becomes zero(0)
* Zero(0) is returned as result
* Command: append secondkey ""
* Result: (integer) 0
*/
commandResult = await redisClient.append("secondkey", "");
console.log("Command: append secondkey "" | Result: " + commandResult);
/**
* Check secondkey
* Command: get secondkey
* Result: ""
*/
commandResult = await redisClient.get("secondkey");
console.log("Command: get secondkey | Result: " + commandResult);
/**
* Check secondkey length
* Command: strlen secondkey
* Result: (integer) 0
*/
commandResult = await redisClient.strLen("secondkey");
console.log("Command: strlen secondkey | Result: " + commandResult);
/**
* Create a list
* Command: lpush mylist abc
* Result: (integer) 1
*/
commandResult = await redisClient.lPush("mylist", "abc");
console.log("Command: lpush mylist abc | Result: " + commandResult);
/**
* Try to append string to the list type. Returns error
* Command: append mylist 98
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.append("mylist", "98");
console.log("Command: append mylist 98 | Result: " + commandResult);
} catch (error) {
console.log(error);
}
process.exit(0);
Output:
Command: get firstkey | Result: null
Command: append firstkey "abc" | Result: 3
Command: get firstkey | Result: abc
Command: append firstkey "def" | Result: 6
Command: get firstkey | Result: abcdef
Command: strlen firstkey | Result: 6
Command: get secondkey | Result: null
Command: append secondkey "" | Result: 0
Command: get secondkey | Result:
Command: strlen secondkey | Result: 0
Command: lpush mylist abc | Result: 1
[ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Notes
- Use method “append” from the redis node package for the APPEND command.
// Redis APPEND example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class Append {
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Check firstkey, it not exist
* Command: get firstkey
* Result: (nil)
*/
String getResult = jedis.get("firstkey");
System.out.println("Command: get firstkey | Result: " + getResult);
/**
* Append "abc" to the firstkey.
* As firstkey does not already exist, so it will be created and "abc" will be appended to that.
* After append the length of firstkey value is three(3), so "3" is returned
* Command: append firstkey "abc"
* Result: (integer) 3
*/
long intResult = jedis.append("firstkey", "abc");
System.out.println("Command: append firstkey "abc" | Result: " + intResult);
/**
* Check firstkey, we get "abc"
* Command: get firstkey
* Result: "abc"
*/
getResult = jedis.get("firstkey");
System.out.println("Command: get firstkey | Result: " + getResult);
/**
* Append "def" to firstkey.
* As firstkey already has "abc" as value, if "def" is appended then firstkey value becomes "abcdef".
* After append the total length of firstkey value is six(6) so "6" is returned as result.
* Command: append firstkey "def"
* Result: (integer) 6
*/
intResult = jedis.append("firstkey", "def");
System.out.println("Command: append firstkey "def" | Result: " + intResult);
/**
* Check firstkey, we get "abcded"
* Command: get firstkey
* Result: "abcdef"
*/
getResult = jedis.get("firstkey");
System.out.println("Command: get firstkey | Result: " + getResult);
/**
* Check the length of firstkey and we get six(6)
* Command: strlen firstkey
* (integer) 6
*/
intResult = jedis.strlen("firstkey");
System.out.println("Command: strlen firstkey | Result: " + intResult);
/**
* Let's check with another key, secondkey, it is not set yet.
* Command: get secondkey
* Result: (nil)
*/
getResult = jedis.get("secondkey");
System.out.println("Command: get secondkey | Result: " + getResult);
/**
* Append a blank string "" to secondkey.
* secondkey will be create and blank sring "" will be appended to it.
* As a result the value os second key becomes a blank string "", and length becomes zero(0)
* Zero(0) is returned as result
* Command: append secondkey ""
* Result: (integer) 0
*/
intResult = jedis.append("secondkey", "");
System.out.println("Command: append secondkey "" | Result: " + intResult);
/**
* Check secondkey
* Command: get secondkey
* Result: ""
*/
getResult = jedis.get("secondkey");
System.out.println("Command: get secondkey | Result: " + getResult);
/**
* Check secondkey length
* Command: strlen secondkey
* Result: (integer) 0
*/
intResult = jedis.strlen("secondkey");
System.out.println("Command: strlen secondkey | Result: " + intResult);
/**
* Create a list
* Command: lpush mylist abc
* Result: (integer) 1
*/
long lpushCommandResult = jedis.lpush("mylist", "abc");
System.out.println("Command: lpush mylist abc | Result: " + lpushCommandResult);
/**
* Try to append string to the list type. Returns error
* Command: append mylist 98
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
intResult = jedis.append("mylist", "98");
System.out.println("Command: append mylist 98 | Result: " + intResult);
} catch (Exception e) {
System.out.println("Command: append mylist 98 | Error: " + e.getMessage());
}
}
}
}
Output:
Command: get firstkey | Result: null
Command: append firstkey "abc" | Result: 3
Command: get firstkey | Result: abc
Command: append firstkey "def" | Result: 6
Command: get firstkey | Result: abcdef
Command: strlen firstkey | Result: 6
Command: get secondkey | Result: null
Command: append secondkey "" | Result: 0
Command: get secondkey | Result:
Command: strlen secondkey | Result: 0
Command: lpush mylist abc | Result: 1
Command: append mylist 98 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “append” from the Jedis package for using Redis APPEND command in Java.
- Signature of the “append” command is-
public long append(final String key, final String value)
using StackExchange.Redis;
namespace Append
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Check firstkey, it not exist
* Command: get firstkey
* Result: (nil)
*/
RedisValue getCommandResult = rdb.StringGet("firstkey");
Console.WriteLine("Command: get firstkey | Result: " + getCommandResult);
/**
* Append "abc" to the firstkey.
* As firstkey does not already exist, so it will be created and "abc" will be appended to that.
* After append the length of firstkey value is three(3), so "3" is returned
* Command: append firstkey "abc"
* Result: (integer) 3
*/
long appendCommandResult = rdb.StringAppend("firstkey", "abc");
Console.WriteLine("Command: append firstkey "abc" | Result: " + appendCommandResult);
/**
* Check firstkey, we get "abc"
* Command: get firstkey
* Result: "abc"
*/
getCommandResult = rdb.StringGet("firstkey");
Console.WriteLine("Command: get firstkey | Result: " + getCommandResult);
/**
* Append "def" to firstkey.
* As firstkey already has "abc" as value, if "def" is appended then firstkey value becomes "abcdef".
* After append the total length of firstkey value is six(6) so "6" is returned as result.
* Command: append firstkey "def"
* Result: (integer) 6
*/
appendCommandResult = rdb.StringAppend("firstkey", "def");
Console.WriteLine("Command: append firstkey "def" | Result: " + appendCommandResult);
/**
* Check firstkey, we get "abcded"
* Command: get firstkey
* Result: "abcdef"
*/
getCommandResult = rdb.StringGet("firstkey");
Console.WriteLine("Command: get firstkey | Result: " + getCommandResult);
/**
* Check the length of firstkey and we get six(6)
* Command: strlen firstkey
* (integer) 6
*/
long intResult = rdb.StringLength("firstkey");
Console.WriteLine("Command: strlen firstkey | Result: " + intResult);
/**
* Let's check with another key, secondkey, it is not set yet.
* Command: get secondkey
* Result: (nil)
*/
getCommandResult = rdb.StringGet("secondkey");
Console.WriteLine("Command: get secondkey | Result: " + getCommandResult);
/**
* Append a blank string "" to secondkey.
* secondkey will be create and blank sring "" will be appended to it.
* As a result the value os second key becomes a blank string "", and length becomes zero(0)
* Zero(0) is returned as result
* Command: append secondkey ""
* Result: (integer) 0
*/
appendCommandResult = rdb.StringAppend("secondkey", "");
Console.WriteLine("Command: append secondkey "" | Result: " + appendCommandResult);
/**
* Check secondkey
* Command: get secondkey
* Result: ""
*/
getCommandResult = rdb.StringGet("secondkey");
Console.WriteLine("Command: get secondkey | Result: " + getCommandResult);
/**
* Check secondkey length
* Command: strlen secondkey
* Result: (integer) 0
*/
intResult = rdb.StringLength("secondkey");
Console.WriteLine("Command: strlen secondkey | Result: " + intResult);
/**
* Create a list
* Command: lpush mylist abc
* Result: (integer) 1
*/
long listCommandResult = rdb.ListLeftPush("mylist", new RedisValue[] { "abc" });
Console.WriteLine("Command: lpush mylist abc | Result: " + listCommandResult);
/**
* Try to append string to the list type. Returns error
* Command: append mylist 98
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
appendCommandResult = rdb.StringAppend("mylist", "98");
Console.WriteLine("Command: append mylist 98 | Result: " + appendCommandResult);
}
catch (Exception e)
{
Console.WriteLine("Command: append mylist 98 | Error: " + e.Message);
}
}
}
}
Output:
Command: get firstkey | Result:
Command: append firstkey "abc" | Result: 3
Command: get firstkey | Result: abc
Command: append firstkey "def" | Result: 6
Command: get firstkey | Result: abcdef
Command: strlen firstkey | Result: 6
Command: get secondkey | Result:
Command: append secondkey "" | Result: 0
Command: get secondkey | Result:
Command: strlen secondkey | Result: 0
Command: lpush mylist abc | Result: 1
Command: append mylist 98 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- We have to use the method “StringAppend” of StackExchange.Redis for this Redis APPEND command.
- Signature of the command is-
long StringAppend(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None)
<?php
// Redis APPEND command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Check firstkey, it not exist
* Command: get firstkey
* Result: (nil)
*/
$commandResult = $redisClient->get("firstkey");
echo "Command: get firstkey | Result: " . $commandResult . "n";
/**
* Append "abc" to the firstkey.
* As firstkey does not already exist, so it will be created and "abc" will be appended to that.
* After append the length of firstkey value is three(3), so "3" is returned
* Command: append firstkey "abc"
* Result: (integer) 3
*/
$commandResult = $redisClient->append("firstkey", "abc");
echo "Command: append firstkey "abc" | Result: " . $commandResult . "n";
/**
* Check firstkey, we get "abc"
* Command: get firstkey
* Result: "abc"
*/
$commandResult = $redisClient->get("firstkey");
echo "Command: get firstkey | Result: " . $commandResult . "n";
/**
* Append "def" to firstkey.
* As firstkey already has "abc" as value, if "def" is appended then firstkey value becomes "abcdef".
* After append the total length of firstkey value is six(6) so "6" is returned as result.
* Command: append firstkey "def"
* Result: (integer) 6
*/
$commandResult = $redisClient->append("firstkey", "def");
echo "Command: append firstkey "def" | Result: " . $commandResult . "n";
/**
* Check firstkey, we get "abcded"
* Command: get firstkey
* Result: "abcdef"
*/
$commandResult = $redisClient->get("firstkey");
echo "Command: get firstkey | Result: " . $commandResult . "n";
/**
* Check the length of firstkey and we get six(6)
* Command: strlen firstkey
* (integer) 6
*/
$commandResult = $redisClient->strlen("firstkey");
echo "Command: strlen firstkey | Result: " . $commandResult . "n";
/**
* Let's check with another key, secondkey, it is not set yet.
* Command: get secondkey
* Result: (nil)
*/
$commandResult = $redisClient->get("secondkey");
echo "Command: get secondkey | Result: " . $commandResult . "n";
/**
* Append a blank string "" to secondkey.
* secondkey will be create and blank sring "" will be appended to it.
* As a result the value os second key becomes a blank string "", and length becomes zero(0)
* Zero(0) is returned as result
* Command: append secondkey ""
* Result: (integer) 0
*/
$commandResult = $redisClient->append("secondkey", "");
echo "Command: append secondkey "" | Result: " . $commandResult . "n";
/**
* Check secondkey
* Command: get secondkey
* Result: ""
*/
$commandResult = $redisClient->get("secondkey");
echo "Command: get secondkey | Result: " . $commandResult . "n";
/**
* Check secondkey length
* Command: strlen secondkey
* Result: (integer) 0
*/
$commandResult = $redisClient->strlen("secondkey");
echo "Command: strlen secondkey | Result: " . $commandResult . "n";
/**
* Create a list
* Command: lpush mylist abc
* Result: (integer) 1
*/
$commandResult = $redisClient->lpush("mylist", "abc");
echo "Command: lpush mylist abc | Result: " . $commandResult . "n";
/**
* Try to append string to the list type. Returns error
* Command: append mylist 98
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->append("mylist", "98");
echo "Command: append mylist 98 | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: append mylist 98 | Error: " . $e->getMessage() . "n";
}
Output:
Command: get firstkey | Result:
Command: append firstkey "abc" | Result: 3
Command: get firstkey | Result: abc
Command: append firstkey "def" | Result: 6
Command: get firstkey | Result: abcdef
Command: strlen firstkey | Result: 6
Command: get secondkey | Result:
Command: append secondkey "" | Result: 0
Command: get secondkey | Result:
Command: strlen secondkey | Result: 0
Command: lpush mylist abc | Result: 1
Command: append mylist 98 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use predis method “append”.
- Signature of this method is-
append(string $key, $value): int
# Redis APPEND command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
username='default', password='',
decode_responses=True)
# Check firstkey, it not exist
# Command: get firstkey
# Result: (nil)
commandResult = redisClient.get("firstkey")
print("Command: get firstkey | Result: {}".format(commandResult))
# Append "abc" to the firstkey.
# As firstkey does not already exist, so it will be created and "abc" will be appended to that.
# After append the length of firstkey value is three(3), so "3" is returned
# Command: append firstkey "abc"
# Result: (integer) 3
commandResult = redisClient.append("firstkey", "abc")
print("Command: append firstkey "abc" | Result: {}".format(commandResult))
# Check firstkey, we get "abc"
# Command: get firstkey
# Result: "abc"
commandResult = redisClient.get("firstkey")
print("Command: get firstkey | Result: {}".format(commandResult))
# Append "def" to firstkey.
# As firstkey already has "abc" as value, if "def" is appended then firstkey value becomes "abcdef".
# After append the total length of firstkey value is six(6) so "6" is returned as result.
# Command: append firstkey "def"
# Result: (integer) 6
commandResult = redisClient.append("firstkey", "def")
print("Command: append firstkey "def" | Result: {}".format(commandResult))
# Check firstkey, we get "abcded"
# Command: get firstkey
# Result: "abcdef"
commandResult = redisClient.get("firstkey")
print("Command: get firstkey | Result: {}".format(commandResult))
# Check the length of firstkey and we get six(6)
# Command: strlen firstkey
# (integer) 6
commandResult = redisClient.strlen("firstkey")
print("Command: strlen firstkey | Result: {}".format(commandResult))
# Let's check with another key, secondkey, it is not set yet.
# Command: get secondkey
# Result: (nil)
commandResult = redisClient.get("secondkey")
print("Command: get secondkey | Result: {}".format(commandResult))
# Append a blank string "" to secondkey.
# secondkey will be create and blank sring "" will be appended to it.
# As a result the value os second key becomes a blank string "", and length becomes zero(0)
# Zero(0) is returned as result
# Command: append secondkey ""
# Result: (integer) 0
commandResult = redisClient.append("secondkey", "")
print("Command: append secondkey "" | Result: {}".format(commandResult))
# Check secondkey
# Command: get secondkey
# Result: ""
commandResult = redisClient.get("secondkey")
print("Command: get secondkey | Result: {}".format(commandResult))
# Check secondkey length
# Command: strlen secondkey
# Result: (integer) 0
commandResult = redisClient.strlen("secondkey")
print("Command: strlen secondkey | Result: {}".format(commandResult))
# Create a list
# Command: lpush mylist abc
# Result: (integer) 1
commandResult = redisClient.lpush("mylist", "abc")
print("Command: lpush mylist abc | Result: {}".format(commandResult))
# Try to append string to the list type. Returns error
# Command: append mylist 98
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.append("mylist", "98")
print("Command: append mylist 98 | Result: {}".format(commandResult))
except Exception as error:
print("Command: append mylist 98 | Error: ", error)
Output:
Command: get firstkey | Result: None
Command: append firstkey "abc" | Result: 3
Command: get firstkey | Result: abc
Command: append firstkey "def" | Result: 6
Command: get firstkey | Result: abcdef
Command: strlen firstkey | Result: 6
Command: get secondkey | Result: None
Command: append secondkey "" | Result: 0
Command: get secondkey | Result:
Command: strlen secondkey | Result: 0
Command: lpush mylist abc | Result: 1
Command: append mylist 98 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the “append” method for appending string to the end of existing string.
- Signature of the “append” method is-
def append(self, key: KeyT, value: EncodableT) -> ResponseT
# Redis APPEND command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Check firstkey, it not exist
# Command: get firstkey
# Result: (nil)
commandResult = redis.get("firstkey")
print("Command: get firstkey | Result: ", commandResult, "n")
# Append "abc" to the firstkey.
# As firstkey does not already exist, so it will be created and "abc" will be appended to that.
# After append the length of firstkey value is three(3), so "3" is returned
# Command: append firstkey "abc"
# Result: (integer) 3
commandResult = redis.append("firstkey", "abc")
print("Command: append firstkey "abc" | Result: ", commandResult, "n")
# Check firstkey, we get "abc"
# Command: get firstkey
# Result: "abc"
commandResult = redis.get("firstkey")
print("Command: get firstkey | Result: ", commandResult, "n")
# Append "def" to firstkey.
# As firstkey already has "abc" as value, if "def" is appended then firstkey value becomes "abcdef".
# After append the total length of firstkey value is six(6) so "6" is returned as result.
# Command: append firstkey "def"
# Result: (integer) 6
commandResult = redis.append("firstkey", "def")
print("Command: append firstkey "def" | Result: ", commandResult, "n")
# Check firstkey, we get "abcded"
# Command: get firstkey
# Result: "abcdef"
commandResult = redis.get("firstkey")
print("Command: get firstkey | Result: ", commandResult, "n")
# Check the length of firstkey and we get six(6)
# Command: strlen firstkey
# (integer) 6
commandResult = redis.strlen("firstkey")
print("Command: strlen firstkey | Result: ", commandResult, "n")
# Let's check with another key, secondkey, it is not set yet.
# Command: get secondkey
# Result: (nil)
commandResult = redis.get("secondkey")
print("Command: get secondkey | Result: ", commandResult, "n")
# Append a blank string "" to secondkey.
# secondkey will be create and blank sring "" will be appended to it.
# As a result the value os second key becomes a blank string "", and length becomes zero(0)
# Zero(0) is returned as result
# Command: append secondkey ""
# Result: (integer) 0
commandResult = redis.append("secondkey", "")
print("Command: append secondkey "" | Result: ", commandResult, "n")
# Check secondkey
# Command: get secondkey
# Result: ""
commandResult = redis.get("secondkey")
print("Command: get secondkey | Result: ", commandResult, "n")
# Check secondkey length
# Command: strlen secondkey
# Result: (integer) 0
commandResult = redis.strlen("secondkey")
print("Command: strlen secondkey | Result: ", commandResult, "n")
# Create a list
# Command: lpush mylist abc
# Result: (integer) 1
commandResult = redis.lpush("mylist", "abc")
print("Command: lpush mylist abc | Result: ", commandResult, "n")
# Try to append string to the list type. Returns error
# Command: append mylist 98
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.append("mylist", "98")
print("Command: append mylist 98 | Result: ", commandResult, "n")
rescue => e
print("Command: getdel users | Error: ", e)
end
Output:
Command: get firstkey | Result:
Command: append firstkey "abc" | Result: 3
Command: get firstkey | Result: abc
Command: append firstkey "def" | Result: 6
Command: get firstkey | Result: abcdef
Command: strlen firstkey | Result: 6
Command: get secondkey | Result:
Command: append secondkey "" | Result: 0
Command: get secondkey | Result:
Command: strlen secondkey | Result: 0
Command: lpush mylist abc | Result: 1
Command: getdel users | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use “append” method of reids-rb.
- Signature of the method is-
def append(key, value)
key and value params both are of type string.
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
GET | Command Details |
SET | Command Details |