175 lines
4.9 KiB
Go
175 lines
4.9 KiB
Go
package utils
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"time"
|
||
|
||
"github.com/go-redis/redis/v8"
|
||
)
|
||
|
||
// RedisUtilsTwo Redis工具类
|
||
type RedisUtilsTwo struct {
|
||
client *redis.Client
|
||
ctx context.Context
|
||
}
|
||
|
||
var RedisTwo *RedisUtilsTwo // 全局Redis实例(库7)
|
||
var RedisTwoForParseFormData *RedisUtilsTwo // 全局Redis实例(专门用于ParseFormData函数,库13)
|
||
|
||
// InitRedisTwo 初始化第二个Redis连接(库7)
|
||
func InitRedisTwo() error {
|
||
RedisTwo = &RedisUtilsTwo{
|
||
client: redis.NewClient(&redis.Options{
|
||
Addr: "36.212.20.113:7963",
|
||
Password: "j8nZ4jra2E",
|
||
DB: 7,
|
||
// 连接池配置
|
||
PoolSize: 10, // 最大连接数
|
||
MinIdleConns: 5, // 最小空闲连接数
|
||
}),
|
||
ctx: context.Background(),
|
||
}
|
||
|
||
// 测试连接
|
||
_, err := RedisTwo.client.Ping(RedisTwo.ctx).Result()
|
||
if err != nil {
|
||
return fmt.Errorf("连接Redis失败: %v", err)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// InitRedisTwoForParseFormData 初始化专门用于ParseFormData函数的Redis连接(库13)
|
||
func InitRedisTwoForParseFormData() error {
|
||
RedisTwoForParseFormData = &RedisUtilsTwo{
|
||
client: redis.NewClient(&redis.Options{
|
||
Addr: "36.212.12.92:6379", // 为ParseFormData函数使用的Redis地址
|
||
Password: "long6166@@",
|
||
DB: 13,
|
||
// 连接池配置
|
||
PoolSize: 50, // 最大连接数,根据实际并发情况调整
|
||
MinIdleConns: 10, // 最小空闲连接数
|
||
MaxRetries: 3, // 最大重试次数
|
||
DialTimeout: 5 * time.Second, // 拨号超时时间
|
||
ReadTimeout: 3 * time.Second, // 读取超时时间
|
||
WriteTimeout: 3 * time.Second, // 写入超时时间
|
||
PoolTimeout: 4 * time.Second, // 连接池超时时间
|
||
}),
|
||
ctx: context.Background(),
|
||
}
|
||
|
||
// 测试连接
|
||
_, err := RedisTwoForParseFormData.client.Ping(RedisTwoForParseFormData.ctx).Result()
|
||
if err != nil {
|
||
return fmt.Errorf("连接Redis失败: %v", err)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// NewRedisUtilsTwo 创建一个新的Redis连接实例
|
||
func NewRedisUtilsTwo(db int) (*RedisUtilsTwo, error) {
|
||
redisUtil := &RedisUtilsTwo{
|
||
client: redis.NewClient(&redis.Options{
|
||
Addr: "36.212.20.113:7963",
|
||
Password: "j8nZ4jra2E",
|
||
DB: db,
|
||
}),
|
||
ctx: context.Background(),
|
||
}
|
||
|
||
// 测试连接
|
||
_, err := redisUtil.client.Ping(redisUtil.ctx).Result()
|
||
if err != nil {
|
||
return nil, fmt.Errorf("连接Redis失败: %v", err)
|
||
}
|
||
|
||
return redisUtil, nil
|
||
}
|
||
|
||
// NewRedisUtilsTwoForParseFormData 为ParseFormData函数创建一个新的Redis连接实例(使用不同的Redis地址)
|
||
func NewRedisUtilsTwoForParseFormData(db int) (*RedisUtilsTwo, error) {
|
||
redisUtil := &RedisUtilsTwo{
|
||
client: redis.NewClient(&redis.Options{
|
||
Addr: "36.212.12.92:6379", // 为ParseFormData函数使用的Redis地址
|
||
Password: "long6166@@",
|
||
DB: db,
|
||
}),
|
||
ctx: context.Background(),
|
||
}
|
||
|
||
// 测试连接
|
||
_, err := redisUtil.client.Ping(redisUtil.ctx).Result()
|
||
if err != nil {
|
||
return nil, fmt.Errorf("连接Redis失败: %v", err)
|
||
}
|
||
|
||
return redisUtil, nil
|
||
}
|
||
|
||
// GetClient 获取底层Redis客户端
|
||
func (r *RedisUtilsTwo) GetClient() *redis.Client {
|
||
return r.client
|
||
}
|
||
|
||
// GetContext 获取上下文
|
||
func (r *RedisUtilsTwo) GetContext() context.Context {
|
||
return r.ctx
|
||
}
|
||
|
||
// HGet 从Redis Hash中获取字段值
|
||
func (r *RedisUtilsTwo) HGet(key, field string) (string, error) {
|
||
return r.client.HGet(r.ctx, key, field).Result()
|
||
}
|
||
|
||
// HGetAll 获取Hash中的所有字段和值
|
||
func (r *RedisUtilsTwo) HGetAll(key string) (map[string]string, error) {
|
||
return r.client.HGetAll(r.ctx, key).Result()
|
||
}
|
||
|
||
// HSet 设置Hash中的字段值
|
||
func (r *RedisUtilsTwo) HSet(key, field string, value interface{}) error {
|
||
return r.client.HSet(r.ctx, key, field, value).Err()
|
||
}
|
||
|
||
// Exists 检查key是否存在
|
||
func (r *RedisUtilsTwo) Exists(key string) (int64, error) {
|
||
return r.client.Exists(r.ctx, key).Result()
|
||
}
|
||
|
||
// Get 获取key的值
|
||
func (r *RedisUtilsTwo) Get(key string) (string, error) {
|
||
return r.client.Get(r.ctx, key).Result()
|
||
}
|
||
|
||
// Set 设置key的值
|
||
func (r *RedisUtilsTwo) Set(key string, value interface{}, expiration time.Duration) error {
|
||
return r.client.Set(r.ctx, key, value, expiration).Err()
|
||
}
|
||
|
||
// Expire 设置key的过期时间
|
||
func (r *RedisUtilsTwo) Expire(key string, expiration time.Duration) error {
|
||
return r.client.Expire(r.ctx, key, expiration).Err()
|
||
}
|
||
|
||
// HIncrBy 对Hash中的字段值进行增量操作
|
||
func (r *RedisUtilsTwo) HIncrBy(key, field string, value int64) (int64, error) {
|
||
return r.client.HIncrBy(r.ctx, key, field, value).Result()
|
||
}
|
||
|
||
// LIndex 通过索引获取List中的元素
|
||
func (r *RedisUtilsTwo) LIndex(key string, index int64) (string, error) {
|
||
return r.client.LIndex(r.ctx, key, index).Result()
|
||
}
|
||
|
||
// LRange 获取List中指定范围内的元素
|
||
func (r *RedisUtilsTwo) LRange(key string, start, stop int64) ([]string, error) {
|
||
return r.client.LRange(r.ctx, key, start, stop).Result()
|
||
}
|
||
|
||
// Close 关闭Redis连接
|
||
func (r *RedisUtilsTwo) Close() error {
|
||
return r.client.Close()
|
||
}
|