lib/app/period-cache.go

37 lines
759 B
Go
Raw Normal View History

2021-02-02 04:19:16 +00:00
package app
import "sync"
var periodCache = periodCacheType{
obj: map[string]map[string]*CalendarPeriod{},
}
// periodCacheType maps [accountID][dateString]
type periodCacheType struct {
sync.RWMutex
obj map[string]map[string]*CalendarPeriod
}
func (m *periodCacheType) get(accountID, dateStr string) (*CalendarPeriod, bool) {
m.RLock()
defer m.RUnlock()
r, ok := m.obj[accountID][dateStr]
return r, ok
}
func (m *periodCacheType) put(accountID, dateStr string, period *CalendarPeriod) {
m.init(accountID)
m.Lock()
defer m.Unlock()
m.obj[accountID][dateStr] = period
}
func (m *periodCacheType) init(accountID string) {
m.RLock()
defer m.RUnlock()
_, ok := m.obj[accountID]
if !ok {
m.obj[accountID] = map[string]*CalendarPeriod{}
}
}