Skip to content

Commit

Permalink
Merge pull request #127 from adomaskizogian/master
Browse files Browse the repository at this point in the history
make isHolCache access thread-safe
  • Loading branch information
rickar committed Jun 19, 2024
2 parents fdbdbbe + 211d788 commit b70653d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions v2/cal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

package cal

import "time"
import (
"sync"
"time"
)

// DefaultLoc is the default time.Location to use in functions that do not
// require a full time.Time value.
Expand All @@ -23,6 +26,9 @@ type Calendar struct {
Cacheable bool // indicates that holiday calcs can be cached (don't change holiday defs while enabled)

isHolCache map[holCacheKey]*holCacheEntry // cached results for IsHoliday

isHolCacheInitOnce sync.Once
isHolCacheMutex sync.RWMutex
}

type holCacheKey struct {
Expand Down Expand Up @@ -76,12 +82,16 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
year, month, day := date.Date()

if c.Cacheable {
if c.isHolCache == nil {
c.isHolCacheInitOnce.Do(func() {
c.isHolCache = make(map[holCacheKey]*holCacheEntry)
}
})

c.isHolCacheMutex.RLock()
if v, ok := c.isHolCache[holCacheKey{year: year, month: month, day: day}]; ok {
c.isHolCacheMutex.RUnlock()
return v.act, v.obs, v.hol
}
c.isHolCacheMutex.RUnlock()
}

for _, hol := range c.Holidays {
Expand All @@ -99,9 +109,11 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
}
if actMatch || obsMatch {
if c.Cacheable {
c.isHolCacheMutex.Lock()
c.evict()
c.isHolCache[holCacheKey{year: year, month: month, day: day}] =
&holCacheEntry{act: actMatch, obs: obsMatch, hol: hol}
c.isHolCacheMutex.Unlock()
}
return actMatch, obsMatch, hol
}
Expand All @@ -122,9 +134,11 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
}
if obsMatch {
if c.Cacheable {
c.isHolCacheMutex.Lock()
c.evict()
c.isHolCache[holCacheKey{year: year, month: month, day: day}] =
&holCacheEntry{act: false, obs: obsMatch, hol: hol}
c.isHolCacheMutex.Unlock()
}
return false, obsMatch, hol
}
Expand All @@ -137,9 +151,11 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
}
if obsMatch {
if c.Cacheable {
c.isHolCacheMutex.Lock()
c.evict()
c.isHolCache[holCacheKey{year: year, month: month, day: day}] =
&holCacheEntry{act: false, obs: obsMatch, hol: hol}
c.isHolCacheMutex.Unlock()
}
return false, obsMatch, hol
}
Expand All @@ -148,8 +164,10 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
}

if c.Cacheable {
c.isHolCacheMutex.Lock()
c.evict()
c.isHolCache[holCacheKey{year: year, month: month, day: day}] = holFalseEntry
c.isHolCacheMutex.Unlock()
}
return false, false, nil
}
Expand Down

0 comments on commit b70653d

Please sign in to comment.