-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcachedcalendar.go
93 lines (76 loc) · 2.41 KB
/
cachedcalendar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package kal
import (
"time"
)
// A CachedCalendar wraps and caches a Calendar
type CachedCalendar struct {
cal Calendar
cacheRed map[time.Time]string // red day description
cacheNotable map[time.Time]string // notable day description
cacheFlag map[time.Time]bool // flag flying day
}
// Creates a new CachedCalendar that wraps and caches the given Calendar.
// A CachedCalendar is also a Calendar itself, since it implements the
// Calendar interface.
func NewCachedCalendar(cal Calendar) CachedCalendar {
var calca CachedCalendar
calca.cal = cal
calca.cacheRed = make(map[time.Time]string)
calca.cacheNotable = make(map[time.Time]string)
calca.cacheFlag = make(map[time.Time]bool)
return calca
}
// Wraps the RedDay function and caches some of the results
func (calca CachedCalendar) RedDay(date time.Time) (bool, string, bool) {
// Return from cache, if it's there
desc, ok := calca.cacheRed[date]
if ok {
return ok, desc, calca.cacheFlag[date]
}
// Get the information from the calendar
red, desc, flag := calca.cal.RedDay(date)
// Add red days to the cache
// TODO: Also cache non-red days
if red {
calca.cacheRed[date] = desc
calca.cacheFlag[date] = flag
}
return red, desc, flag
}
// Wraps the NotableDay function and caches some of the results
func (calca CachedCalendar) NotableDay(date time.Time) (bool, string, bool) {
// Return from cache, if it's there
desc, ok := calca.cacheNotable[date]
if ok {
return ok, desc, calca.cacheFlag[date]
}
// Get the information from the calendar
notable, desc, flag := calca.cal.NotableDay(date)
// Add notable days to cache
// TODO: Also cache non-notable days
if notable {
calca.cacheNotable[date] = desc
calca.cacheFlag[date] = flag
}
return notable, desc, flag
}
// --- These are here just to satisfy the Calendar interface ---
// Wraps the NotablePeriod function
func (calca CachedCalendar) NotablePeriod(date time.Time) (bool, string) {
return calca.cal.NotablePeriod(date)
}
// Wraps the DayName function
func (calca CachedCalendar) DayName(date time.Weekday) string {
return calca.cal.DayName(date)
}
// Wraps the NormalDay function
func (calca CachedCalendar) NormalDay() string {
return calca.cal.NormalDay()
}
// Wraps the MonthName function
func (calca CachedCalendar) MonthName(month time.Month) string {
return calca.cal.MonthName(month)
}
func (calca CachedCalendar) MondayFirst() bool {
return calca.cal.MondayFirst()
}