Files
file-transfer-go/internal/services/memory_store.go
MatrixSeven 70ad644a71 第一版本
2025-07-28 16:33:10 +08:00

62 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package services
import (
"fmt"
"sync"
"time"
"chuan/internal/models"
)
// 内存存储生产环境应使用Redis
type MemoryStore struct {
files map[string]*models.FileInfo
mutex sync.RWMutex
}
var globalStore = &MemoryStore{
files: make(map[string]*models.FileInfo),
}
// StoreFileInfo 存储文件信息
func (ms *MemoryStore) StoreFileInfo(fileInfo *models.FileInfo) error {
ms.mutex.Lock()
defer ms.mutex.Unlock()
ms.files[fileInfo.Code] = fileInfo
return nil
}
// GetFileInfo 获取文件信息
func (ms *MemoryStore) GetFileInfo(code string) (*models.FileInfo, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
fileInfo, exists := ms.files[code]
if !exists {
return nil, fmt.Errorf("文件不存在或已过期")
}
// 检查是否过期
if time.Now().After(fileInfo.ExpiryTime) {
delete(ms.files, code)
return nil, fmt.Errorf("文件已过期")
}
return fileInfo, nil
}
// DeleteFileInfo 删除文件信息
func (ms *MemoryStore) DeleteFileInfo(code string) error {
ms.mutex.Lock()
defer ms.mutex.Unlock()
delete(ms.files, code)
return nil
}
// GetStore 获取全局存储实例
func GetStore() *MemoryStore {
return globalStore
}