package service import ( "strings" "sync" "time" ) type LiveDownload struct { Logs strings.Builder LastUpdate time.Time } type ProgressCache struct { mu sync.RWMutex data map[int64]*LiveDownload } func NewProgressCache() *ProgressCache { return &ProgressCache{ data: make(map[int64]*LiveDownload), } } func (c *ProgressCache) Set(id int64, d *LiveDownload) { c.mu.Lock() defer c.mu.Unlock() c.data[id] = d } func (c *ProgressCache) Get(id int64) (*LiveDownload, bool) { c.mu.RLock() defer c.mu.RUnlock() d, ok := c.data[id] return d, ok } func (c *ProgressCache) AppendLog(id int64, line string) { c.mu.Lock() defer c.mu.Unlock() if d, ok := c.data[id]; ok { d.Logs.WriteString(line) d.Logs.WriteByte('\n') d.LastUpdate = time.Now() } } func (c *ProgressCache) GetLogs(id int64) string { c.mu.RLock() defer c.mu.RUnlock() if d, ok := c.data[id]; ok { return d.Logs.String() } return "" } func (c *ProgressCache) Delete(id int64) { c.mu.Lock() defer c.mu.Unlock() delete(c.data, id) } func (c *ProgressCache) FlushLogs(id int64) string { c.mu.Lock() defer c.mu.Unlock() if d, ok := c.data[id]; ok { logs := d.Logs.String() return logs } return "" }