100-go-mistakes/11-testing/87-time-api/listing3/main.go
2021-12-27 15:56:17 +01:00

43 lines
622 B
Go

package listing1
import (
"sync"
"time"
)
type Cache struct {
mu sync.RWMutex
events []Event
}
type Event struct {
Timestamp time.Time
Data string
}
func (c *Cache) TrimBefore(now time.Time, since time.Duration) {
c.mu.RLock()
defer c.mu.RUnlock()
t := now.Add(-since)
for i := 0; i < len(c.events); i++ {
if c.events[i].Timestamp.After(t) {
c.events = c.events[i:]
return
}
}
}
func (c *Cache) Add(events []Event) {
c.mu.Lock()
defer c.mu.Unlock()
c.events = append(c.events, events...)
}
func (c *Cache) GetAll() []Event {
c.mu.RLock()
defer c.mu.RUnlock()
return c.events
}