refactor: replace standard log with zap logger and optimize code patterns

- Migrate all log.Printf/log.Println calls to structured zap logging
- Use cmp.Or for cleaner default value handling
- Replace manual loops with slices.ContainsFunc/IndexFunc
- Add batch query methods (IsLikedBatch, IsFavoritedBatch) to solve N+1 problem
- Update JSON tags from omitempty to omitzero for numeric fields
- Use errgroup-style wg.Go for worker goroutines
- Remove duplicate casbin/v2 dependency (keeping v3)
- Add plans/ to gitignore
This commit is contained in:
lafay
2026-03-17 00:47:17 +08:00
parent b028f7e1d3
commit 5d6c982c9c
38 changed files with 2256 additions and 290 deletions

View File

@@ -3,10 +3,10 @@ package cache
import (
"context"
"encoding/json"
"log"
"time"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
redisPkg "carrot_bbs/internal/pkg/redis"
)
@@ -32,13 +32,19 @@ func (c *RedisCache) Set(key string, value interface{}, ttl time.Duration) {
data, err := json.Marshal(value)
if err != nil {
recordSetError()
log.Printf("[RedisCache] Failed to marshal value for key %s: %v", key, err)
zap.L().Error("Failed to marshal value for key",
zap.String("key", key),
zap.Error(err),
)
return
}
if err := c.client.Set(c.ctx, key, data, ttl); err != nil {
recordSetError()
log.Printf("[RedisCache] Failed to set key %s: %v", key, err)
zap.L().Error("Failed to set key",
zap.String("key", key),
zap.Error(err),
)
}
}
@@ -50,7 +56,10 @@ func (c *RedisCache) Get(key string) (interface{}, bool) {
if err == redis.Nil {
return nil, false
}
log.Printf("[RedisCache] Failed to get key %s: %v", key, err)
zap.L().Error("Failed to get key",
zap.String("key", key),
zap.Error(err),
)
return nil, false
}
@@ -63,7 +72,10 @@ func (c *RedisCache) Delete(key string) {
key = normalizeKey(key)
recordInvalidate()
if err := c.client.Del(c.ctx, key); err != nil {
log.Printf("[RedisCache] Failed to delete key %s: %v", key, err)
zap.L().Error("Failed to delete key",
zap.String("key", key),
zap.Error(err),
)
}
}
@@ -76,14 +88,20 @@ func (c *RedisCache) DeleteByPrefix(prefix string) {
for {
keys, nextCursor, err := rdb.Scan(c.ctx, cursor, prefix+"*", 100).Result()
if err != nil {
log.Printf("[RedisCache] Failed to scan keys with prefix %s: %v", prefix, err)
zap.L().Error("Failed to scan keys with prefix",
zap.String("prefix", prefix),
zap.Error(err),
)
return
}
if len(keys) > 0 {
recordInvalidateMultiple(int64(len(keys)))
if err := c.client.Del(c.ctx, keys...); err != nil {
log.Printf("[RedisCache] Failed to delete keys with prefix %s: %v", prefix, err)
zap.L().Error("Failed to delete keys with prefix",
zap.String("prefix", prefix),
zap.Error(err),
)
}
}
@@ -97,13 +115,15 @@ func (c *RedisCache) DeleteByPrefix(prefix string) {
// Clear 清空所有缓存
func (c *RedisCache) Clear() {
if settings.DisableFlushDB {
log.Printf("[RedisCache] Skip FlushDB because cache.disable_flushdb=true")
zap.L().Debug("Skip FlushDB because cache.disable_flushdb=true")
return
}
recordInvalidate()
rdb := c.client.GetClient()
if err := rdb.FlushDB(c.ctx).Err(); err != nil {
log.Printf("[RedisCache] Failed to clear cache: %v", err)
zap.L().Error("Failed to clear cache",
zap.Error(err),
)
}
}
@@ -112,7 +132,10 @@ func (c *RedisCache) Exists(key string) bool {
key = normalizeKey(key)
n, err := c.client.Exists(c.ctx, key)
if err != nil {
log.Printf("[RedisCache] Failed to check existence of key %s: %v", key, err)
zap.L().Error("Failed to check existence of key",
zap.String("key", key),
zap.Error(err),
)
return false
}
return n > 0
@@ -129,7 +152,10 @@ func (c *RedisCache) IncrementBy(key string, value int64) int64 {
rdb := c.client.GetClient()
result, err := rdb.IncrBy(c.ctx, key, value).Result()
if err != nil {
log.Printf("[RedisCache] Failed to increment key %s: %v", key, err)
zap.L().Error("Failed to increment key",
zap.String("key", key),
zap.Error(err),
)
return 0
}
return result