• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H05-Mar-2021-

example/otel/H05-Mar-2021-

extra/H05-Mar-2021-

fuzz/H05-Mar-2021-

internal/H05-Mar-2021-

testdata/H05-Mar-2021-

.gitignoreH A D05-Mar-202125

.golangci.ymlH A D05-Mar-2021353

.prettierrcH A D05-Mar-202164

.travis.ymlH A D05-Mar-2021327

CHANGELOG.mdH A D05-Mar-2021156

LICENSEH A D05-Mar-20211.3 KiB

MakefileH A D05-Mar-2021658

README.mdH A D05-Mar-20215.5 KiB

bench_test.goH A D05-Mar-20216.2 KiB

cluster.goH A D05-Mar-202136 KiB

cluster_commands.goH A D05-Mar-2021445

cluster_test.goH A D05-Mar-202130.4 KiB

command.goH A D05-Mar-202148.6 KiB

command_test.goH A D05-Mar-20212.3 KiB

commands.goH A D05-Mar-202181.7 KiB

commands_test.goH A D05-Mar-2021136.3 KiB

doc.goH A D05-Mar-202161

error.goH A D05-Mar-20212.3 KiB

example_instrumentation_test.goH A D05-Mar-20211.9 KiB

example_test.goH A D05-Mar-202112.9 KiB

export_test.goH A D05-Mar-20211.7 KiB

go.modH A D05-Mar-2021343

go.sumH A D05-Mar-20219.3 KiB

internal_test.goH A D05-Mar-20211.6 KiB

iterator.goH A D05-Mar-20211.5 KiB

iterator_test.goH A D05-Mar-20213.3 KiB

main_test.goH A D05-Mar-20219.8 KiB

options.goH A D05-Mar-20218.3 KiB

options_test.goH A D05-Mar-20213.4 KiB

pipeline.goH A D05-Mar-20213.1 KiB

pipeline_test.goH A D05-Mar-20212.3 KiB

pool_test.goH A D05-Mar-20213.7 KiB

pubsub.goH A D05-Mar-202113.9 KiB

pubsub_test.goH A D05-Mar-202112.1 KiB

race_test.goH A D05-Mar-20218.2 KiB

redis.goH A D05-Mar-202117.4 KiB

redis_test.goH A D05-Mar-202110.3 KiB

result.goH A D05-Mar-20214.8 KiB

ring.goH A D05-Mar-202115.7 KiB

ring_test.goH A D05-Mar-202116.6 KiB

script.goH A D05-Mar-20211.6 KiB

sentinel.goH A D05-Mar-202119.3 KiB

sentinel_test.goH A D05-Mar-20215 KiB

tx.goH A D05-Mar-20213.8 KiB

tx_test.goH A D05-Mar-20213.4 KiB

universal.goH A D05-Mar-20215.2 KiB

universal_test.goH A D05-Mar-2021931

README.md

