mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
4.1 KiB
129 lines
4.1 KiB
6 years ago
|
# Redis client for Golang
|
||
|
|
||
|
[![Build Status](https://travis-ci.org/go-redis/redis.png?branch=master)](https://travis-ci.org/go-redis/redis)
|
||
|
[![GoDoc](https://godoc.org/github.com/go-redis/redis?status.svg)](https://godoc.org/github.com/go-redis/redis)
|
||
|
[![Airbrake](https://img.shields.io/badge/kudos-airbrake.io-orange.svg)](https://airbrake.io)
|
||
|
|
||
|
Supports:
|
||
|
|
||
|
- Redis 3 commands except QUIT, MONITOR, SLOWLOG and SYNC.
|
||
|
- Automatic connection pooling with [circuit breaker](https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern) support.
|
||
|
- [Pub/Sub](https://godoc.org/github.com/go-redis/redis#PubSub).
|
||
4 years ago
|
- [Transactions](https://godoc.org/github.com/go-redis/redis#example-Client-TxPipeline).
|
||
6 years ago
|
- [Pipeline](https://godoc.org/github.com/go-redis/redis#example-Client-Pipeline) and [TxPipeline](https://godoc.org/github.com/go-redis/redis#example-Client-TxPipeline).
|
||
|
- [Scripting](https://godoc.org/github.com/go-redis/redis#Script).
|
||
|
- [Timeouts](https://godoc.org/github.com/go-redis/redis#Options).
|
||
|
- [Redis Sentinel](https://godoc.org/github.com/go-redis/redis#NewFailoverClient).
|
||
|
- [Redis Cluster](https://godoc.org/github.com/go-redis/redis#NewClusterClient).
|
||
|
- [Cluster of Redis Servers](https://godoc.org/github.com/go-redis/redis#example-NewClusterClient--ManualSetup) without using cluster mode and Redis Sentinel.
|
||
|
- [Ring](https://godoc.org/github.com/go-redis/redis#NewRing).
|
||
|
- [Instrumentation](https://godoc.org/github.com/go-redis/redis#ex-package--Instrumentation).
|
||
|
- [Cache friendly](https://github.com/go-redis/cache).
|
||
|
- [Rate limiting](https://github.com/go-redis/redis_rate).
|
||
4 years ago
|
- [Distributed Locks](https://github.com/bsm/redislock).
|
||
6 years ago
|
|
||
|
API docs: https://godoc.org/github.com/go-redis/redis.
|
||
|
Examples: https://godoc.org/github.com/go-redis/redis#pkg-examples.
|
||
|
|
||
|
## Installation
|
||
|
|
||
4 years ago
|
go-redis requires a Go version with [Modules](https://github.com/golang/go/wiki/Modules) support and uses import versioning. So please make sure to initialize a Go module before installing go-redis:
|
||
6 years ago
|
|
||
4 years ago
|
``` shell
|
||
|
go mod init github.com/my/repo
|
||
|
go get github.com/go-redis/redis/v7
|
||
6 years ago
|
```
|
||
|
|
||
|
Import:
|
||
|
|
||
4 years ago
|
``` go
|
||
|
import "github.com/go-redis/redis/v7"
|
||
6 years ago
|
```
|
||
|
|
||
|
## Quickstart
|
||
|
|
||
4 years ago
|
``` go
|
||
6 years ago
|
func ExampleNewClient() {
|
||
|
client := redis.NewClient(&redis.Options{
|
||
|
Addr: "localhost:6379",
|
||
|
Password: "", // no password set
|
||
|
DB: 0, // use default DB
|
||
|
})
|
||
|
|
||
|
pong, err := client.Ping().Result()
|
||
|
fmt.Println(pong, err)
|
||
|
// Output: PONG <nil>
|
||
|
}
|
||
|
|
||
|
func ExampleClient() {
|
||
4 years ago
|
client := redis.NewClient(&redis.Options{
|
||
|
Addr: "localhost:6379",
|
||
|
Password: "", // no password set
|
||
|
DB: 0, // use default DB
|
||
|
})
|
||
6 years ago
|
err := client.Set("key", "value", 0).Err()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
val, err := client.Get("key").Result()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
fmt.Println("key", val)
|
||
|
|
||
|
val2, err := client.Get("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
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## Howto
|
||
|
|
||
|
Please go through [examples](https://godoc.org/github.com/go-redis/redis#pkg-examples) to get an idea how to use this package.
|
||
|
|
||
|
## Look and feel
|
||
|
|
||
|
Some corner cases:
|
||
|
|
||
4 years ago
|
``` go
|
||
6 years ago
|
// SET key value EX 10 NX
|
||
|
set, err := client.SetNX("key", "value", 10*time.Second).Result()
|
||
|
|
||
|
// SORT list LIMIT 0 2 ASC
|
||
4 years ago
|
vals, err := client.Sort("list", &redis.Sort{Offset: 0, Count: 2, Order: "ASC"}).Result()
|
||
6 years ago
|
|
||
|
// ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
|
||
4 years ago
|
vals, err := client.ZRangeByScoreWithScores("zset", &redis.ZRangeBy{
|
||
6 years ago
|
Min: "-inf",
|
||
|
Max: "+inf",
|
||
|
Offset: 0,
|
||
|
Count: 2,
|
||
|
}).Result()
|
||
|
|
||
|
// ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
|
||
4 years ago
|
vals, err := client.ZInterStore("out", &redis.ZStore{
|
||
|
Keys: []string{"zset1", "zset2"},
|
||
|
Weights: []int64{2, 3}
|
||
|
}).Result()
|
||
6 years ago
|
|
||
|
// EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello"
|
||
|
vals, err := client.Eval("return {KEYS[1],ARGV[1]}", []string{"key"}, "hello").Result()
|
||
|
|
||
4 years ago
|
// custom command
|
||
|
res, err := client.Do("set", "key", "value").Result()
|
||
6 years ago
|
```
|
||
|
|
||
|
## See also
|
||
|
|
||
|
- [Golang PostgreSQL ORM](https://github.com/go-pg/pg)
|
||
|
- [Golang msgpack](https://github.com/vmihailenco/msgpack)
|
||
4 years ago
|
- [Golang message task queue](https://github.com/vmihailenco/taskq)
|