-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// RedisClient is interface for redis client calls | ||
type RedisClient interface { | ||
HashSet(ctx context.Context, key string, values interface{}) error | ||
Set(ctx context.Context, key string, value interface{}) error | ||
HGetAll(ctx context.Context, key string) ([]interface{}, error) | ||
Get(ctx context.Context, key string) (interface{}, error) | ||
Expire(ctx context.Context, key string, expiration time.Duration) error | ||
Ping(ctx context.Context) error | ||
Delete(ctx context.Context, key string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package config | ||
|
||
import "time" | ||
|
||
// RedisConfig is interface for redis config settings | ||
type RedisConfig interface { | ||
Address() string | ||
ConnectionTimeout() time.Duration | ||
MaxIdle() int | ||
IdleTimeout() time.Duration | ||
ElementTTL() time.Duration | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/gomodule/redigo/redis" | ||
|
||
"github.com/valek177/platform-common/pkg/client/cache" | ||
"github.com/valek177/platform-common/pkg/client/cache/config" | ||
) | ||
|
||
var _ cache.RedisClient = (*client)(nil) | ||
|
||
type handler func(ctx context.Context, conn redis.Conn) error | ||
|
||
type client struct { | ||
pool *redis.Pool | ||
config config.RedisConfig | ||
} | ||
|
||
// NewClient creates new redis client | ||
func NewClient(pool *redis.Pool, config config.RedisConfig) *client { | ||
return &client{ | ||
pool: pool, | ||
config: config, | ||
} | ||
} | ||
|
||
func (c *client) HashSet(ctx context.Context, key string, values interface{}) error { | ||
err := c.execute(ctx, func(_ context.Context, conn redis.Conn) error { | ||
_, err := conn.Do("HSET", redis.Args{key}.AddFlat(values)...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) Set(ctx context.Context, key string, value interface{}) error { | ||
err := c.execute(ctx, func(_ context.Context, conn redis.Conn) error { | ||
_, err := conn.Do("SET", redis.Args{key}.Add(value)...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) HGetAll(ctx context.Context, key string) ([]interface{}, error) { | ||
var values []interface{} | ||
err := c.execute(ctx, func(_ context.Context, conn redis.Conn) error { | ||
var errEx error | ||
values, errEx = redis.Values(conn.Do("HGETALL", key)) | ||
if errEx != nil { | ||
return errEx | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return values, nil | ||
} | ||
|
||
func (c *client) Get(ctx context.Context, key string) (interface{}, error) { | ||
var value interface{} | ||
err := c.execute(ctx, func(_ context.Context, conn redis.Conn) error { | ||
var errEx error | ||
value, errEx = conn.Do("GET", key) | ||
if errEx != nil { | ||
return errEx | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
func (c *client) Expire(ctx context.Context, key string, expiration time.Duration) error { | ||
err := c.execute(ctx, func(_ context.Context, conn redis.Conn) error { | ||
_, err := conn.Do("EXPIRE", key, int(expiration.Seconds())) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) Ping(ctx context.Context) error { | ||
err := c.execute(ctx, func(_ context.Context, conn redis.Conn) error { | ||
_, err := conn.Do("PING") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) Delete(ctx context.Context, key string) error { | ||
err := c.execute(ctx, func(_ context.Context, conn redis.Conn) error { | ||
_, err := conn.Do("DEL", key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) execute(ctx context.Context, handler handler) error { | ||
conn, err := c.getConnect(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
err = conn.Close() | ||
if err != nil { | ||
log.Printf("failed to close redis connection: %v\n", err) | ||
} | ||
}() | ||
|
||
err = handler(ctx, conn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) getConnect(ctx context.Context) (redis.Conn, error) { | ||
getConnTimeoutCtx, cancel := context.WithTimeout(ctx, c.config.ConnectionTimeout()) | ||
defer cancel() | ||
|
||
conn, err := c.pool.GetContext(getConnTimeoutCtx) | ||
if err != nil { | ||
log.Printf("failed to get redis connection: %v\n", err) | ||
|
||
_ = conn.Close() | ||
return nil, err | ||
} | ||
|
||
return conn, nil | ||
} |