forked from schollz/find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
145 lines (125 loc) · 3.12 KB
/
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2015-2016 Zack Scholl. All rights reserved.
// Use of this source code is governed by a AGPL
// license that can be found in the LICENSE file.
// cache.go handles the global variables for caching and the clearing.
package main
import (
"strings"
"sync"
"time"
)
var psCache = struct {
sync.RWMutex
m map[string]FullParameters
}{m: make(map[string]FullParameters)}
var usersCache = struct {
sync.RWMutex
m map[string][]string
}{m: make(map[string][]string)}
var userPositionCache = struct {
sync.RWMutex
m map[string]UserPositionJSON
}{m: make(map[string]UserPositionJSON)}
var isLearning = struct {
sync.RWMutex
m map[string]bool
}{m: make(map[string]bool)}
func init() {
go clearCache()
go clearCacheFast()
}
func clearCacheFast() {
for {
go resetCache("userCache")
time.Sleep(time.Second * 30)
}
}
func clearCache() {
for {
//Debug.Println("Resetting cache")
go resetCache("isLearning")
go resetCache("psCache")
go resetCache("userPositionCache")
time.Sleep(time.Second * 600)
}
}
func resetCache(cache string) {
if cache == "userCache" {
usersCache.Lock()
usersCache.m = make(map[string][]string)
usersCache.Unlock()
} else if cache == "userPositionCache" {
userPositionCache.Lock()
userPositionCache.m = make(map[string]UserPositionJSON)
userPositionCache.Unlock()
} else if cache == "psCache" {
psCache.Lock()
psCache.m = make(map[string]FullParameters)
psCache.Unlock()
} else if cache == "isLearning" {
isLearning.Lock()
isLearning.m = make(map[string]bool)
isLearning.Unlock()
}
}
func getLearningCache(group string) (bool, bool) {
//Debug.Println("getLearningCache")
isLearning.RLock()
cached, ok := isLearning.m[group]
isLearning.RUnlock()
return cached, ok
}
func setLearningCache(group string, val bool) {
isLearning.Lock()
isLearning.m[group] = val
isLearning.Unlock()
}
func getUserCache(group string) ([]string, bool) {
//Debug.Println("Getting userCache")
usersCache.RLock()
cached, ok := usersCache.m[group]
usersCache.RUnlock()
return cached, ok
}
func setUserCache(group string, users []string) {
usersCache.Lock()
usersCache.m[group] = users
usersCache.Unlock()
}
func appendUserCache(group string, user string) {
usersCache.Lock()
if _, ok := usersCache.m[group]; ok {
if len(usersCache.m[group]) == 0 {
usersCache.m[group] = append([]string{}, strings.ToLower(user))
}
}
usersCache.Unlock()
}
func getPsCache(group string) (FullParameters, bool) {
//Debug.Println("Getting pscache")
psCache.RLock()
psCached, ok := psCache.m[group]
psCache.RUnlock()
return psCached, ok
}
func setPsCache(group string, ps FullParameters) {
//Debug.Println("Setting pscache")
psCache.Lock()
psCache.m[group] = ps
psCache.Unlock()
return
}
func getUserPositionCache(group string) (UserPositionJSON, bool) {
//Debug.Println("getUserPositionCache")
userPositionCache.RLock()
cached, ok := userPositionCache.m[group]
userPositionCache.RUnlock()
return cached, ok
}
func setUserPositionCache(group string, p UserPositionJSON) {
//Debug.Println("setUserPositionCache")
userPositionCache.Lock()
userPositionCache.m[group] = p
userPositionCache.Unlock()
return
}