go语言搭建本地redis

本文档介绍如何使用Go语言中的redis包连接Redis服务器及集群,包括非集群和集群连接方式,并提供具体实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用包:https://github.com/go-redis/redis/tree/v3.2.29
使用方法:https://godoc.org/gopkg.in/redis.v3#pkg-examples
链接方法:redis.NewClient,里面有例子
这个包也满足集群的方式。
package lib

import (
"gopkg.in/redis.v3"
)
//no cluster
func NewRedisClient(host string, port string, password string) *redis.Client{
client := redis.NewClient(&redis.Options{
Addr: host + ":" + port,
Password: password, // no password set
DB: 0, // use default DB
})
return client
}
//cluster connection
func NewClusterRedisClient(redis_urls []string) *redis.ClusterClient{
client := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: redis_urls,//[]string{"127.0.0.1:7000","127.0.0.1:7001","127.0.0.1:7002","127.0.0.1:7003"},
})
return client
}

type ConfigRedis struct {
Production map[string][]string
Development map[string][]string
Test map[string][]string
}
func GetClusterRedisUrls()(redis_urls []string) {
var config_redis ConfigRedis
redis_url_file_path,_ := filepath.Abs("config/redis_cluster.yml")
redis_url_content,_ := ioutil.ReadFile(redis_url_file_path)
yaml.Unmarshal(redis_url_content,&config_redis)
switch CurrentEnv{
case "dev":
redis_urls = config_redis.Development["urls"]//如果是单个就去第一个["urls"][0]
case "prod":
redis_urls = config_redis.Production["urls"]
default:
redis_urls = config_redis.Production["urls"]
}
return redis_urls
}

#使用方法文档都有。
ClusterRedis:= NewClusterRedisClient(GetClusterRedisUrls())
ip_fc:=ClusterRedis.HGetAll("fc:"+bl.Bidrequest.Udevice.Ip)
ip_fc_map := common.ToMap(ip_fc)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值