refactor: upgrade to Go 1.26 and modernize code idioms
- Replace `interface{}` with `any` type alias across all packages
- Use built-in `min()`/`max()` for parameter clamping
- Use `slices.SortFunc`, `slices.Min`, `slices.Max` for cleaner code
- Use `strings.Cut()` for simpler string parsing in auth middleware
- Use `errors.Is()` for proper error comparison in handlers
- Update dependencies: golang.org/x/image 0.37.0 -> 0.38.0
- Add Wire code generation guidelines to ARCHITECTURE.md
- Disable Go cache in CI build workflow
This commit is contained in:
14
internal/cache/cache.go
vendored
14
internal/cache/cache.go
vendored
@@ -17,9 +17,9 @@ var ErrKeyNotFound = errors.New("key not found")
|
||||
// Cache 缓存接口
|
||||
type Cache interface {
|
||||
// Set 设置缓存值,支持TTL
|
||||
Set(key string, value interface{}, ttl time.Duration)
|
||||
Set(key string, value any, ttl time.Duration)
|
||||
// Get 获取缓存值
|
||||
Get(key string) (interface{}, bool)
|
||||
Get(key string) (any, bool)
|
||||
// Delete 删除缓存
|
||||
Delete(key string)
|
||||
// DeleteByPrefix 根据前缀删除缓存
|
||||
@@ -35,13 +35,13 @@ type Cache interface {
|
||||
|
||||
// ==================== Hash 操作 ====================
|
||||
// HSet 设置 Hash 字段
|
||||
HSet(ctx context.Context, key string, field string, value interface{}) error
|
||||
HSet(ctx context.Context, key string, field string, value any) error
|
||||
// HMSet 批量设置 Hash 字段
|
||||
HMSet(ctx context.Context, key string, values map[string]interface{}) error
|
||||
HMSet(ctx context.Context, key string, values map[string]any) error
|
||||
// HGet 获取 Hash 字段值
|
||||
HGet(ctx context.Context, key string, field string) (string, error)
|
||||
// HMGet 批量获取 Hash 字段值
|
||||
HMGet(ctx context.Context, key string, fields ...string) ([]interface{}, error)
|
||||
HMGet(ctx context.Context, key string, fields ...string) ([]any, error)
|
||||
// HGetAll 获取 Hash 所有字段
|
||||
HGetAll(ctx context.Context, key string) (map[string]string, error)
|
||||
// HDel 删除 Hash 字段
|
||||
@@ -59,7 +59,7 @@ type Cache interface {
|
||||
// ZReplaceSortedSet 原子替换整个 ZSET(先删后批量写入)
|
||||
ZReplaceSortedSet(ctx context.Context, key string, members []redis.Z) error
|
||||
// ZRem 删除 Sorted Set 成员
|
||||
ZRem(ctx context.Context, key string, members ...interface{}) error
|
||||
ZRem(ctx context.Context, key string, members ...any) error
|
||||
// ZCard 获取 Sorted Set 成员数量
|
||||
ZCard(ctx context.Context, key string) (int64, error)
|
||||
|
||||
@@ -145,7 +145,7 @@ func normalizePrefix(prefix string) string {
|
||||
return cmp.Or(settings.KeyPrefix, "") + ":" + prefix
|
||||
}
|
||||
|
||||
func SetWithJitter(c Cache, key string, value interface{}, ttl time.Duration, jitterRatio float64) {
|
||||
func SetWithJitter(c Cache, key string, value any, ttl time.Duration, jitterRatio float64) {
|
||||
if !settings.Enabled {
|
||||
return
|
||||
}
|
||||
|
||||
12
internal/cache/redis_cache.go
vendored
12
internal/cache/redis_cache.go
vendored
@@ -26,7 +26,7 @@ func NewRedisCache(client *redisPkg.Client) *RedisCache {
|
||||
}
|
||||
|
||||
// Set 设置缓存值
|
||||
func (c *RedisCache) Set(key string, value interface{}, ttl time.Duration) {
|
||||
func (c *RedisCache) Set(key string, value any, ttl time.Duration) {
|
||||
key = normalizeKey(key)
|
||||
// 将值序列化为JSON
|
||||
data, err := json.Marshal(value)
|
||||
@@ -49,7 +49,7 @@ func (c *RedisCache) Set(key string, value interface{}, ttl time.Duration) {
|
||||
}
|
||||
|
||||
// Get 获取缓存值
|
||||
func (c *RedisCache) Get(key string) (interface{}, bool) {
|
||||
func (c *RedisCache) Get(key string) (any, bool) {
|
||||
key = normalizeKey(key)
|
||||
data, err := c.client.Get(c.ctx, key)
|
||||
if err != nil {
|
||||
@@ -164,13 +164,13 @@ func (c *RedisCache) IncrementBy(key string, value int64) int64 {
|
||||
// ==================== RedisCache Hash 操作 ====================
|
||||
|
||||
// HSet 设置 Hash 字段
|
||||
func (c *RedisCache) HSet(ctx context.Context, key string, field string, value interface{}) error {
|
||||
func (c *RedisCache) HSet(ctx context.Context, key string, field string, value any) error {
|
||||
key = normalizeKey(key)
|
||||
return c.client.HSet(ctx, key, field, value)
|
||||
}
|
||||
|
||||
// HMSet 批量设置 Hash 字段
|
||||
func (c *RedisCache) HMSet(ctx context.Context, key string, values map[string]interface{}) error {
|
||||
func (c *RedisCache) HMSet(ctx context.Context, key string, values map[string]any) error {
|
||||
key = normalizeKey(key)
|
||||
return c.client.HMSet(ctx, key, values)
|
||||
}
|
||||
@@ -182,7 +182,7 @@ func (c *RedisCache) HGet(ctx context.Context, key string, field string) (string
|
||||
}
|
||||
|
||||
// HMGet 批量获取 Hash 字段值
|
||||
func (c *RedisCache) HMGet(ctx context.Context, key string, fields ...string) ([]interface{}, error) {
|
||||
func (c *RedisCache) HMGet(ctx context.Context, key string, fields ...string) ([]any, error) {
|
||||
key = normalizeKey(key)
|
||||
return c.client.HMGet(ctx, key, fields...)
|
||||
}
|
||||
@@ -239,7 +239,7 @@ func (c *RedisCache) ZReplaceSortedSet(ctx context.Context, key string, members
|
||||
}
|
||||
|
||||
// ZRem 删除 Sorted Set 成员
|
||||
func (c *RedisCache) ZRem(ctx context.Context, key string, members ...interface{}) error {
|
||||
func (c *RedisCache) ZRem(ctx context.Context, key string, members ...any) error {
|
||||
key = normalizeKey(key)
|
||||
return c.client.ZRem(ctx, key, members...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user