-
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
1 parent
55d750d
commit 832bfc1
Showing
16 changed files
with
197 additions
and
133 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,57 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v8" | ||
|
||
"github.com/agflow/tools/log" | ||
) | ||
|
||
// Client is a wrapper of a redis client | ||
type Client struct { | ||
Redis *redis.Client | ||
} | ||
|
||
// NewClient returns a verified redis client | ||
func NewClient(address, password string) *redis.Client { | ||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: address, | ||
Password: password, | ||
DB: 0, // use default DB | ||
}) | ||
if rdb == nil { | ||
log.Warnf("failed to initialize redis client") | ||
return rdb | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
defer cancel() | ||
|
||
if err := rdb.Ping(ctx).Err(); err != nil { | ||
log.Warnf("can't ping redis, %v", err) | ||
} | ||
return rdb | ||
} | ||
|
||
// New returns a new redis client | ||
func New(address, password string) *Client { | ||
redisClient := NewClient(address, password) | ||
return &Client{Redis: redisClient} | ||
} | ||
|
||
// Get gets the value stored on `key` | ||
func (c *Client) Get(ctx context.Context, key string) ([]byte, error) { | ||
return c.Redis.Get(ctx, key).Bytes() | ||
} | ||
|
||
// Set sets `value` on `key` with a `timeout`` | ||
func (c *Client) Set( | ||
ctx context.Context, | ||
key string, | ||
value interface{}, | ||
timeout time.Duration, | ||
) error { | ||
return c.Redis.Set(ctx, key, value, timeout).Err() | ||
} |
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 cache | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// Service declares the interface of a cache service | ||
type Service interface { | ||
Get(context.Context, string) ([]byte, error) | ||
Set(context.Context, string, interface{}, time.Duration) 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
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,18 @@ | ||
package security | ||
|
||
import ( | ||
"crypto/sha512" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Hash hashes `vs` | ||
func Hash(vs interface{}) (string, error) { | ||
h := sha512.New() | ||
r, err := json.Marshal(vs) | ||
if err != nil { | ||
return "", err | ||
} | ||
h.Write(r) | ||
return fmt.Sprintf("%x", h.Sum(nil)), nil | ||
} |
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
Oops, something went wrong.