mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
528240026d
- make format 现在与 code-check 使用同一格式化器(golangci-lint fmt),消除 goimports -local 与 gofumpt 的格式拉锯 - .golangci.yml 关闭默认 50/3 截断,完整上报所有问题(只增强不弱化) - 全库 gofumpt 规范化(203 files, 纯格式无行为变更)
101 lines
2.0 KiB
Go
101 lines
2.0 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cache_memory
|
|
|
|
import (
|
|
"Wavelet/core"
|
|
"Wavelet/core/contracts"
|
|
"Wavelet/pkg/cache/ram"
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
type ramEntry struct {
|
|
data []byte
|
|
expireAt time.Time
|
|
}
|
|
|
|
type memoryCacheService struct {
|
|
ramCache *ram.Cache[string, ramEntry]
|
|
events *core.EventBus
|
|
}
|
|
|
|
func newMemoryCacheService(capacity int, events *core.EventBus) (*memoryCacheService, error) {
|
|
ramCache, err := ram.New[string, ramEntry](ram.Options{
|
|
MaximumSize: capacity,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &memoryCacheService{
|
|
ramCache: ramCache,
|
|
events: events,
|
|
}, nil
|
|
}
|
|
|
|
func (s *memoryCacheService) Get(_ context.Context, key string, target any) error {
|
|
if entry, ok := s.ramCache.GetIfPresent(key); ok {
|
|
if entry.expireAt.IsZero() || time.Now().Before(entry.expireAt) {
|
|
return json.Unmarshal(entry.data, target)
|
|
}
|
|
// Expired
|
|
s.ramCache.Invalidate(key)
|
|
}
|
|
return contracts.ErrCacheMiss
|
|
}
|
|
|
|
func (s *memoryCacheService) Set(_ 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)
|
|
}
|
|
|
|
s.ramCache.Set(key, ramEntry{
|
|
data: data,
|
|
expireAt: expireAt,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (s *memoryCacheService) Delete(ctx context.Context, key string) error {
|
|
s.ramCache.Invalidate(key)
|
|
if s.events != nil {
|
|
_ = s.events.Emit(ctx, "cache:invalidate", key)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *memoryCacheService) Invalidate(ctx context.Context, key string) error {
|
|
return s.Delete(ctx, key)
|
|
}
|
|
|
|
func (s *memoryCacheService) 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
|
|
}
|
|
|
|
val, err := loader()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if setErr := s.Set(ctx, key, val, ttl); setErr != nil {
|
|
return setErr
|
|
}
|
|
|
|
data, err := json.Marshal(val)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return json.Unmarshal(data, target)
|
|
}
|