feat(middleware):为登录与互动接口增加限流中间件

This commit is contained in:
tangerineyu
2026-03-27 16:09:01 +08:00
parent 145bf3fa86
commit e89d5aca5d
3 changed files with 109 additions and 9 deletions

View File

@@ -77,3 +77,20 @@ func (c *Client) Unlock(ctx context.Context, key string, token string) error {
_, err := unlockScript.Run(ctx, c.rdb, []string{key}, token).Result()
return err
}
func (c *Client) IncrementWithExpire(ctx context.Context, key string, expire time.Duration) (int64, error) {
if c == nil || c.rdb == nil {
return 0, nil
}
count, err := c.rdb.Incr(ctx, key).Result()
if err != nil {
return 0, err
}
if count == 1 {
err = c.rdb.Expire(ctx, key, expire).Err()
if err != nil {
return 0, err
}
}
return count, nil
}