modulepackage
0.0.0-20190714061509-7d5ca20d0820
Repository: https://github.com/alauda/go-redis-client.git
Documentation: pkg.go.dev
# README
alauda/go-redis-client
Introduction
alauda/go-redis-client is a secondary encapsulation on the official redis package. It is suitable for container environment. It can automatically obtain parameters from container environment variables and mounted configuration files and create redis-client instances. Convenient for developers
Usage
Installation and use in programs
1. For Go developer,you need to install package
go get github.com/alauda/go-redis-client
2. create redis-client in your programs
There are four ways to create redis-client based on security, operations and maintenance requirements
- Configuring parameters in programs and create redisclient
//This method is mainly used in the test environment. It is not recommended in the production environment.
import "redis" github.com/alauda/go-redis-client
func main() {
// check options.go for more details
opts := redis.RedisClientOptions{
Type: redis.ClientNormal,
Hosts: []string{"localhost:6379"},
Password: "123456",
Database: 0,
}
client := redis.NewRedisClient(opts)
if err := client.Ping().Err(); err != nil {
panic(err)
}
// Using cluster mode
clusterOpts := redis.RedisClientOptions{
Type: redis.ClientCluster,
Hosts: []string{"localhost:7000","localhost:7001","localhost:7002"},
Password: "123456",
Database: 0,
// all keys with a prefix
KeyPrefix: "my-app:",
}
clusterClient := redis.NewRedisClient(clusterOpts)
if err := clusterClient.Ping().Err(); err != nil {
panic(err)
}
}
- Getting parameters from environment variables to create redisclient
import redis "github.com/alauda/go-redis-client"
func main(){
//RWType:
// OnlyWrite
// OnlyWrite
// ReadAndWrite
r:= redis.OnlyRead
client,err:= redis.AutoConfigRedisClientInEnv(r)
if err!{
panic(err)
}
}
- Get parameters from the configuration file to create redisclient
import redis "github.com/alauda/go-redis-client"
func main(){
//RWType:
// OnlyWrite
// OnlyWrite
// ReadAndWrite
r:= redis.OnlyRead
client,err:= redis.AutoConfigRedisClientInVolume(r)
if err!{
panic(err)
}
}
- Get parameters from configuration files and environment variables to create redisclient Priority: Environment Variables > Configuration files
import redis "github.com/alauda/go-redis-client"
func main(){
//RWType:
// OnlyWrite
// OnlyWrite
// ReadAndWrite
r:= redis.OnlyRead
client,err:= redis.AutoConfigRedisClient(r)
if err!{
panic(err)
}
}
Configuration customization
1 For container maintainers, you need to configure Pod to inject the necessary environment variables
apiVersion: v1
...
spec:
volumes:
- name: redis-secret
secret:
secretName: redis-secret
containers:
....
volumeMounts:
- name: redis-secret
mountPath: "/etc/paas" # it must be this value
readOnly: true
env:
- name: REDIS_TYPE_READER # Injected redis variables must have ENV_PREFIX when ENV_PREFIX is not null
value: "cluster"
- name: REDIS_HOST_READER
value: "1.1.1.1,1.1.1.2,1.1.1.3"
- name: REDIS_PORT_READER
value: "26379 26379 26379"
- name: REDIS_DB_NAME_READER
value: '0'
- name: REDIS_DB_PASSWORD_READER
value: "aiyijing" # redis:passwd
- name: REDIS_MAX_CONNECTIONS_READER
value: "32"
- name: REDIS_KEY_PREFIX_READER
value: "aiyijing_"
- name: REDIS_SKIP_FULL_COVER_CHECK_READER
value: "false"
- name: REDIS_TIMEOUT_READER
value: '5'
2 Configure secret and mount
- For Example:
Secret Config info
apiVersion: v1
kind: Secret
metadata:
name: redis-secret
type: Opaque
data:
# only support toml
redis.toml: `base64`
- config.toml text
If you need to configure a WRITER cluster, you can change the READER suffix to WRITER
# normal and cluster
REDIS_TYPE_READER="cluster"
# ip,Separation with commas
REDIS_HOST_READER=["10.0.129.115","10.0.128.150","10.0.128.89"]
# port,Separation with commas,len(HOST)===len(PORT)
REDIS_PORT_READER=["26379","26379","26379"]
# redis's DB name,cluster mode is ignored.
REDIS_DB_NAME_READER=0
# passwd
REDIS_DB_PASSWORD_READER="alauda_redis_passwd"
# max connection , default:32
REDIS_MAX_CONNECTIONS_READER=32
# redis key's prefix
REDIS_KEY_PREFIX_READER="alauda_redis_passwd"
# Used to shield CONFIG commands,set true,Only the cluster mode has this variable.
REDIS_SKIP_FULL_COVER_CHECK_READER=false
# redis timeout default:5 sec
REDIS_TIMEOUT_READER=5
Refer to Detailed
# Packages
No description provided by the author
# Functions
AutoConfigRedisClient merges configuration files and environment variables to create redisclient.
AutoConfigRedisClientFromEnv create redisclient using purely environment variables parameters.
AutoConfigRedisClientFromVolume create redisclient using parameters in the configuration file.
NewClient Initiates a new client.
# Constants
ClientCluster for official redis cluster.
ClientNormal for standard instance client.
OnlyRead serves as a search suffix for configuration parameters.
OnlyWrite serves as a search suffix for configuration parameters.
ReadAndWrite serves as a search suffix for configuration parameters.
RedisNil means nil reply, .e.g.
# Variables
ErrNotImplemented not implemented error.
# Interfaces
No description provided by the author
Commander an interface containing all methods.
Decremeter interface to decrement.
Expirer interface for expire methods.
Getter interface for getting key commands.
Hasher interface for hashtable commands.
Incrementer interface to increment.
No description provided by the author
Pinger pinger interface for redis clients.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Setter interface for setting key commands.
No description provided by the author
No description provided by the author