Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持多个redis实例 #153

Open
ian-qiu opened this issue Oct 14, 2024 · 2 comments
Open

支持多个redis实例 #153

ian-qiu opened this issue Oct 14, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@ian-qiu
Copy link

ian-qiu commented Oct 14, 2024

貌似现在只支持一个redis实例(default),希望能支持多个。

@qloog
Copy link
Contributor

qloog commented Oct 14, 2024

目前是支持的,以增加 user-service 为例, 添加步骤如下:

1、增加多实例配置

default:
  Addr: 127.0.0.1:6379
  Password: ""
  DB: 0
  MinIdleConn: 200
  DialTimeout: 60s
  ReadTimeout: 500ms
  WriteTimeout: 500ms
  PoolSize: 100
  PoolTimeout: 240s
  EnableTrace: true
user-service:
  Addr: 127.0.0.1:6379
  Password: ""
  DB: 0
  MinIdleConn: 200
  DialTimeout: 60s
  ReadTimeout: 500ms
  WriteTimeout: 500ms
  PoolSize: 100
  PoolTimeout: 240s
  EnableTrace: true

2、实例化多个redis

internal/cache下的 redis.Init 只实例化默认redis实例,如果需要开启多实例请参考下面写法

package cache

import (
	"github.com/go-eagle/eagle/pkg/redis"
	"github.com/google/wire"
	rdb "github.com/redis/go-redis/v9"
)

// 新增
var (
        // 全局实例
	UserRedis *rdb.Client
)

// ProviderSet is cache providers.
var ProviderSet = wire.NewSet(redis.Init, NewUserCache)

// 新增
func init() {
	mgr := redis.NewRedisManager()
        // user-service 对应到配置里的新增的 section
        // 绑定到对应的全局变量上
	UserRedis, err := mgr.GetClient("user-service")
	if err != nil {
		panic(err)
	}

	_ = UserRedis
}

3、最后就可以在对应的地方进行使用了

希望可以帮到你 😄

@qloog qloog added the enhancement New feature or request label Oct 14, 2024
@ian-qiu
Copy link
Author

ian-qiu commented Oct 14, 2024

收到,感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants