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

feat: remove the cache implementation related to redis v2.6.0 #714

Open
wants to merge 8 commits into
base: v2
Choose a base branch
from

Conversation

houseme
Copy link
Collaborator

@houseme houseme commented Aug 25, 2023

#661

先发布 rc 版本,正式版之前,完全移除 redis 的依赖,只留下 memcache 的当做示例及默认实现。 @silenceper

完全移除redis相关的代码,建议用户参照示例代码实现redis的缓存功能。

如下为 redis V9的示例代码:

package cache

import (
	"context"
	"time"

	"github.com/redis/go-redis/v9"
)

// Redis .redis cache
type Redis struct {
	ctx  context.Context
	conn redis.UniversalClient
}

// RedisOpts redis 连接属性
type RedisOpts struct {
	Host        string `yml:"host" json:"host"`
	Password    string `yml:"password" json:"password"`
	Database    int    `yml:"database" json:"database"`
	MaxIdle     int    `yml:"max_idle" json:"max_idle"`
	MaxActive   int    `yml:"max_active" json:"max_active"`
	IdleTimeout int    `yml:"idle_timeout" json:"idle_timeout"` // second
}

// NewRedis 实例化
func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
	conn := redis.NewUniversalClient(&redis.UniversalOptions{
		Addrs:           []string{opts.Host},
		DB:              opts.Database,
		Password:        opts.Password,
		ConnMaxIdleTime: time.Second * time.Duration(opts.IdleTimeout),
		MinIdleConns:    opts.MaxIdle,
	})
	return &Redis{ctx: ctx, conn: conn}
}

// SetConn 设置 conn
func (r *Redis) SetConn(conn redis.UniversalClient) {
	r.conn = conn
}

// SetRedisCtx 设置 redis ctx 参数
func (r *Redis) SetRedisCtx(ctx context.Context) {
	r.ctx = ctx
}

// Get 获取一个值
func (r *Redis) Get(key string) interface{} {
	return r.GetContext(r.ctx, key)
}

// GetContext 获取一个值
func (r *Redis) GetContext(ctx context.Context, key string) interface{} {
	result := r.conn.Get(ctx, key)
	if result.Err() != nil {
		return ""
	}
	return result.Val()
}

// Set 设置一个值
func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error {
	return r.SetContext(r.ctx, key, val, timeout)
}

// SetContext 设置一个值
func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
	return r.conn.SetEx(ctx, key, val, timeout).Err()
}

// IsExist 判断 key 是否存在
func (r *Redis) IsExist(key string) bool {
	return r.IsExistContext(r.ctx, key)
}

// IsExistContext 判断 key 是否存在
func (r *Redis) IsExistContext(ctx context.Context, key string) bool {
	result, _ := r.conn.Exists(ctx, key).Result()

	return result > 0
}

// Delete 删除
func (r *Redis) Delete(key string) error {
	return r.DeleteContext(r.ctx, key)
}

// DeleteContext 删除
func (r *Redis) DeleteContext(ctx context.Context, key string) error {
	return r.conn.Del(ctx, key).Err()
}

@houseme houseme changed the title feat: redis deprecated feat: remove the cache implementation related to redis Sep 26, 2023
@houseme houseme changed the title feat: remove the cache implementation related to redis feat: remove the cache implementation related to redis v2.6.0 Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant