获取
Syntax
GET key
- Available since:
- 1.0.0
- Time complexity:
- O(1)
- ACL categories:
-
@read
,@string
,@fast
,
获取key
的值。
如果键不存在,则返回特殊值nil
。
如果存储在key
的值不是字符串,则返回错误,因为GET
仅处理字符串值。
示例
代码示例
"""
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"
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();
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 + ".");
}
}
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)
}
using NRedisStack.Tests;
using StackExchange.Redis;
public class SetGetExample
{
public void run()
{
var redis = ConnectionMultiplexer.Connect("localhost:6379");
var db = redis.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 回复
以下之一:
- Bulk string reply: 键的值。
- Nil reply: 如果键不存在。
RESP3 回复
以下之一:
- Bulk string reply: 键的值。
- Null reply: 键不存在。