1<p align="center">
2  <a href="https://uptrace.dev/?utm_source=gh-redis&utm_campaign=gh-redis-banner1">
3    <img src="https://raw.githubusercontent.com/uptrace/roadmap/master/banner1.png">
4  </a>
5</p>
6
7# Redis client for Golang
8
9[![Build Status](https://travis-ci.org/go-redis/redis.png?branch=master)](https://travis-ci.org/go-redis/redis)
10[![PkgGoDev](https://pkg.go.dev/badge/github.com/go-redis/redis/v8)](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc)
11[![Documentation](https://img.shields.io/badge/redis-documentation-informational)](https://redis.uptrace.dev/)
12[![Chat](https://discordapp.com/api/guilds/752070105847955518/widget.png)](https://discord.gg/rWtp5Aj)
13
14- Join [Discord](https://discord.gg/rWtp5Aj) to ask questions.
15- [Documentation](https://redis.uptrace.dev)
16- [Reference](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc)
17- [Examples](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#pkg-examples)
18- [RealWorld example app](https://github.com/uptrace/go-treemux-realworld-example-app)
19
20## Ecosystem
21
22- [Redis Mock](https://github.com/go-redis/redismock).
23- [Distributed Locks](https://github.com/bsm/redislock).
24- [Redis Cache](https://github.com/go-redis/cache).
25- [Rate limiting](https://github.com/go-redis/redis_rate).
26
27## Features
28
29- Redis 3 commands except QUIT, MONITOR, and SYNC.
30- Automatic connection pooling with
31  [circuit breaker](https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern) support.
32- [Pub/Sub](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#PubSub).
33- [Transactions](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-Client-TxPipeline).
34- [Pipeline](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-Client-Pipeline) and
35  [TxPipeline](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-Client-TxPipeline).
36- [Scripting](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Script).
37- [Timeouts](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Options).
38- [Redis Sentinel](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#NewFailoverClient).
39- [Redis Cluster](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#NewClusterClient).
40- [Cluster of Redis Servers](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-NewClusterClient--ManualSetup)
41  without using cluster mode and Redis Sentinel.
42- [Ring](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#NewRing).
43- [Instrumentation](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#ex-package--Instrumentation).
44
45## Installation
46
47go-redis supports 2 last Go versions and requires a Go version with
48[modules](https://github.com/golang/go/wiki/Modules) support. So make sure to initialize a Go
49module:
50
51```shell
52go mod init github.com/my/repo
53```
54
55And then install go-redis/v8 (note _v8_ in the import; omitting it is a popular mistake):
56
57```shell
58go get github.com/go-redis/redis/v8
59```
60
61## Quickstart
62
63```go
64import (
65    "context"
66    "github.com/go-redis/redis/v8"
67)
68
69var ctx = context.Background()
70
71func ExampleClient() {
72    rdb := redis.NewClient(&redis.Options{
73        Addr:     "localhost:6379",
74        Password: "", // no password set
75        DB:       0,  // use default DB
76    })
77
78    err := rdb.Set(ctx, "key", "value", 0).Err()
79    if err != nil {
80        panic(err)
81    }
82
83    val, err := rdb.Get(ctx, "key").Result()
84    if err != nil {
85        panic(err)
86    }
87    fmt.Println("key", val)
88
89    val2, err := rdb.Get(ctx, "key2").Result()
90    if err == redis.Nil {
91        fmt.Println("key2 does not exist")
92    } else if err != nil {
93        panic(err)
94    } else {
95        fmt.Println("key2", val2)
96    }
97    // Output: key value
98    // key2 does not exist
99}
100```
101
102## Look and feel
103
104Some corner cases:
105
106```go
107// SET key value EX 10 NX
108set, err := rdb.SetNX(ctx, "key", "value", 10*time.Second).Result()
109
110// SET key value keepttl NX
111set, err := rdb.SetNX(ctx, "key", "value", redis.KeepTTL).Result()
112
113// SORT list LIMIT 0 2 ASC
114vals, err := rdb.Sort(ctx, "list", &redis.Sort{Offset: 0, Count: 2, Order: "ASC"}).Result()
115
116// ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
117vals, err := rdb.ZRangeByScoreWithScores(ctx, "zset", &redis.ZRangeBy{
118    Min: "-inf",
119    Max: "+inf",
120    Offset: 0,
121    Count: 2,
122}).Result()
123
124// ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
125vals, err := rdb.ZInterStore(ctx, "out", &redis.ZStore{
126    Keys: []string{"zset1", "zset2"},
127    Weights: []int64{2, 3}
128}).Result()
129
130// EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello"
131vals, err := rdb.Eval(ctx, "return {KEYS[1],ARGV[1]}", []string{"key"}, "hello").Result()
132
133// custom command
134res, err := rdb.Do(ctx, "set", "key", "value").Result()
135```
136
137## Run the test
138
139go-redis will start a redis-server and run the test cases.
140
141The paths of redis-server bin file and redis config file are definded in `main_test.go`:
142
143```
144var (
145	redisServerBin, _  = filepath.Abs(filepath.Join("testdata", "redis", "src", "redis-server"))
146	redisServerConf, _ = filepath.Abs(filepath.Join("testdata", "redis", "redis.conf"))
147)
148```
149
150For local testing, you can change the variables to refer to your local files, or create a soft link
151to the corresponding folder for redis-server and copy the config file to `testdata/redis/`:
152
153```
154ln -s /usr/bin/redis-server ./go-redis/testdata/redis/src
155cp ./go-redis/testdata/redis.conf ./go-redis/testdata/redis/
156```
157
158Lastly, run:
159
160```
161go test
162```
163
164## See also
165
166- [Fast and flexible HTTP router](https://github.com/vmihailenco/treemux)
167- [Golang PostgreSQL ORM](https://github.com/go-pg/pg)
168- [Golang msgpack](https://github.com/vmihailenco/msgpack)
169- [Golang message task queue](https://github.com/vmihailenco/taskq)
170