refactor(layout): consolidate backend codebase into backend/ package and clean root directory

- Moved cmd/, core/, plugins/, pkg/, downstream/, and main.go into backend/ directory
- Batch updated all Go source files to import github.com/Rain-kl/Wavelet/backend/...
- Updated Makefile, scripts/swagger.sh, architecture guards, and platform skills
- Passed all quality gates (100% tests, 0 lint issues, clean build)
This commit is contained in:
ryan
2026-08-28 12:56:02 +08:00
parent 33b38f8687
commit 43dc97e48c
319 changed files with 912 additions and 1031 deletions
+12
View File
@@ -0,0 +1,12 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package cache
const (
errRedisHashSetFailed = "failed to set redis hash: %w"
errRedisHashDeleteFailed = "failed to delete redis hash field: %w"
errUnmarshalDataFailed = "failed to unmarshal data: %w"
errMarshalDataFailed = "failed to marshal data: %w"
errRedisKeySetFailed = "failed to set redis key: %w"
)
+267
View File
@@ -0,0 +1,267 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
// Package cache provides the multi-tier caching infrastructure plugin for Cordis.
package cache
import (
"context"
"encoding/json"
"errors"
"sync"
"time"
"github.com/Rain-kl/Wavelet/backend/core"
"github.com/Rain-kl/Wavelet/backend/core/contracts"
"github.com/Rain-kl/Wavelet/backend/pkg/cache/ram"
"github.com/Rain-kl/Wavelet/backend/pkg/util"
"github.com/redis/go-redis/v9"
)
const (
defaultRAMCapacity = 10000
defaultPubSubChannel = "wavelet:cache:invalidation"
)
type ramEntry struct {
data []byte
expireAt time.Time
}
// Option configures the cache plugin.
type Option func(*Plugin)
// WithRedis sets an explicit Redis client instance.
func WithRedis(client redis.UniversalClient) Option {
return func(p *Plugin) {
p.redisClient = client
}
}
// WithKeyPrefix sets a custom Redis key prefix.
func WithKeyPrefix(prefix string) Option {
return func(p *Plugin) {
p.keyPrefix = prefix
}
}
// WithRAMCapacity sets the maximum capacity for the L1 RAM cache.
func WithRAMCapacity(capacity int) Option {
return func(p *Plugin) {
p.ramCapacity = capacity
}
}
// Plugin implements core.Plugin to provide contracts.CacheService.
type Plugin struct {
redisClient redis.UniversalClient
keyPrefix string
ramCapacity int
}
// New creates a new cache infrastructure plugin.
func New(opts ...Option) *Plugin {
p := &Plugin{
ramCapacity: defaultRAMCapacity,
}
for _, opt := range opts {
if opt != nil {
opt(p)
}
}
return p
}
// Name returns the unique identifier of the cache plugin.
func (p *Plugin) Name() string {
return "cache"
}
// Apply mounts the multi-layer cache service into the Context.
func (p *Plugin) Apply(ctx *core.Context) error {
redisClient := p.redisClient
if redisClient == nil {
redisClient = Redis
}
ramCache, err := ram.New[string, ramEntry](ram.Options{
MaximumSize: p.ramCapacity,
})
if err != nil {
return err
}
svc := &cacheServiceImpl{
ramCache: ramCache,
redisClient: redisClient,
keyPrefix: p.keyPrefix,
pubSubChannel: defaultPubSubChannel,
stopCh: make(chan struct{}),
}
if redisClient != nil {
svc.startPubSubListener()
ctx.OnDispose(func() error {
svc.stopPubSubListener()
return nil
})
}
core.Provide[contracts.CacheService](ctx, svc)
return nil
}
type cacheServiceImpl struct {
ramCache *ram.Cache[string, ramEntry]
redisClient redis.UniversalClient
keyPrefix string
pubSubChannel string
subOnce sync.Once
stopOnce sync.Once
stopCh chan struct{}
pubsub *redis.PubSub
}
func (s *cacheServiceImpl) prefixedKey(key string) string {
if s.keyPrefix != "" {
return s.keyPrefix + key
}
return PrefixedKey(key)
}
func (s *cacheServiceImpl) startPubSubListener() {
if s.redisClient == nil {
return
}
s.subOnce.Do(func() {
pubsub := s.redisClient.Subscribe(context.Background(), s.pubSubChannel)
s.pubsub = pubsub
util.Go(func() {
ch := pubsub.Channel()
for {
select {
case <-s.stopCh:
return
case msg, ok := <-ch:
if !ok {
return
}
if msg != nil && msg.Payload != "" {
s.ramCache.Invalidate(msg.Payload)
}
}
}
})
})
}
func (s *cacheServiceImpl) stopPubSubListener() {
s.stopOnce.Do(func() {
close(s.stopCh)
if s.pubsub != nil {
_ = s.pubsub.Close()
}
})
}
func (s *cacheServiceImpl) Get(ctx context.Context, key string, target any) error {
// 1. Check L1 RAM cache
if entry, ok := s.ramCache.GetIfPresent(key); ok {
if entry.expireAt.IsZero() || time.Now().Before(entry.expireAt) {
return json.Unmarshal(entry.data, target)
}
// Expired in L1 RAM
s.ramCache.Invalidate(key)
}
// 2. Check L2 Redis cache
if s.redisClient != nil {
data, err := s.redisClient.Get(ctx, s.prefixedKey(key)).Bytes()
if err == nil {
// Backfill L1 RAM cache
s.ramCache.Set(key, ramEntry{
data: data,
})
return json.Unmarshal(data, target)
} else if !errors.Is(err, redis.Nil) {
return err
}
}
return contracts.ErrCacheMiss
}
func (s *cacheServiceImpl) Set(ctx context.Context, key string, value any, ttl time.Duration) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
var expireAt time.Time
if ttl > 0 {
expireAt = time.Now().Add(ttl)
}
// 1. Write L1 RAM cache
s.ramCache.Set(key, ramEntry{
data: data,
expireAt: expireAt,
})
// 2. Write L2 Redis cache
if s.redisClient != nil {
if err := s.redisClient.Set(ctx, s.prefixedKey(key), data, ttl).Err(); err != nil {
return err
}
}
return nil
}
func (s *cacheServiceImpl) Delete(ctx context.Context, key string) error {
// 1. Evict L1 RAM
s.ramCache.Invalidate(key)
// 2. Evict L2 Redis and broadcast invalidation to cluster nodes
if s.redisClient != nil {
if err := s.redisClient.Del(ctx, s.prefixedKey(key)).Err(); err != nil && !errors.Is(err, redis.Nil) {
return err
}
_ = s.redisClient.Publish(ctx, s.pubSubChannel, key).Err()
}
return nil
}
func (s *cacheServiceImpl) Invalidate(ctx context.Context, key string) error {
return s.Delete(ctx, key)
}
func (s *cacheServiceImpl) GetOrSet(ctx context.Context, key string, target any, ttl time.Duration, loader func() (any, error)) error {
err := s.Get(ctx, key, target)
if err == nil {
return nil
}
if !errors.Is(err, contracts.ErrCacheMiss) {
return err
}
val, err := loader()
if err != nil {
return err
}
if err := s.Set(ctx, key, val, ttl); err != nil {
return err
}
data, err := json.Marshal(val)
if err != nil {
return err
}
return json.Unmarshal(data, target)
}
+82
View File
@@ -0,0 +1,82 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package cache_test
import (
"context"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/Rain-kl/Wavelet/backend/core"
"github.com/Rain-kl/Wavelet/backend/core/contracts"
"github.com/Rain-kl/Wavelet/backend/plugins/infra/cache"
)
func TestCachePluginOperations(t *testing.T) {
mr, err := miniredis.Run()
require.NoError(t, err)
defer mr.Close()
rdb := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
defer func() { _ = rdb.Close() }()
p := cache.New(
cache.WithRedis(rdb),
cache.WithKeyPrefix("app:"),
cache.WithRAMCapacity(500),
)
assert.Equal(t, "cache", p.Name())
ctx := core.NewContext(context.Background())
require.NoError(t, p.Apply(ctx))
svc, err := core.Inject[contracts.CacheService](ctx)
require.NoError(t, err)
type Data struct {
Value string `json:"value"`
}
testCtx := context.Background()
// 1. ErrCacheMiss
var out Data
err = svc.Get(testCtx, "missing", &out)
assert.ErrorIs(t, err, contracts.ErrCacheMiss)
// 2. Set & Get
in := Data{Value: "hello"}
require.NoError(t, svc.Set(testCtx, "key1", in, 5*time.Minute))
require.NoError(t, svc.Get(testCtx, "key1", &out))
assert.Equal(t, "hello", out.Value)
// 3. GetOrSet
var target Data
err = svc.GetOrSet(testCtx, "key1", &target, time.Minute, func() (any, error) {
return Data{Value: "from_loader"}, nil
})
require.NoError(t, err)
assert.Equal(t, "hello", target.Value)
err = svc.GetOrSet(testCtx, "key2", &target, time.Minute, func() (any, error) {
return Data{Value: "from_loader"}, nil
})
require.NoError(t, err)
assert.Equal(t, "from_loader", target.Value)
// 4. Invalidate / Delete
require.NoError(t, svc.Invalidate(testCtx, "key1"))
err = svc.Get(testCtx, "key1", &out)
assert.ErrorIs(t, err, contracts.ErrCacheMiss)
require.NoError(t, ctx.Dispose())
}
+198
View File
@@ -0,0 +1,198 @@
// Copyright 2025 linux.do
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package cache
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/Rain-kl/Wavelet/backend/pkg/config"
"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
"github.com/redis/go-redis/v9/maintnotifications"
"go.opentelemetry.io/otel/attribute"
)
var (
// Redis 全局 Redis 客户端实例
Redis redis.UniversalClient
)
func init() {
cfg := config.Config.Redis
if !cfg.Enabled {
log.Println("[Redis] is disabled, skipping Redis initialization")
return
}
if cfg.ClusterMode {
// Cluster 模式
Redis = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: cfg.Addrs,
Username: cfg.Username,
Password: cfg.Password,
PoolSize: cfg.PoolSize,
MinIdleConns: cfg.MinIdleConn,
DialTimeout: time.Duration(cfg.DialTimeout) * time.Second,
ReadTimeout: time.Duration(cfg.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(cfg.WriteTimeout) * time.Second,
MaxRetries: cfg.MaxRetries,
PoolTimeout: time.Duration(cfg.PoolTimeout) * time.Second,
ConnMaxIdleTime: time.Duration(cfg.ConnMaxIdleTime) * time.Second,
MaintNotificationsConfig: redisMaintNotificationsConfig(cfg.MaintNotifications),
})
log.Println("[Redis] initialized in Cluster mode")
} else {
// Standalone 或 Sentinel 模式
options := &redis.UniversalOptions{
Addrs: cfg.Addrs,
MasterName: cfg.MasterName, // 非空时启用 Sentinel
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
PoolSize: cfg.PoolSize,
MinIdleConns: cfg.MinIdleConn,
DialTimeout: time.Duration(cfg.DialTimeout) * time.Second,
ReadTimeout: time.Duration(cfg.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(cfg.WriteTimeout) * time.Second,
MaxRetries: cfg.MaxRetries,
PoolTimeout: time.Duration(cfg.PoolTimeout) * time.Second,
ConnMaxIdleTime: time.Duration(cfg.ConnMaxIdleTime) * time.Second,
MaintNotificationsConfig: redisMaintNotificationsConfig(cfg.MaintNotifications),
}
if cfg.MasterName != "" {
client := redis.NewFailoverClient(options.Failover())
// FailoverOptions 暂不暴露该配置,在首次建连前写入客户端选项。
client.Options().MaintNotificationsConfig = redisMaintNotificationsConfig(cfg.MaintNotifications)
Redis = client
log.Println("[Redis] initialized in Sentinel mode")
} else {
Redis = redis.NewUniversalClient(options)
log.Println("[Redis] initialized in Standalone mode")
}
}
// OpenTelemetry 追踪(UniversalClient 兼容)
if err := redisotel.InstrumentTracing(
Redis,
redisotel.WithAttributes(
attribute.String("db.instance", fmt.Sprintf("%v", cfg.DB)),
attribute.String("db.ip", strings.Join(cfg.Addrs, ",")),
attribute.String("db.system", "Redis"),
),
); err != nil {
log.Fatalf("[Redis] failed to init trace: %v\n", err)
}
// 测试连接
_, err := Redis.Ping(context.Background()).Result()
if err != nil {
log.Fatalf("[Redis] failed to connect to redis: %v\n", err)
}
}
func redisMaintNotificationsConfig(enabled bool) *maintnotifications.Config {
mode := maintnotifications.ModeDisabled
if enabled {
mode = maintnotifications.ModeAuto
}
return &maintnotifications.Config{Mode: mode}
}
// PrefixedKey 返回带前缀的 Key
func PrefixedKey(key string) string {
prefix := config.Config.Redis.KeyPrefix
if prefix == "" {
return key
}
return prefix + key
}
// HSetJSON 将泛型数据序列化为 JSON 并设置到 Redis Hash
// ctx: 上下文
// hashKey: Redis Hash key
// fieldKey: Hash field key
// data: 要存储的数据(泛型)
func HSetJSON[T any](ctx context.Context, hashKey, fieldKey string, data T) error {
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
if err := Redis.HSet(ctx, PrefixedKey(hashKey), fieldKey, jsonData).Err(); err != nil {
return fmt.Errorf(errRedisHashSetFailed, err)
}
return nil
}
// HDel removes one or more fields from a Redis Hash.
func HDel(ctx context.Context, hashKey string, fieldKeys ...string) error {
if Redis == nil || len(fieldKeys) == 0 {
return nil
}
if err := Redis.HDel(ctx, PrefixedKey(hashKey), fieldKeys...).Err(); err != nil {
return fmt.Errorf(errRedisHashDeleteFailed, err)
}
return nil
}
// HGetJSON 从 Redis Hash 获取数据并反序列化为泛型类型
// ctx: 上下文
// hashKey: Redis Hash key
// fieldKey: Hash field key
// data: 用于接收数据的指针(泛型)
func HGetJSON[T any](ctx context.Context, hashKey, fieldKey string, data *T) error {
val, err := Redis.HGet(ctx, PrefixedKey(hashKey), fieldKey).Result()
if err != nil {
return err
}
if err := json.Unmarshal([]byte(val), data); err != nil {
return fmt.Errorf(errUnmarshalDataFailed, err)
}
return nil
}
// GetJSON 从Redis获取数据并反序列化为泛型类型
// ctx: 上下文
// key: Redis key
// data: 用于接收数据的指针(泛型)
func GetJSON[T any](ctx context.Context, key string, data *T) error {
val, err := Redis.Get(ctx, PrefixedKey(key)).Bytes()
if err != nil {
return err
}
if err := json.Unmarshal(val, data); err != nil {
return fmt.Errorf(errUnmarshalDataFailed, err)
}
return nil
}
// SetJSON 将泛型数据序列化为JSON并设置到Redis
// ctx: 上下文
// key: Redis key
// data: 要存储的数据(泛型)
// expiration: 过期时间
func SetJSON[T any](ctx context.Context, key string, data T, expiration time.Duration) error {
jsonData, err := json.Marshal(data)
if err != nil {
return fmt.Errorf(errMarshalDataFailed, err)
}
if err := Redis.Set(ctx, PrefixedKey(key), jsonData, expiration).Err(); err != nil {
return fmt.Errorf(errRedisKeySetFailed, err)
}
return nil
}
+28
View File
@@ -0,0 +1,28 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package cache
import (
"testing"
"github.com/redis/go-redis/v9/maintnotifications"
)
func TestRedisMaintNotificationsConfig(t *testing.T) {
for _, test := range []struct {
name string
enabled bool
want maintnotifications.Mode
}{
{name: "disabled by default", enabled: false, want: maintnotifications.ModeDisabled},
{name: "auto when enabled", enabled: true, want: maintnotifications.ModeAuto},
} {
t.Run(test.name, func(t *testing.T) {
cfg := redisMaintNotificationsConfig(test.enabled)
if cfg.Mode != test.want {
t.Fatalf("maintenance notifications mode = %v, want %v", cfg.Mode, test.want)
}
})
}
}