-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
55 lines (43 loc) · 926 Bytes
/
cache.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
package pkg
import (
"sync"
"time"
"google.golang.org/api/youtube/v3"
)
const expiry = 24 * time.Hour
type item struct {
Updated time.Time
Subs []*youtube.Subscription
}
var (
subsCache = make(map[string]*item)
subsCacheLock sync.RWMutex
)
func readSubsCache(key string) []*youtube.Subscription {
subsCacheLock.RLock()
defer subsCacheLock.RUnlock()
item := subsCache[key]
if item == nil {
return nil
}
if !item.Updated.After(time.Now().Add(-expiry)) {
return nil
}
subsCopy := make([]*youtube.Subscription, len(item.Subs))
copy(subsCopy, item.Subs)
return subsCopy
}
func storeSubsCache(key string, subs []*youtube.Subscription) {
subsCacheLock.Lock()
defer subsCacheLock.Unlock()
if subs == nil {
subsCache[key] = nil
return
}
subsCopy := make([]*youtube.Subscription, len(subs))
copy(subsCopy, subs)
subsCache[key] = &item{
Updated: time.Now(),
Subs: subsCopy,
}
}