mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-20 16:45:56 +08:00
53 lines
748 B
Go
53 lines
748 B
Go
package listing1
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type now func() time.Time
|
|
|
|
type Cache struct {
|
|
mu sync.RWMutex
|
|
events []Event
|
|
now now
|
|
}
|
|
|
|
func NewCache() *Cache {
|
|
return &Cache{
|
|
events: make([]Event, 0),
|
|
now: time.Now,
|
|
}
|
|
}
|
|
|
|
type Event struct {
|
|
Timestamp time.Time
|
|
Data string
|
|
}
|
|
|
|
func (c *Cache) TrimBefore(since time.Duration) {
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
|
|
t := time.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
|
|
}
|