[Awesome Go] Use go-redis to access Redis Type-safe
Redis client for Golang
go-redis is a Type-safe Redis client for Golang.
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
Features
-
Redis 3 commands except QUIT, MONITOR, and SYNC.
-
Automatic connection pooling with circuit breaker support.
-
Pub/Sub.
-
Transactions.
-
Pipeline and TxPipeline.
-
Scripting.
-
Timeouts.
-
Redis Sentinel.
-
Redis Cluster.
-
Cluster of Redis Servers without using cluster mode and Redis Sentinel.
-
Ring.
-
Instrumentation.
Installation
go-redis supports 2 last Go versions and requires a Go version with modules support. So make sure to initialize a Go module:
1 | go mod init github.com/my/repo |
s
And then install go-redis/v8
(note v8 in the import; omitting it is a popular mistake):
1 | go get -u github.com/go-redis/redis/v8 |
Quickstart
1 | import ( |
Look and feel
Some corner cases:
1 | // SET key value EX 10 NX |
References
[1] go-redis/redis: Type-safe Redis client for Golang - https://github.com/go-redis/redis
[2] Redis client - Type-safe Redis client for Go - https://redis.uptrace.dev/
[3] redis · pkg.go.dev - https://pkg.go.dev/github.com/go-redis/redis/v8