GET (original) (raw)
Syntax
GET key
Available since:
Redis Open Source 1.0.0
Time complexity:
O(1)
ACL categories:
@read
, @string
, @fast
,
Get the value of key
. If the key does not exist the special value nil
is returned. An error is returned if the value stored at key
is not a string, because GET
only handles string values.
Examples
GET nonexisting SET mykey "Hello" GET mykey
Code examples
Python
"""
Code samples for data structure store quickstart pages:
https://redis.io/docs/latest/develop/get-started/data-store/
"""
import redis
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
res = r.set("bike:1", "Process 134")
print(res)
# >>> True
res = r.get("bike:1")
print(res)
# >>> "Process 134"
Node.js
import { createClient } from 'redis';
const client = createClient();
client.on('error', err => console.log('Redis Client Error', err));
await client.connect().catch(console.error);
await client.set('bike:1', 'Process 134');
const value = await client.get('bike:1');
console.log(value);
// returns 'Process 134'
await client.quit();
Java-Sync
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
public class SetGetExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
String status = jedis.set("bike:1", "Process 134");
if ("OK".equals(status)) System.out.println("Successfully added a bike.");
String value = jedis.get("bike:1");
if (value != null) System.out.println("The name of the bike is: " + value + ".");
jedis.close();
}
}
Go
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_Set_and_get() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
err := rdb.Set(ctx, "bike:1", "Process 134", 0).Err()
if err != nil {
panic(err)
}
fmt.Println("OK")
value, err := rdb.Get(ctx, "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Printf("The name of the bike is %s", value)
}
C#
using NRedisStack.Tests;
using StackExchange.Redis;
public class SetGetExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool status = db.StringSet("bike:1", "Process 134");
if (status)
Console.WriteLine("Successfully added a bike.");
var value = db.StringGet("bike:1");
if (value.HasValue)
Console.WriteLine("The name of the bike is: " + value + ".");
}
}
RESP2 Reply
One of the following:
- Bulk string reply: the value of the key.
- Nil reply: if the key does not exist.
RESP3 Reply
One of the following:
- Bulk string reply: the value of the key.
- Null reply: key does not exist.