-
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.
**BugFixes** 1. [gin_logger] 分离请求日志和响应日志,并均加入X-Request-Id,用于定位请求; 2. [accept_language] 注释不必要的开发打印;
KrisYu
committed
Nov 22, 2024
1 parent
96dce1a
commit 28a35be
Showing
9 changed files
with
155 additions
and
28 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
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,82 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"github.com/go-redis/redis/v9" | ||
"time" | ||
) | ||
|
||
// NewRedis redis模式 | ||
func NewRedis(client *redis.Client, options *redis.Options) (*Redis, error) { | ||
if client == nil { | ||
client = redis.NewClient(options) | ||
} | ||
r := &Redis{ | ||
client: client, | ||
} | ||
err := r.connect() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
|
||
// Redis cache implement | ||
type Redis struct { | ||
client *redis.Client | ||
} | ||
|
||
func (*Redis) String() string { | ||
return "redis" | ||
} | ||
|
||
// connect connect test | ||
func (r *Redis) connect() error { | ||
var err error | ||
_, err = r.client.Ping(context.TODO()).Result() | ||
return err | ||
} | ||
|
||
// Get from key | ||
func (r *Redis) Get(key string) (string, error) { | ||
return r.client.Get(context.TODO(), key).Result() | ||
} | ||
|
||
// Set value with key and expire time | ||
func (r *Redis) Set(key string, val interface{}, expire int) error { | ||
return r.client.Set(context.TODO(), key, val, time.Duration(expire)*time.Second).Err() | ||
} | ||
|
||
// Del delete key in redis | ||
func (r *Redis) Del(key string) error { | ||
return r.client.Del(context.TODO(), key).Err() | ||
} | ||
|
||
// HashGet from key | ||
func (r *Redis) HashGet(hk, key string) (string, error) { | ||
return r.client.HGet(context.TODO(), hk, key).Result() | ||
} | ||
|
||
// HashDel delete key in specify redis's hashtable | ||
func (r *Redis) HashDel(hk, key string) error { | ||
return r.client.HDel(context.TODO(), hk, key).Err() | ||
} | ||
|
||
// Increase | ||
func (r *Redis) Increase(key string) error { | ||
return r.client.Incr(context.TODO(), key).Err() | ||
} | ||
|
||
func (r *Redis) Decrease(key string) error { | ||
return r.client.Decr(context.TODO(), key).Err() | ||
} | ||
|
||
// Set ttl | ||
func (r *Redis) Expire(key string, dur time.Duration) error { | ||
return r.client.Expire(context.TODO(), key, dur).Err() | ||
} | ||
|
||
// GetClient 暴露原生client | ||
func (r *Redis) GetClient() *redis.Client { | ||
return r.client | ||
} |
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,32 @@ | ||
package locker | ||
|
||
import ( | ||
"context" | ||
"github.com/go-redis/redis/v9" | ||
"time" | ||
|
||
"github.com/bsm/redislock" | ||
) | ||
|
||
// NewRedis 初始化locker, redis分布锁 | ||
func NewRedis(c *redis.Client) *Redis { | ||
return &Redis{ | ||
client: c, | ||
} | ||
} | ||
|
||
type Redis struct { | ||
client *redis.Client | ||
mutex *redislock.Client | ||
} | ||
|
||
func (Redis) String() string { | ||
return "redis" | ||
} | ||
|
||
func (r *Redis) Lock(key string, ttl int64, options *redislock.Options) (*redislock.Lock, error) { | ||
if r.mutex == nil { | ||
r.mutex = redislock.New(r.client) | ||
} | ||
return r.mutex.Obtain(context.TODO(), key, time.Duration(ttl)*time.Second, options) | ||
} |
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