Skip to content
Snippets Groups Projects
client.go 818 B
Newer Older
  • Learn to ignore specific revisions
  • package redis
    
    import (
    	"context"
    	"time"
    
    	"github.com/go-redis/redis/v8"
    
    	"code.vereign.com/gaiax/tsa/golib/errors"
    )
    
    type Client struct {
    	rdb        *redis.Client
    	defaultTTL time.Duration
    }
    
    func New(addr, user, pass string, db int, defaultTTL time.Duration) *Client {
    	rdb := redis.NewClient(&redis.Options{
    		Addr:         addr,
    		Username:     user,
    		Password:     pass,
    		DB:           db,
    		DialTimeout:  10 * time.Second,
    		ReadTimeout:  5 * time.Second,
    		WriteTimeout: 5 * time.Second,
    	})
    
    	return &Client{
    		rdb: rdb,
    	}
    }
    
    func (c *Client) Get(ctx context.Context, key string) ([]byte, error) {
    	result := c.rdb.Get(ctx, key)
    	if result.Err() != nil {
    		if result.Err() == redis.Nil {
    			return nil, errors.New(errors.NotFound)
    		}
    		return nil, result.Err()
    	}
    	return []byte(result.Val()), nil
    }