refactor: cleanup unused code and simplify internal packages
This commit performs a significant cleanup of the codebase by removing unused functions, methods, and entire files across various internal modules. This reduces technical debt and simplifies the project structure. Key changes include: - **cache**: Removed unused cache key generators and metrics snapshots. - **dto**: Removed redundant converter functions and segment creation helpers. - **middleware**: Deleted the unused `logger.go` middleware and simplified `ratelimit.go` and `casbin.go`. - **model**: Removed unused ID helpers, database closing functions, and batch decryption logic. - **pkg**: Cleaned up unused utility functions in `circuitbreaker`, `crypto`, `cursor`, `hook`, and `utils`. - **service**: Deleted `account_cleanup_service.go` and removed unused helper functions in `log_cleanup_service.go` and `sensitive_service.go`. - **repository**: Removed unused private loading methods in `comment_repo.go`.
This commit is contained in:
67
internal/cache/layered_cache.go
vendored
67
internal/cache/layered_cache.go
vendored
@@ -2,13 +2,11 @@
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
redislib "github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
|
||||
redisPkg "with_you/internal/pkg/redis"
|
||||
)
|
||||
@@ -412,59 +410,6 @@ func (c *LayeredCache) GetRedisClient() *redisPkg.Client {
|
||||
return c.redis.GetRedisClient()
|
||||
}
|
||||
|
||||
func GetTypedLayered[T any](c *LayeredCache, key string) (T, bool) {
|
||||
var zero T
|
||||
if !c.enabled {
|
||||
return zero, false
|
||||
}
|
||||
|
||||
raw, ok := c.Get(key)
|
||||
if !ok {
|
||||
return zero, false
|
||||
}
|
||||
|
||||
if typed, ok := raw.(T); ok {
|
||||
return typed, true
|
||||
}
|
||||
|
||||
if str, ok := raw.(string); ok {
|
||||
var out T
|
||||
if err := json.Unmarshal([]byte(str), &out); err != nil {
|
||||
return zero, false
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
|
||||
if data, err := json.Marshal(raw); err == nil {
|
||||
var out T
|
||||
if err := json.Unmarshal(data, &out); err != nil {
|
||||
return zero, false
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
|
||||
return zero, false
|
||||
}
|
||||
|
||||
func GetOrLoadLayered[T any](
|
||||
c *LayeredCache,
|
||||
key string,
|
||||
ttl time.Duration,
|
||||
loader func() (T, error),
|
||||
) (T, error) {
|
||||
if cached, ok := GetTypedLayered[T](c, key); ok {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
loaded, err := loader()
|
||||
if err != nil {
|
||||
var zero T
|
||||
return zero, err
|
||||
}
|
||||
|
||||
c.Set(key, loaded, ttl)
|
||||
return loaded, nil
|
||||
}
|
||||
|
||||
func (c *LayeredCache) InvalidateUserCache(userID uint) {
|
||||
c.DeleteByPrefix(cacheKeyPrefixUser + ":")
|
||||
@@ -509,15 +454,3 @@ func buildGroupMembersKey(groupID uint) string {
|
||||
}
|
||||
|
||||
var _ Cache = (*LayeredCache)(nil)
|
||||
|
||||
func (c *LayeredCache) logStats() {
|
||||
stats := c.GetStats()
|
||||
zap.L().Debug("LayeredCache stats",
|
||||
zap.Int64("local_hits", stats.LocalHits),
|
||||
zap.Int64("redis_hits", stats.RedisHits),
|
||||
zap.Int64("misses", stats.Misses),
|
||||
zap.Int64("sets", stats.Sets),
|
||||
zap.Int64("invalidates", stats.Invalidates),
|
||||
zap.Float64("hit_rate", c.GetHitRate()),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user