Skip to content

Commit

Permalink
Added learning cache
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Jul 21, 2016
1 parent 99c9dff commit 4ea830f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
48 changes: 36 additions & 12 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ var userPositionCache = struct {
m map[string]UserPositionJSON
}{m: make(map[string]UserPositionJSON)}

var isLearning map[string]bool
var isLearning = struct {
sync.RWMutex
m map[string]bool
}{m: make(map[string]bool)}

func init() {
go clearCache()
Expand All @@ -36,12 +39,10 @@ func init() {
func clearCache() {
for {
Debug.Println("Resetting cache")
isLearning = make(map[string]bool)
psCache.Lock()
psCache.m = make(map[string]FullParameters)
psCache.Unlock()
resetCache("userCache")
resetCache("userPositionCache")
go resetCache("isLearning")
go resetCache("psCache")
go resetCache("userCache")
go resetCache("userPositionCache")
time.Sleep(time.Second * 10)
}
}
Expand All @@ -55,9 +56,31 @@ func resetCache(cache string) {
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()
Expand All @@ -66,6 +89,12 @@ func getUserCache(group string) ([]string, bool) {
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 {
Expand All @@ -75,11 +104,6 @@ func appendUserCache(group string, user string) {
}
usersCache.Unlock()
}
func setUserCache(group string, users []string) {
usersCache.Lock()
usersCache.m[group] = users
usersCache.Unlock()
}

func getPsCache(group string) (FullParameters, bool) {
Debug.Println("Getting pscache")
Expand Down
7 changes: 4 additions & 3 deletions fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func learnFingerprint(jsonFingerprint Fingerprint) (string, bool) {
return "No fingerprints found to insert, see API", false
}
putFingerprintIntoDatabase(jsonFingerprint, "fingerprints")
isLearning[strings.ToLower(jsonFingerprint.Group)] = true
setLearningCache(strings.ToLower(jsonFingerprint.Group), true)
message := "Inserted fingerprint containing " + strconv.Itoa(len(jsonFingerprint.WifiFingerprint)) + " APs for " + jsonFingerprint.Username + " at " + jsonFingerprint.Location
return message, true
}
Expand All @@ -172,11 +172,12 @@ func trackFingerprint(jsonFingerprint Fingerprint) (string, bool, string, map[st
if len(jsonFingerprint.Username) == 0 {
return "No username defined, see API", false, "", bayes, make(map[string]float64)
}
if wasLearning, ok := isLearning[strings.ToLower(jsonFingerprint.Group)]; ok {
wasLearning, ok := getLearningCache(strings.ToLower(jsonFingerprint.Group))
if ok {
if wasLearning {
Debug.Println("Was learning, calculating priors")
group := strings.ToLower(jsonFingerprint.Group)
isLearning[group] = false
setLearningCache(group, false)
optimizePriorsThreaded(group)
if RuntimeArgs.Svm {
dumpFingerprintsSVM(group)
Expand Down

0 comments on commit 4ea830f

Please sign in to comment.