Golang Redis客户端


本站和网页 https://redis.uptrace.dev/zh/ 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

Golang Redis客户端
Open-source APMGolang ORMGolang ClickHouseBlogGo Redis API文档 在新窗口打开 GitHub 在新窗口打开选择语言选择语言 English 简体中文 API文档 在新窗口打开 GitHub 在新窗口打开选择语言选择语言 English 简体中文 Go Redis支持Redis Server和Redis Cluster的Golang客户端 介绍 入门 多种客户端支持单机Redis Server、Redis Cluster、Redis Sentinel、Redis分片服务器数据类型go-redis会根据不同的redis命令处理成指定的数据类型,不必进行繁琐的数据类型转换功能完善go-redis支持管道(pipeline)、事务、pub/sub、Lua脚本、mock、分布式锁等功能package main
import (
"context"
"github.com/redis/go-redis/v9"
func main() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
err := rdb.Set(ctx, "key", "value", 0).Err()
if err != nil {
panic(err)
val, err := rdb.Get(ctx, "key").Result()
if err != nil {
panic(err)
fmt.Println("key", val)
val2, err := rdb.Get(ctx, "key2").Result()
if err == redis.Nil {
fmt.Println("key2 does not exist")
} else if err != nil {
panic(err)
} else {
fmt.Println("key2", val2)
// Output: key value
// key2 does not exist
Copyright © 2023 Go-Redis Authors